Sub topic 10
Multi-level headers with id and headers
This chapter assumes you can write basic tables with table, tr, td, and th. It also assumes you know colspan, rowspan, and the scope attribute. Here you will learn how to connect data cells to headers explicitly when scope is not enough.
Why scope sometimes falls short
The scope attribute tells assistive technology which direction a header covers: col, row, colgroup, or rowgroup. For simple grids this is all you need. A screen reader reads the header cell for each data cell as the user moves through the table.
Problems appear when headers sit on several levels. Consider sales figures grouped by year, with quarters under each year. The cell for North in Q1 of 2025 depends on three headers: the region, the year, and the quarter. Complex layouts with irregular spans can make scope ambiguous, and some assistive technology handles them poorly.
The id and headers attributes solve this by stating the mapping directly. Each header cell gets a unique id. Each data cell gets a headers attribute that lists the ids of every header that applies to it.
How id and headers work together
idon athgives that header a unique name in the page.headerson atd(orth) holds a space-separated list of those ids.- The order of ids in the list is the order in which a screen reader may announce them, so list them from the most general to the most specific.
The mapping is explicit, so it does not depend on the browser guessing from the grid layout.
A worked example
This table has a two-level column header. The year 2025 spans two quarter columns, and each region has its own row header.
<table>
<caption>Sales by region</caption>
<thead>
<tr>
<td rowspan="2"></td>
<th id="y25" colspan="2" scope="colgroup">2025</th>
</tr>
<tr>
<th id="q1" headers="y25" scope="col">Q1</th>
<th id="q2" headers="y25" scope="col">Q2</th>
</tr>
</thead>
<tbody>
<tr>
<th id="north" scope="row">North</th>
<td headers="north y25 q1">120</td>
<td headers="north y25 q2">135</td>
</tr>
</tbody>
</table>Follow the structure step by step:
- The top-left cell is an empty
tdwithrowspan="2". It covers both header rows, so the grid stays aligned. - The
2025header spans two columns withcolspan="2"and has the idy25. - The
Q1andQ2headers each have their own id. Theirheaders="y25"attribute says they sit under the year. - The
Northrow header has the idnorth. - Each data cell lists the row header, the year, and the quarter that apply to it.
When a screen reader lands on the value 135, it can announce "North, 2025, Q2, 135". The information comes from the attributes you wrote.
Rules and common errors
- Every id in a
headerslist must exist in the same table. A misspelled id is silently ignored, and the reader loses that header. - Ids must be unique across the whole page, not only within one table. If you put two tables on a page, prefix ids, for example
sales-y25. - Only reference header cells. Pointing at a
tdis an error. - Separate ids with spaces, not commas.
- Do not skip a level. If a cell lists
northandq1but omitsy25, the year is not announced.
When to choose each approach
| Situation | Best choice |
|---|---|
| One header row, one header column | `scope="col"` and `scope="row"` |
| Header groups with simple spans | `scope="colgroup"` or `scope="rowgroup"` |
| Two or more header levels over the same cells | `id` and `headers` |
| Header cells that do not line up in a regular grid | `id` and `headers` |
If a table needs a lot of headers attributes, consider whether it can be split into two simpler tables. Simpler tables are easier for every reader, not only screen reader users.
Exercise
A data cell sits under the column header "Q2", which is inside the column group "2026", and in the row headed "South". The header ids are q2, y26, and south. Write the headers attribute for this cell and explain what happens if you write headers="south q2".
Show answer
Write headers="south y26 q2". With headers="south q2", the year 2026 is left out, so a screen reader announces the region and quarter but not the year. The cell is still valid HTML, but the reader loses information about the top header level.
Exercise
Why must the ids used in a headers attribute be unique across the whole page, and what is one way to avoid clashes between two tables?
Show answer
The browser resolves each id against the whole document, so a duplicate id can point a cell at the wrong header. Prefix ids with a table name, such as sales-y25 and costs-y25.