Larger tables benefit from structure. HTML lets you divide a table into a header, body and footer, which helps styling, scripting, printing and assistive technology. This lesson also covers how to keep tables usable and when to avoid them entirely.
thead, tbody and tfoot#
<thead>contains the header rows.<tbody>contains the main data rows.<tfoot>contains summary rows, such as totals.
<table>
<caption>Brew and Byte sales this week</caption>
<thead>
<tr>
<th scope="col">Item</th>
<th scope="col">Cups sold</th>
<th scope="col">Income (Rs.)</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Espresso</th>
<td>120</td>
<td>18,000</td>
</tr>
<tr>
<th scope="row">Latte</th>
<td>95</td>
<td>20,900</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row">Total</th>
<td>215</td>
<td>38,900</td>
</tr>
</tfoot>
</table>Browsers add <tbody> automatically if you leave it out, but writing it explicitly is clearer. In modern HTML, <tfoot> may be placed after <tbody>, as shown, and you may have several <tbody> elements to group related rows.
What the sections give you#
- Styling: target
thead thortfoot tdseparately, or stripe only the body rows. - Printing: browsers can repeat the header on each printed page of a long table.
- Scrolling: you can make the body scroll while keeping the header visible, using CSS.
- Meaning: software can tell header, data and summary rows apart.
Accessibility checklist#
- Give every table a
caption. - Use
thfor all headers, withscope. - Keep tables simple, avoiding spans where possible.
- Do not use empty cells for spacing.
- Make sure text has enough colour contrast, including stripes.
Small screens#
Wide tables overflow narrow phone screens. A common fix is to wrap the table in a scrollable container with a label so keyboard users can reach it:
<div class="table-scroll" tabindex="0" role="region" aria-label="Weekly sales table">
<table>
<!-- ...table content... -->
</table>
</div>With CSS overflow-x: auto on that wrapper, visitors can scroll sideways while the page itself stays put. Also consider whether you need so many columns at all.
When not to use a table#
A table is for data with relationships between rows and columns. Avoid it when:
- You only want to position things side by side. Use CSS Flexbox or Grid.
- The content is a single list of items. Use
ulorol. - The content is a set of label and value pairs. Use a description list.
- You need a page layout with columns, headers and sidebars. Use semantic layout elements.
Common mistakes#
- Using
<tfoot>for content that is not a summary. - Leaving out the
caption. - Using tables for page layout.
- Applying
display: blockto table elements in CSS, which can strip semantics in some browsers. - Ignoring horizontal overflow on phones.
Try it yourself#
Extend your drinks price table with thead, tbody and tfoot. Add a footer row showing the average price. Wrap the table in a scrollable region and test it by narrowing your browser window to phone width. Finally, write down one example from your own projects where you used to reach for a table, but a list or CSS layout would be a better fit.
Recap#
Structure large tables with thead, tbody and tfoot, add captions and scoped headers, and make wide tables scroll safely. Use tables for real data only, and use lists, description lists or CSS for everything else.
Check your understanding
Lesson quiz
OptionalA short quiz to check what you just learned.
- Questions
- 4
- Time limit
- 5 min
- Pass mark
- 70%
- Best score
- -