Sub topic 5
Grouping rows with thead, tbody, and tfoot
This chapter assumes you can write a basic table with table, tr, td, and th, and that you know basic CSS selectors. You will group rows into header, body, and footer sections and see what that grouping gives you.
Why group rows
A table with only tr elements treats every row the same. The browser cannot tell which row is a heading, which rows hold data, and which row summarizes the data. Row groups name those roles.
HTML has three row group elements:
| Element | Role | Typical content |
|---|---|---|
| `thead` | Header section | Column headings |
| `tbody` | Body section | The data rows |
| `tfoot` | Footer section | Totals or summaries |
Each group is a direct child of table and contains only tr elements.
A complete example
This table lists monthly expenses. It uses all three groups.
<table>
<caption>Monthly expenses</caption>
<thead>
<tr>
<th scope="col">Item</th>
<th scope="col">Cost</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Rent</th>
<td>900</td>
</tr>
<tr>
<th scope="row">Food</th>
<td>300</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row">Total</th>
<td>1200</td>
</tr>
</tfoot>
</table>The thead holds the column headings. The tbody holds the data. The tfoot holds the total.
Source order and display order
In current HTML, the tfoot element can appear after the tbody, as shown above. This is the order you should use. Older HTML 4 required tfoot before tbody, so the browser could render the footer before a very long body had loaded. Current HTML no longer requires this, and the footer is displayed at the bottom either way.
A table can have at most one thead and one tfoot. If you include a caption or colgroup, they come before the row groups.
Several tbody elements
A table may have more than one tbody. Use this when the data falls into logical sections, such as one section per region. Each tbody is a separate unit that you can style or target on its own.
<table>
<caption>Inventory by warehouse</caption>
<thead>
<tr>
<th scope="col">Product</th>
<th scope="col">Stock</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="rowgroup" colspan="2">Warehouse A</th>
</tr>
<tr>
<td>Widgets</td>
<td>40</td>
</tr>
</tbody>
<tbody>
<tr>
<th scope="rowgroup" colspan="2">Warehouse B</th>
</tr>
<tr>
<td>Widgets</td>
<td>25</td>
</tr>
</tbody>
</table>Here each tbody begins with a heading row that labels the section. The scope="rowgroup" value tells assistive technology that the heading applies to all rows in that tbody.
What the grouping gives you
- Styling hooks. You can write
thead thortfoot tdinstead of adding classes to cells. - Meaning for assistive technology. Screen readers can announce which section a cell belongs to, and can find the header row.
- Printing. When a table spans several printed pages, browsers can repeat the
theadandtfooton each page. - Scripting. Code can sort or filter rows in
tbodywithout touching the heading or total rows.
The next CSS rule shades the footer and sets a stronger border above it. It relies only on the grouping elements.
tfoot th,
tfoot td {
background: #f0f0f0;
font-weight: bold;
border-top: 2px solid #333;
}Common mistakes
- Putting
trand a group directly side by side. If you usetheadortbody, put every row inside a group. Mixing loosetrelements with groups is invalid. - Adding
tbodyyourself and not expecting it. When you write a table with loosetrelements, the HTML parser inserts atbodyfor you. A CSS selector liketable > trwill then fail to match, because the DOM containstable > tbody > tr. - Using
tfootfor page footers. It is for table summaries only. - Using
theadandtdcells. Headings intheadshould normally bethcells.
Exercise
A table written with only tr elements and no tbody is loaded in a browser. A stylesheet uses the selector table > tr { background: #eee; }. Why does the rule not apply, and how do you fix it?
Show answer
The HTML parser inserts a tbody around loose rows, so the DOM has table > tbody > tr and table > tr matches nothing. Fix it by writing table tr or tbody tr, or by adding the tbody explicitly in the markup.
Exercise
Write the minimal markup for a table with one header row containing "Product" and "Price", one data row "Pen" and "2", and a footer row "Total" and "2". Use all three row groups.
Show answer
<table><thead><tr><th scope="col">Product</th><th scope="col">Price</th></tr></thead><tbody><tr><td>Pen</td><td>2</td></tr></tbody><tfoot><tr><th scope="row">Total</th><td>2</td></tr></tfoot></table>