Sub topic 9
Tables for tabular data
This chapter assumes you know basic HTML syntax (tags, attributes, nesting) and can open your browser's developer tools. CSS knowledge helps but is not required. You should also have read the chapters on the accessibility tree and on generic containers, because tables are a case where the right element gives you a lot of free behavior.
What a table is for
A table presents data that has two dimensions: each value is meaningful because of the row it sits in and the column it sits under. A schedule, a price list and a comparison of products are tables. A list of links, a row of buttons and a page layout are not.
Before 2010, many sites used <table> to position things on the page. That is a misuse. A screen reader announces a layout table as data ("table with 3 rows and 2 columns"), then reads cells in an order that may not match the visual order. Use CSS Grid or Flexbox for layout, and keep <table> for data.
A quick test: if you could write the content as a spreadsheet and each cell would make sense with its row and column labels, use a table.
The core elements
The following elements make up a well-formed data table.
| Element | Purpose |
|---|---|
| `table` | Wraps the whole table. |
| `caption` | Gives the table a visible title. It must be the first child of `table`. |
| `thead` | Groups the header rows. |
| `tbody` | Groups the body rows. A table can have several. |
| `tfoot` | Groups summary rows, such as totals. |
| `tr` | One row. |
| `th` | A header cell. |
| `td` | A data cell. |
Here is a complete table from the topic's examples.
<table>
<caption>Monthly visitors</caption>
<thead>
<tr>
<th scope="col">Month</th>
<th scope="col">Visitors</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">January</th>
<td>1200</td>
</tr>
<tr>
<th scope="row">February</th>
<td>1450</td>
</tr>
</tbody>
</table>Three details matter here:
- The
<caption>names the table. A screen reader announces it when the user reaches the table, and sighted users see it too. - The first column holds
<th scope="row">cells because each month labels its row. Only the cells that label something should beth. theadandtbodydo not change the default appearance much, but they let you style header and body separately. They also let browsers repeat the header on each printed page.
Header cells and the scope attribute
A th tells the browser that the cell describes other cells. The scope attribute says which ones:
scope="col"means the cell heads the column below it.scope="row"means the cell heads the row to its right.colgroupandrowgroupapply to groups of columns or rows, and are rarely needed.
Browsers can often guess the direction for simple tables, but scope removes the guesswork. When a screen reader user moves into the "1450" cell, the reader can announce "February, Visitors, 1450". That announcement is possible only because the header associations exist in the markup.
Using td with bold styling for headers breaks this. The text looks like a header, but nothing in the accessibility tree says so.
<!-- Avoid: looks like a header, is not one -->
<tr>
<td><b>Month</b></td>
<td><b>Visitors</b></td>
</tr>
<!-- Prefer: a real header cell -->
<tr>
<th scope="col">Month</th>
<th scope="col">Visitors</th>
</tr>Spanning cells
Use colspan and rowspan when a cell covers more than one column or row. A common case is a grouped header.
<table>
<caption>Quarterly sales by region</caption>
<thead>
<tr>
<td rowspan="2"></td>
<th scope="colgroup" colspan="2">2025</th>
<th scope="colgroup" colspan="2">2026</th>
</tr>
<tr>
<th scope="col">Q1</th>
<th scope="col">Q2</th>
<th scope="col">Q1</th>
<th scope="col">Q2</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">North</th>
<td>120</td>
<td>135</td>
<td>150</td>
<td>160</td>
</tr>
<tr>
<th scope="row">South</th>
<td>90</td>
<td>95</td>
<td>110</td>
<td>118</td>
</tr>
</tbody>
</table>The empty top-left cell is a td, because it heads nothing. The year cells use scope="colgroup" because each one covers a group of columns.
Spans are easy to get wrong. If the cell counts in a row do not add up to the same total as the other rows, the browser still renders the table, but the grid is misaligned and header associations may be wrong. Count the columns in every row, including the ones covered by spans from rows above.
For tables with more than two levels of headers, or with irregular structure, give each header an id and list the relevant ones in each data cell's headers attribute.
<table>
<caption>Opening hours</caption>
<thead>
<tr>
<td></td>
<th id="open" scope="col">Opens</th>
<th id="close" scope="col">Closes</th>
</tr>
</thead>
<tbody>
<tr>
<th id="weekday" scope="row">Weekdays</th>
<td headers="weekday open">09:00</td>
<td headers="weekday close">17:00</td>
</tr>
</tbody>
</table>Treat headers as a last resort. If you can split a complex table into two simple ones, do that instead. Simple tables are easier to read, to maintain and to navigate with assistive technology.
Totals with tfoot
Put summary rows in tfoot. In the current HTML specification, tfoot may appear after tbody in the source, which matches the visual order.
<table>
<caption>Expenses, March</caption>
<thead>
<tr>
<th scope="col">Item</th>
<th scope="col">Cost (USD)</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Hosting</th>
<td>40</td>
</tr>
<tr>
<th scope="row">Domain</th>
<td>12</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row">Total</th>
<td>52</td>
</tr>
</tfoot>
</table>How a table reaches the accessibility tree
The browser maps table to the table role, tr to row, th to columnheader or rowheader, and td to cell. The caption becomes the table's accessible name. Screen readers use this information to offer table navigation commands: jump to the next table, move one cell down, or hear the headers when the cell changes.
You can see this in Chrome DevTools. Open the Elements panel, select the table, and open the Accessibility tab. Check that the role is table, the name matches your caption, and a data cell lists its header relationships.
Two CSS choices can break this mapping:
- Setting
display: blockordisplay: flexon table elements may cause some browsers to drop the table semantics. If you need a different layout at narrow widths, test with a screen reader, or restore roles with ARIA. - Using
display: tableon adivgives a visual table with no table semantics. Appearance does not create meaning.
Accessible names and descriptions
Every data table should have a name. Prefer caption because it is visible and requires no ARIA. If a visible title already exists elsewhere, such as a heading, you can point to it with aria-labelledby.
<h2 id="visitors-title">Monthly visitors</h2>
<table aria-labelledby="visitors-title">
<thead>
<tr>
<th scope="col">Month</th>
<th scope="col">Visitors</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">January</th>
<td>1200</td>
</tr>
</tbody>
</table>Do not use the old summary attribute. It is obsolete. If you need to explain how to read a complex table, write a paragraph before it.
Responsive tables
Wide tables overflow small screens. The safest fix keeps the table intact and lets the user scroll inside a wrapper.
<div class="table-wrap" tabindex="0" role="region" aria-labelledby="wide-title">
<h2 id="wide-title">Annual figures</h2>
<table>
<caption>All regions, 2020 to 2025</caption>
<thead>
<tr>
<th scope="col">Region</th>
<th scope="col">2020</th>
<th scope="col">2025</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">North</th>
<td>80</td>
<td>150</td>
</tr>
</tbody>
</table>
</div>Pair it with CSS such as .table-wrap { overflow-x: auto; }. The tabindex="0" makes the scrollable area reachable by keyboard, and the region role with a name makes it a landmark that screen readers can announce. Use this pattern only when the area actually scrolls, because every extra landmark adds noise.
Common mistakes
| Mistake | Why it hurts | Fix |
|---|---|---|
| Table used for page layout | Screen readers announce data that is not there | Use CSS Grid or Flexbox |
| Header styled with `td` and bold text | No header associations | Use `th` with `scope` |
| Missing `caption` | The table has no name | Add `caption` or `aria-labelledby` |
| Empty cells used for spacing | Readers announce blank cells | Use CSS padding and margins |
| Row counts that do not match because of spans | Misaligned grid | Count every row, validate |
| Splitting one table into several for styling | Headers are lost | Keep one table, use `tbody` groups |
Process for deciding
- Ask whether the content has meaningful row and column relationships. If not, use a list or a grouping element.
- Identify which cells label others. Make those
th, and setscope. - Add a
caption. - Group rows with
thead,tbodyandtfoot. - Use spans only when the data needs them. Check the result in the accessibility tree and with a validator.
Exercise
A developer builds a price list with div elements styled with display: grid and bold text for the first row. It looks like a table. Name two things that are missing for assistive technology, and give the elements you would use to fix them.
Show answer
First, the grid has no table role, so screen readers cannot offer table navigation or announce row and column counts. Use table, tr and td. Second, the bold first row is not a header, so no cell has a header association. Use th scope="col" for the first row, and th scope="row" for any cell that names its row. Also add a caption so the table has an accessible name.
Exercise
In a table with colspan="2" on a year heading, one body row has only three cells while the other rows have four. What happens, and how do you find the error?
Show answer
The browser still renders the table, but the short row leaves a gap or shifts values under the wrong headers. Count the columns in every row, including cells covered by spans from rows above. An HTML validator reports tables with rows of unequal width, and the Accessibility tab in devtools shows which headers each cell is associated with.