Within the <table> element, the <thead> attribute is always defined, and within the header, the <tr> attribute is located. The <tr> attribute stands for a Table row, which dictates how many columns (<td>) the table will have inside, which represent the table data. The next attribute defined in the table is <tbody>, which displays the rows. To enter a table header, we use the <th> attribute, as it emphasizes the word (uses bold font). The third attribute is <tfoot>, which is optional but welcome for calculations and represents the table footer. We can also use the <tr> and <td> sub-attributes within the footer.
When styling tables within the HTML code itself (using the style attribute), it is crucial to understand the difference between a row (<tr>) and a cell (<td> or <th>). While it might seem logical to write styles directly in the row, browsers only respect certain properties if they are written directly in the cell.
A row acts as a container for text, so properties concerning the text itself are usually successfully passed down (inherited) to all cells within it.
A cell is the basic "building block" of a table. Anything that defines the physical shape and structure of an individual square must be written here.
Golden Rule: If a style added to <tr> does not work, simply move it to <td>. The cell always has the final word in the browser.
This table provides a quick reference for all key elements and attributes for creating structured tables.
| Element/Attribute | Description | Example Code | Notes |
|---|---|---|---|
<table> |
Main container for the table. | <table> ... </table> |
Contains <thead>, <tbody>, <tfoot>. |
<caption> |
Title or description of the table (displayed above the table). | <caption>Sales 2024</caption> |
Important for accessibility. |
<thead> |
Table header (column titles). | <thead><tr>...</tr></thead> |
Usually contains <th>. |
<tbody> |
Table content (data). | <tbody><tr>...</tr></tbody> |
Main part of the table. |
<tfoot> |
Table footer (totals, notes). | <tfoot><tr>...</tr></tfoot> |
Can also be placed before <tbody>. |
<tr> |
Table row (Table Row). | <tr><td>...</td></tr> |
Does not contain data directly, only cells. |
<th> |
Header cell (Header Cell). | <th>Name</th> |
Displayed bold and centered (by default). |
<td> |
Data cell (Table Data). | <td>Data</td> |
Basic cell for content. |
colspan="n" |
Crosses multiple columns (horizontal). | <td colspan="2">Total</td> |
Useful for merging headers or totals. |
rowspan="n" |
Crosses multiple rows (vertical). | <td rowspan="2">Dept</td> |
Useful for merging categories. |
scope="col" |
Specifies that <th> is a column header. | <th scope="col">Name</th> |
Important for screen readers. |
scope="row" |
Specifies that <th> is a row header. | <th scope="row">Dept</th> |
Important for screen readers. |
border="1" |
Simple border around cells (old method). | <table border="1"> |
For modern styling use CSS (border). |
cellpadding |
Spacing between content and cell border. | <table cellpadding="10"> |
Replacement: CSS padding on td. |
cellspacing |
Spacing between cells. | <table cellspacing="0"> |
Replacement: CSS border-collapse: collapse. |