Loading...

Go Back

Next page
Go Back Course Outline

Html5 Full Course


Tables in Html

Tables in HTML

Tables are used to display data in a tabular format. In HTML, a table is created using the <table> tag. The table tag is used to define a table. The <tr> tag is used to define a row in a table. The <td> tag is used to define a cell in a table. The <th> tag is used to define a header cell in a table. The <caption> tag is used to define a table caption. The <colgroup> tag is used to define a group of columns in a table. The <col> tag is used to define a column in a table. The <thead> tag is used to group the header content in a table. The <tbody> tag is used to group the body content in a table. The <tfoot> tag is used to group the footer content in a table.

Creating a Table

To create a table in HTML, you need to use the <table> tag. The <table> tag is used to define a table. The <tr> tag is used to define a row in a table. The <td> tag is used to define a cell in a table. The <th> tag is used to define a header cell in a table. The <caption> tag is used to define a table caption. The <colgroup> tag is used to define a group of columns in a table. The <col> tag is used to define a column in a table. The <thead> tag is used to group the header content in a table. The <tbody> tag < lang="en"> HTML Tables Explained

HTML Tables Explained

1. Basic Table Structure

Here is a basic table structure with rows and columns:

Header 1 Header 2 Header 3
Data 1 Data 2 Data 3
Data 4 Data 5 Data 6
                            <table>
                              <tr>
                                <th>Header 1</th>
                                <th>Header 2</th>
                                <th>Header 3</th>
                              </tr>
                              <tr>
                                <td>Data 1</td>
                                <td>Data 2</td>
                                <td>Data 3</td>
                              </tr>
                              <tr>
                                <td>Data 4</td>
                                <td>Data 5</td>
                                <td>Data 6</td>
                              </tr>
                            </table>
                                    


2. Grouping Table Sections: <thead>, <tbody>, and <tfoot>

Grouping table sections allows for better organization and easier styling:

Header 1 Header 2 Header 3
Data 1 Data 2 Data 3
Data 4 Data 5 Data 6
Footer Information


                            <table>
                              <thead>
                                <tr>
                                  <th>Header 1</th>
                                  <th>Header 2</th>
                                  <th>Header 3</th>
                                </tr>
                              </thead>
                              <tbody>
                                <tr>
                                  <td>Data 1</td>
                                  <td>Data 2</td>
                                  <td>Data 3</td>
                                </tr>
                                <tr>
                                  <td>Data 4</td>
                                  <td>Data 5</td>
                                  <td>Data 6</td>
                                </tr>
                              </tbody>
                              <tfoot>
                                <tr>
                                  <td colspan="3">Footer Information</td>
                                </tr>
                              </tfoot>
                            </table>
                                    

3. Using colspan and rowspan for Merging Cells

Use colspan and rowspan to merge multiple rows or columns:

Header Spanning 2 Columns
Data spanning 2 rows Data 1
Data 2
                            <table border="1">
                              <tr>
                                <th colspan="2">Header Spanning 2 Columns</th>
                              </tr>
                              <tr>
                                <td rowspan="2">Data spanning 2 rows</td>
                                <td>Data 1</td>
                              </tr>
                              <tr>
                                <td>Data 2</td>
                              </tr>
                            </table>
                                    

4. Styling Tables with CSS

CSS can be used to style the table to improve readability:

Header 1 Header 2
Data 1 Data 2
                            <table>
                              <tr>
                                <th>Header 1</th>
                                <th>Header 2</th>
                              </tr>
                              <tr>
                                <td>Data 1</td>
                                <td>Data 2</td>
                              </tr>
                            </table>
                                    


5. Accessibility Considerations

Always use <caption> and scope for accessibility:

Sample Table with Data
Header 1 Header 2
Data 1 Data 2
                            <table>
                              <caption>Sample Table with Data</caption>
                              <tr>
                                <th scope="col">Header 1</th>
                                <th scope="col">Header 2</th>
                              </tr>
                              <tr>
                                <td>Data 1</td>
                                <td>Data 2</td>
                              </tr>
                            </table>
                                    

6. Responsive Tables

For responsiveness, wrap the table in a container with overflow:

Header 1 Header 2
Data 1 Data 2



Notes

Here are a few important things to consider when using tables:

  • Use tables for tabular data: Tables should be used to display data in a grid format, not for general layout purposes.
  • Ensure accessibility: Use <caption> and scope attributes to make your tables more accessible to screen readers.
  • Responsiveness: For mobile devices, you may need to make tables scrollable using overflow-x: auto;.
  • Use CSS for styling: Use CSS for borders, padding, and row colors to make your tables easier to read and more visually appealing.
  • Use colspan and rowspan with caution: They allow you to merge cells, but improper usage can make your tables harder to read or confusing.
Go Back

Next page