HTML Complex Tables: Header Associations
Associate cells explicitly
When several headers apply to one value, give each relevant th a document-wide unique id. Put the applicable IDs, separated by spaces, in the data cell’s headers attribute. Every token must refer to a header cell in the same table.
<table> <caption>Seats remaining</caption> <tr> <th scope="col">Room</th> <th id="mon" scope="col">Monday</th> <th id="tue" scope="col">Tuesday</th> </tr> <tr> <th id="room-a" scope="row">A</th> <td headers="room-a mon">4</td> <td headers="room-a tue">2</td> </tr></table>The first data cell explicitly names Room A and Monday. Simple scope alone would also suffice here; this example introduces the mechanism.
Use column groups for grouped headings
A colgroup defines a set of columns before the heading and body rows. A header with scope="colgroup" labels a column group. In this example each quarter has two subcolumns, planned and actual. The row header labels the branch.
<table> <caption>Orders by quarter</caption> <colgroup span="1"></colgroup> <colgroup span="2"></colgroup> <colgroup span="2"></colgroup> <thead> <tr> <th rowspan="2" scope="col">Branch</th> <th colspan="2" scope="colgroup">Q1</th> <th colspan="2" scope="colgroup">Q2</th> </tr> <tr> <th scope="col">Plan</th> <th scope="col">Actual</th> <th scope="col">Plan</th> <th scope="col">Actual</th> </tr> </thead> <tbody> <tr> <th scope="row">North</th> <td>10</td> <td>9</td> <td>12</td> <td>11</td> </tr> </tbody></table>The grid has five columns. Q1 and Q2 each label two columns.
Name every relevant level
For a multi-level header layout, explicit IDs can remove ambiguity. List all applicable headers for each data cell rather than assuming that one ID covers all other levels. For row groups, scope="rowgroup" is available on a header anchored in a row group; use explicit associations if the relationships remain unclear.
<table> <caption>Quarterly sales (EUR)</caption> <thead> <tr> <th rowspan="2" scope="col">Branch</th> <th id="q1" colspan="2">Q1</th> </tr> <tr> <th id="plan" scope="col">Plan</th> <th id="actual" scope="col">Actual</th> </tr> </thead> <tbody> <tr> <th id="west" scope="row">West</th> <td headers="west q1 plan">30</td> <td headers="west q1 actual">28</td> </tr> </tbody></table>The value 28 is explicitly associated with West, Q1 and Actual.
Common mistakes and practice
Prefer separate simple tables if they preserve the meaning. Never reference missing IDs, duplicate IDs, or td cells in headers. Test keyboard access and screen-reader navigation before release; valid DOM structure alone does not prove assistive-technology usability.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Complex Tables: Header Associations — Knowledge Check
Apply table structure and accessibility concepts from this lesson.
What may a headers token reference?
How are multiple header IDs separated in headers?
When is splitting a complex table into simpler tables a good option?
A data cell uses headers="west actual". What should you verify?
Why use explicit header IDs in a complex table?