Sub topic 9
Tables for tabular data
This chapter assumes you know basic HTML syntax, can open your browser's developer tools, and understand the idea of the accessibility tree from earlier chapters. It covers when to use a table, how to mark one up, and the errors that break its meaning.
When a table is the right element
A table is for data that has relationships along two axes, such as rows of months against columns of visitor counts. Each data cell takes its meaning from the row and column it sits in.
Do not use a table to position content on the page. Layout tables make screen readers announce row and column counts that mean nothing. Use CSS grid or flexbox for layout instead.
Anatomy of an accessible table
The example below uses the elements that give a table its structure.
<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>The elements work as follows:
captiongives the table a visible title. Assistive technology announces it when a user reaches the table.theadandtbodyseparate the header rows from the data rows. Atfootcan hold totals.this a header cell.tdis a data cell.scope="col"says a header applies to the cells below it.scope="row"says it applies to the cells to its right.
Here, the cell "1450" is associated with the headers "February" and "Visitors". A screen reader can announce both when the user lands on it. A plain td styled to look bold gives no such association.
How the pieces map to the accessibility tree
The following diagram shows how the table elements relate to each other.
Browsers expose these elements with the roles table, row, columnheader, rowheader and cell. In Chrome DevTools, open the Accessibility pane and select a td to see the headers it is linked to.
Common errors
| Mistake | Problem | Fix |
|---|---|---|
| Using a table for page layout | Screen readers announce meaningless structure | Use CSS grid or flexbox |
| Using `td` with bold styling as a header | No header association | Use `th` with `scope` |
| Putting the title in a `p` above the table | The title is not tied to the table | Use `caption` |
| Using `<br>` or empty cells to fake spacing | Adds blank cells to the reading order | Use CSS padding and margin |
| Splitting one dataset across several tables | Users lose the shared headers | Keep one table, or repeat headers |
Complex tables
Merged cells use colspan and rowspan. Keep them rare, because screen readers navigate irregular grids less reliably. If a table has several levels of headers, consider splitting it into smaller tables, each with its own caption.
Exercise
A developer marks up a price list with a div for each row and applies display: table in CSS so it looks like a table. The first row is bold text in div elements. What is wrong with this, and what should replace it?
Show answer
CSS changes appearance but does not add meaning, so assistive technology sees only generic div elements with no rows, columns or headers. Replace it with a table that has a caption, a thead with th scope="col" cells, and a tbody of rows with td cells for prices.
Exercise
In the Monthly visitors table, which headers does a screen reader associate with the cell containing 1200?
Show answer
"January" (the row header) and "Visitors" (the column header).