CHAPTER 26 · HTML
Table groups: separate headings, records and totals
A longer table is easier to maintain when its header, main records and summary have clear places. Use thead, tbody and tfoot without expecting them to calculate values or create scrolling by themselves.
Download example files (ZIP)Think of a paper register
Write the parts in a clear order
<table>
<caption>Supplies for the practice room</caption>
<thead>
<tr><th scope="col">Item</th><th scope="col">Quantity</th></tr>
</thead>
<tbody>
<tr><th scope="row">Notebooks</th><td>4</td></tr>
</tbody>
<tfoot>
<tr><th scope="row">Total items</th><td>4</td></tr>
</tfoot>
</table>- 1Thead: the header rows
- 2Tbody: the main records
- 3Tfoot: the written summary
- 4Tr and cells: still needed inside groups
Groups do not calculate totals, create sticky headers or add scrolling by themselves.
Complete program: supply-total.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Practice Room Supply Count</title>
</head>
<body>
<h1>Count the practice room supplies</h1>
<p>These are fictional quantities for a counting exercise.</p>
<table>
<caption>Supplies prepared for the room</caption>
<thead>
<tr><th scope="col">Item</th><th scope="col">Quantity</th></tr>
</thead>
<tbody>
<tr><th scope="row">Notebooks</th><td>4</td></tr>
<tr><th scope="row">Folders</th><td>6</td></tr>
</tbody>
<tfoot>
<tr><th scope="row">Total items</th><td>10</td></tr>
</tfoot>
</table>
</body>
</html>This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.
Why use more than one tbody?
Complete program: grouped-sessions.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Two Days of Practice Sessions</title>
</head>
<body>
<h1>Plan two practice days</h1>
<table>
<caption>Monday and Tuesday room plan</caption>
<thead>
<tr><th scope="col">Session</th><th scope="col">Room</th></tr>
</thead>
<tbody>
<tr><th scope="row">Monday: headings</th><td>Room 6</td></tr>
<tr><th scope="row">Monday: paragraphs</th><td>Room 8</td></tr>
</tbody>
<tbody>
<tr><th scope="row">Tuesday: links</th><td>Room 6</td></tr>
<tr><th scope="row">Tuesday: lists</th><td>Room 8</td></tr>
</tbody>
</table>
</body>
</html>This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.
Header groups and header cells have different jobs
Printing and scrolling: avoid promises
When a footer is useful
Problem and solution: a quantity changes but the summary does not
Common mistakes and useful tips
Confusing thead with the document head. Putting cells directly inside a row group without tr. Assuming every cell inside thead automatically becomes a header. Adding several thead elements instead of grouping body records appropriately. Using tfooter, which is not the correct element name. Expecting totals, sorting, sticky headers or scrolling to appear automatically. Forgetting to recheck a static summary after changing records.
Practice questions
What does each of thead, tbody and tfoot group? Can a table have more than one tbody? Can th scope="row" appear in tbody? Does tfoot calculate a sum? Will thead alone keep a header fixed during scrolling? Add one item with quantity 3 to supply-total.html. Update the total, then explain which rows belong to each group and why.
Check your practice answers
Header rows, main data rows and footer or summary rows respectively. Yes, for meaningful groups of body records. Keep their labels clear in the content. Yes. A row-header cell can label a body row without becoming a header-group row. No. Its contents are written by the author unless additional code calculates them. No. Sticky positioning is a separate styling task. Starting from the original 4 and 6 quantities, the new total is 13. Put the new item row in tbody and update the summary in tfoot. Keep the column labels in thead.
Interview questions
How does thead differ from th?
Is tbody always visible in the saved source?
Is tfoot compulsory?
What should be checked after editing a table with totals?
Quick revision
Row groups contain tr rows, and rows contain cells. Keep header groups, data groups and summaries in a clear order. Multiple tbody groups are allowed; thead and tfoot are each limited to one. Groups do not automatically calculate, sort or create scrolling.