Tables
Fundamentals Chapter 6 introduced scope, caption, and basic spanning. This chapter goes further — building a genuinely complex, multi-level header table, and covering the accessibility attributes that matter once a table's structure is no longer simple rows and columns.
A Complex Header Structure with colspan/rowspan
| Product | 2026 Sales | |
|---|---|---|
| Q1 | Q2 | |
| Widget | £420 | £510 |
"Product" spans two rows since it has no separate sub-category; "2026 Sales" spans two columns since it's a grouping header for Q1 and Q2 beneath it. Working out colspan/rowspan values is genuinely easier by sketching the grid on paper first — getting them wrong silently shifts every following cell out of alignment.
headers Attribute — Explicit Cell-to-Header Association
Fundamentals Chapter 6's scope handles simple row/column relationships well. For a genuinely complex table — like the multi-level header above — a data cell can explicitly reference which specific header IDs describe it, for the most precise possible accessibility association.
Reserve headers for cases where scope alone genuinely can't express the relationship — a cell that depends on headers from both a row group AND a column group simultaneously, exactly the multi-level case above.
Sortable Table Headers — A Common Real-World Pattern
Many real tables let a visitor click a header to sort by that column — purely a JavaScript behaviour, but the underlying markup convention is worth knowing.
Using a genuine <button> inside the header cell, rather than attaching a click handler directly to the <th>, keeps the element keyboard-accessible and properly announced as interactive — exactly the kind of distinction the browser security/accessibility courses return to repeatedly.
Responsive Tables — A Brief Practical Note
Wide tables on narrow (mobile) screens are a genuinely hard layout problem, solved primarily with CSS rather than HTML markup changes — wrapping the table in a horizontally scrollable container is the most common, robust approach:
Full coverage of responsive layout techniques belongs in the CSS courses — this is included here purely because it's such a common real-world pairing with genuinely wide tables.
Chapter 2 Quick Reference
- colspan/rowspan in multi-level headers — sketch the grid first to avoid silent misalignment
- headers="id1 id2" on a <td> — explicit header association for cases scope alone can't express
- Sortable headers: wrap the clickable label in a real <button>, not a click handler on <th> directly
- Wide tables on mobile — wrap in a horizontally scrollable container; full responsive technique belongs in CSS
- Next chapter: multimedia in depth — video/audio attributes, tracks/subtitles, the picture element for responsive images