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

A class register has labels at the top, student records in the middle and perhaps a count at the end. These are different parts of one table, not three unrelated tables.

HTML provides row groups for that structure. A row group contains related tr rows. The cells still live inside those rows; the group does not replace tr, th or td.

ElementPurposeFamiliar example
theadGroup the table's header rowsItem and Quantity labels
tbodyGroup the main data rowsOne row per supply
tfootGroup summary or footer rowsTotal quantity

Do not confuse thead with the document's head. Head contains document information outside body. Thead belongs inside a table in body and contains visible rows. Similarly, tfoot is a table part, not the page's footer.

Write the parts in a clear order

For these lessons, use caption, optional column groups, thead, one or more tbody elements and then optional tfoot. A table can have at most one thead and one tfoot, but multiple tbody groups.

HTML fragment — part of a document
<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>

Each group is a direct child of table. Each tr is inside its appropriate group. Do not put a td directly inside tbody or nest one tbody inside another. When you choose explicit tbody groups, keep all the ordinary body rows inside them rather than mixing grouped and ungrouped rows.

One table, several jobs
  1. 1Thead: the header rows
  2. 2Tbody: the main records
  3. 3Tfoot: the written summary
  4. 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

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>
Browser previewDocument title: Practice Room Supply Count

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.

Expected result: one two-column table with labels, two item rows and a total row. There may be no borders until CSS is added. The number 10 was written by the author because 4 + 6 = 10. HTML does not perform that calculation.

Change Folders from 6 to 7, save and refresh. The total remains 10 until you also edit it. That is a useful demonstration of a static value: a value supplied in the saved page rather than calculated automatically during use. Restore a correct total of 11 if you keep the change.

Why use more than one tbody?

Separate body groups can represent related batches of records, such as sessions grouped by day. They help organise the source and provide a way to apply styles to groups later. They do not automatically print a group title or add a gap.

Keep useful labels in the cells. A reader cannot infer Monday from an invisible grouping boundary. In this example, each row's session label includes its day, so the meaning remains clear in a plain browser view.

Complete program: grouped-sessions.html

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>
Browser previewDocument title: Two Days of Practice Sessions

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.

Expected result: four sessions appear below one header row. The source contains two tbody groups, each holding one day's records. The browser may make them look like one continuous table, which is fine: grouping has structural meaning even without a special visual effect.

Header groups and header cells have different jobs

Thead groups header rows. Th identifies a particular header cell and scope explains which cells it labels. Putting ordinary td cells inside thead does not turn them into proper th headers.

Likewise, a row header can appear inside tbody. In supply-total.html, Notebooks labels a body row through th scope="row". It does not belong in thead simply because it is a header cell. Decide whether you mean a group of rows or a cell's relationship to data.

Printing and scrolling: avoid promises

Some browsers can repeat table header or footer groups when printing a long table. The result depends on print styling, browser support and pagination. Check Print Preview before promising repeated labels on every sheet.

Thead does not automatically make a header sticky while the page scrolls. Tbody does not automatically create an independently scrolling panel. Those are separate layout behaviours that need appropriate CSS and testing. First make the table's structure correct.

When a footer is useful

Use tfoot for actual table summaries or related footer rows. It is optional. Do not add an empty tfoot because every example is assumed to need all three groups. A room timetable with no summary may need only thead and tbody.

For totals, include what is being counted: Total items is clearer than Total when the table might contain both item types and quantities. If units differ, do not add them as if they were the same thing. Five notebooks plus two hours is not a meaningful single quantity.

Problem and solution: a quantity changes but the summary does not

Problem: the visible total disagrees with the item rows after an edit.

Solution: recompute the intended total, update the saved summary and check it again. A table footer is a place for summary content, not a spreadsheet formula. Dynamic totals require additional programming, which this HTML example does not include.

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

  1. What does each of thead, tbody and tfoot group?
  2. Can a table have more than one tbody?
  3. Can th scope="row" appear in tbody?
  4. Does tfoot calculate a sum?
  5. Will thead alone keep a header fixed during scrolling?
  6. 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
  1. Header rows, main data rows and footer or summary rows respectively.
  2. Yes, for meaningful groups of body records. Keep their labels clear in the content.
  3. Yes. A row-header cell can label a body row without becoming a header-group row.
  4. No. Its contents are written by the author unless additional code calculates them.
  5. No. Sticky positioning is a separate styling task.
  6. 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?

Thead groups rows in the header section. Th identifies a header cell. They operate at different levels and neither replaces the other's job.

Is tbody always visible in the saved source?

No. Browsers can insert a tbody in the parsed document when source rows are written directly inside table. Writing explicit groups makes the intended structure easier to maintain.

Is tfoot compulsory?

No. Use it when there are summary or footer rows that belong to the table. Do not create meaningless empty groups merely to follow a visual template.

What should be checked after editing a table with totals?

Check each row's column alignment, the values and units, and all manually written summaries. A browser displaying the table does not prove that the arithmetic is correct.

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.