thead, tbody, and tfoot Explained
A basic HTML table only needs table, tr, and td, and plenty of tables on the web get by with nothing else but once a table grows past a few rows, the three grouping elements thead, tbody, and tfoot start to earn their place.
They divide a table into a header, a body, and a footer, which makes it easier to style, easier to script, and easier for assistive technology to understand.
This post explains what thead, tbody, and tfoot do and how to use them well.
What they are
These three elements are row groups, each one wraps a set of rows and tells the browser what role those rows play.
The thead holds the header rows, which label the columns.
The tbody holds the main data.
The tfoot holds summary rows such as totals or notes.
None of them changes how the table looks on its own, since a table with and without them renders identically by default, their value is in meaning and in the hooks they give you.
<table>
<caption>Monthly sales by region</caption>
<thead>
<tr>
<th scope="col">Region</th>
<th scope="col">October</th>
<th scope="col">November</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">North</th>
<td>1,200</td>
<td>1,450</td>
</tr>
<tr>
<th scope="row">South</th>
<td>980</td>
<td>1,100</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row">Total</th>
<td>2,180</td>
<td>2,550</td>
</tr>
</tfoot>
</table>
The thead element
The thead element contains the row or rows that label the columns, inside it you normally use th cells in place of td, since a th marks a cell as a heading, a table can have only one thead, and it should come before the body rows.
Wrapping your header in thead lets you style it separately, for example with a different background color, and it lets browsers treat it specially, when a long table is printed across several pages, many browsers repeat the header at the top of each page, which makes the printout much easier to read.
The tbody element
The tbody element holds the actual data rows, in many tables you will have one, but you can use several. Multiple tbody elements are a good way to group related rows, such as one group per category or per month, and you can style or script each group on its own.
There is also a quirk worth knowing. If you write tr elements directly inside a table without a tbody, the browser quietly inserts one for you when it builds the page, you will see it in the developer tools even though it is not in your source, that matters when you write CSS selectors or JavaScript, since a rule like table > tr will not match the rows, because the real parent is the invisible tbody.
The tfoot element
The tfoot element holds summary rows,Totals, averages, and footnotes are the usual contents.
A table can have only one tfoot, in older HTML versions it had to come before the body, which looked strange in the source.
In HTML5 you can place it after the tbody, which reads more naturally and is what most people do. The browser still shows it at the bottom of the table either way.
Why bother: styling and structure
The practical payoff starts with CSS, with row groups in place, you can target each part of the table cleanly without extra class names.
table {
border-collapse: collapse;
width: 100%;
}
thead th {
background: #1f2937;
color: #ffffff;
text-align: left;
}
tbody tr:nth-child(even) {
background: #f3f4f6;
}
tfoot td,
tfoot th {
font-weight: 700;
border-top: 2px solid #1f2937;
}
The second rule is a common pattern called zebra striping because the stripes are applied to rows inside tbody, the header and footer are left alone, without the grouping you would have to work around them.
Sticky headers
Another useful trick is keeping the header visible while a long table scrolls with a header grouped in thead, modern CSS makes this short work:
thead th {
position: sticky;
top: 0;
background: #1f2937;
}
The table needs to sit inside a container that scrolls, or the page itself needs to scroll, for this to have any effect.
You will also want a solid background on the cells, or the rows scrolling underneath will show through.
Accessibility
Row groups help assistive technology see the structure of a table, but they do not do all the work, the part that tells a screen reader which header belongs to which cell is the th element, along with the scope attribute.
Use scope="col" on column headers and scope="row" on row headers, as in the example above, and add a caption to give the table a title, for simple tables this is usually enough.
Very complex tables, with headers that span several levels, may need extra markup such as headers and id attributes, and are often better split into several smaller tables.
Working with the groups in JavaScript
The grouping also makes scripts simpler, Tables expose their groups as properties, and you can select only the data rows without picking up the header or footer.
This snippet adds up the second column of the body and writes the result into the footer:
const table = document.querySelector("table");
const rows = table.tBodies[0].rows;
let total = 0;
for (const row of rows) {
const value = Number(row.cells[1].textContent.replace(/,/g, ""));
total += value;
}
table.tFoot.rows[0].cells[1].textContent = total.toLocaleString();
The properties tHead, tBodies, and tFoot are part of the standard table interface in the DOM, so this works in every current browser.
Common mistakes to avoid
One frequent mistake is leaving out the grouping elements entirely and then fighting to style the header row with extra classes.
Another is using td instead of th in the header, which looks fine but gives screen readers nothing to go on.
People also put data rows inside thead or tfoot because of the visual effect they want, when those elements are meant for headers and summaries.
Another trap is trying to make the tbody scroll on its own by changing its display type to block. That breaks the table’s column alignment and can hurt accessibility, so a scrolling wrapper around the whole table is the safer route.
And finally, tables are for data. Do not use them for page layout, since CSS grid and flexbox are far better tools.
Why this matters for developers
Well-structured tables are easier to maintain, easier to restyle, and easier to turn into something interactive, such as a sortable or filterable list, many table libraries and plugins, including those you will run into in WordPress, expect a proper thead and tbody, and may misbehave without them.
If you generate tables from data in a template or a script, build the groups in from the start so that sorting, pagination, and accessibility tools have a clear structure to work with.
A quick way to practice is to take a table from one of your own pages, add the three groups, give it a caption and scope attributes, and then try the sticky header and zebra stripes from this guide.
Opening the result in your browser’s developer tools will also show you how the table is built behind the scenes.
Discover more from Developer Hint
Subscribe to get the latest posts sent to your email.
