Sub topic 10
Multi-level headers with id and headers
This chapter covers tables whose headers sit on more than one level. It shows how the id and headers attributes tie each data cell to every header that applies to it. It assumes you know basic HTML document structure, how to write elements and attributes, and the table elements from earlier chapters: table, tr, td, th, thead, tbody, colspan, rowspan, and scope.
Why scope is sometimes not enough
In a simple table, one header describes each column and one header describes each row. The scope attribute handles that case. A screen reader walks from a data cell up its column and left along its row, and it announces the headers it finds.
A table with grouped headers is harder. Take a sales table where the year 2025 sits above two quarters, Q1 and Q2. The cell with the value 120 belongs to three headers: the row header North, the column header Q1, and the group header 2025. The scope attribute can mark 2025 as a header for a group of columns. Browsers and screen readers often resolve that relationship well, but support varies. Some combinations of browser and assistive technology skip the outer level or read headers in a confusing order.
The headers attribute removes the guesswork. You list the exact headers for each data cell, and the browser exposes that list to assistive technology as stated.
The two attributes
Two attributes work together.
idgoes on a header cell. It gives that cell a name that is unique in the whole document.headersgoes on a data cell, or on a header cell that itself needs a parent. Its value is a space-separated list ofidvalues. Each value names one header cell that applies.
The list is a set of references, not a path. Order does not change which headers apply, though listing them from the broadest to the narrowest is a good habit, because many screen readers announce them in the order given.
A worked example
This is the table from the topic's examples. It has a year group that spans two quarter columns, and a row header for each region.
<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>Walk through the markup in four steps.
- The top-left corner is an empty
tdwithrowspan="2". It is atd, not ath, because it has no content and describes nothing. It covers the corner across both header rows. - The cell
id="y25"spans two columns withcolspan="2". Itsscope="colgroup"says it is a header for a group of columns. - The cells
q1andq2each carryheaders="y25". They declare that 2025 is their parent header. - Each data cell lists three ids: its row header, the year, and its quarter. A screen reader can read the first value as "North, 2025, Q1, 120".
The grid has three columns: one for the row headers and two for the quarters. The corner cell takes the first column. The year cell takes the other two. Every row therefore covers three columns.
Rules the browser checks
The headers mechanism has strict rules. Breaking one makes the association fail silently, because browsers do not show an error.
- Every
idmust be unique in the document. A duplicate means one reference points at the wrong cell, or at nothing. - Every token in
headersmust match anidof a header cell in the same table. A typo gives no warning. - The
headersattribute is case-sensitive in its values, because ids are.Northandnorthare different. - Referenced cells must be header cells (
th) in the same table. Pointing at an ordinarytdor at a cell in another table does not work. - Separate tokens with spaces only. Commas make a single, invalid token.
A common cause of problems is copy and paste. If you copy a table and keep its ids, the page now has two cells with id="q1". Prefix ids per table, for example sales-q1 and costs-q1, when a page has more than one table with similar headers.
Adding a second row group
Real tables have more rows. Here is a larger version with two regions and a second year, so the pattern repeats.
<table>
<caption>Sales by region and year</caption>
<thead>
<tr>
<td rowspan="2"></td>
<th id="y24" colspan="2" scope="colgroup">2024</th>
<th id="y25" colspan="2" scope="colgroup">2025</th>
</tr>
<tr>
<th id="q1-24" headers="y24" scope="col">Q1</th>
<th id="q2-24" headers="y24" scope="col">Q2</th>
<th id="q1-25" headers="y25" scope="col">Q1</th>
<th id="q2-25" headers="y25" scope="col">Q2</th>
</tr>
</thead>
<tbody>
<tr>
<th id="north" scope="row">North</th>
<td headers="north y24 q1-24">110</td>
<td headers="north y24 q2-24">118</td>
<td headers="north y25 q1-25">120</td>
<td headers="north y25 q2-25">135</td>
</tr>
<tr>
<th id="south" scope="row">South</th>
<td headers="south y24 q1-24">95</td>
<td headers="south y24 q2-24">99</td>
<td headers="south y25 q1-25">101</td>
<td headers="south y25 q2-25">104</td>
</tr>
</tbody>
</table>Notice the ids. The quarter labels repeat (Q1 appears twice), so the ids carry the year: q1-24 and q1-25. This is the main reason ids are required here. Two header cells with the same visible text still need different ids.
Each data cell needs its own headers list. With two regions and four quarters, that is eight lists. The cost grows with the table, which is one reason to use the technique only when it is needed.
Row headers with two levels
Headers can also nest in the row direction. Suppose rows are grouped by region and then by product.
<table>
<caption>Units sold by region and product</caption>
<thead>
<tr>
<td colspan="2"></td>
<th id="units" scope="col">Units</th>
</tr>
</thead>
<tbody>
<tr>
<th id="north" rowspan="2" scope="rowgroup">North</th>
<th id="n-pens" headers="north" scope="row">Pens</th>
<td headers="north n-pens units">400</td>
</tr>
<tr>
<th id="n-ink" headers="north" scope="row">Ink</th>
<td headers="north n-ink units">150</td>
</tr>
</tbody>
</table>The region header spans two rows with rowspan="2" and uses scope="rowgroup". Each product header points to the region through headers="north". The data cell then lists region, product, and the column header. The same ideas apply as in the column case, rotated.
Testing the result
You cannot see an association by looking at the page, so check it another way.
- Open the browser's accessibility tree in its developer tools. Select a data cell and read the computed name or description, if the browser shows one.
- Run an automated checker, such as the axe extension or the Nu HTML Checker. They report
headersvalues that match noidand ids that repeat. - Use a screen reader on one or two cells. Move through the table with its table navigation keys and listen to what it announces.
The first two are fast and catch most typing mistakes. The third catches the case where the markup is valid but the announced order is confusing.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The screen reader announces only the column header | `headers` is missing on the data cell, or the token does not match an id | Check each token against the `id` values character by character |
| The wrong year is announced | Two header cells share an id, such as `q1` in both years | Make ids unique, for example `q1-24` and `q1-25` |
| The validator reports "no cell with that id" | The `id` is on a `td`, or on a cell outside the table | Put the `id` on a `th` inside the same table |
| Headers are ignored entirely | Tokens are separated by commas | Use spaces |
| Table is wrong after adding a column | New data cells were added without a `headers` list | Update every row, and update the `colspan` on group headers |
When to simplify instead
Before using headers, ask whether the table can be restructured. Two simpler options exist.
- Split one table into several. A table with a year group and separate region groups may read better as two small tables, each with a single header level and a clear caption.
- Repeat the context in the header text. A header such as "Q1 2025" needs no group header above it, and
scope="col"is enough.
Both options are easier to build, easier to maintain, and less likely to be misread. Use id and headers when the table is genuinely two-dimensional in its headers and you cannot reduce it.
Summary of the technique
- Give each header cell that others depend on a unique
id. - On every data cell, write a
headerslist that names every applicable header. - On lower-level headers, point to the parent through
headers. - Keep
scopeon header cells as well. The two attributes do not conflict. - Check for duplicate ids, wrong tokens, and tokens separated by anything other than spaces.
Exercise
In the 2024/2025 sales table in this chapter, the South region's Q2 value for 2025 is 104. Write the opening tag of that data cell, including its headers attribute. Then explain what happens if you accidentally give both Q1 header cells the same id q1.
Show answer
The tag is <td headers="south y25 q2-25">104</td>. If both Q1 cells use id="q1", the id is no longer unique. Browsers resolve a duplicate id to the first matching element, so every cell that refers to q1 is associated with the first Q1 header, 2024. A data cell for 2025 Q1 would be announced as 2024 Q1 or paired with the wrong year, and validators report a duplicate id. The fix is to give each cell a distinct id, such as q1-24 and q1-25.
Exercise
A table has one header row (Name, Email, Phone) and one row per person. Does it need id and headers? What should you use instead?
Show answer
No. It has a single header level in each direction, so scope="col" on the three header cells is enough, and scope="row" if the name cells act as row headers. The id and headers attributes add maintenance cost and give no benefit here.