TBL-001 Build data in rows and columns as a table in code, with its headings marked as headers
- Level
- Rule
- Verdict
- Requirement
- Area
- Tables
- Status
- proposed, version 1.0.0
Rule
When values are shown in rows and columns under headings, build them as a table in code. Mark each heading cell as the header of its column or row, so assistive technology can tell which headings every value belongs to.
Examples
Live examples built with Justified's components. Try them with the mouse, the keyboard or a screen reader.
Do: A real table with headers
| Site | pH | Nitrate (mg/l) | Collected by |
|---|---|---|---|
| Mill Lane footbridge | 7.4 | 12 | R. Okafor |
| Harbour steps | 7.1 | 9 | M. Lindqvist |
| Quarry pond | 6.8 | Not measured | R. Okafor |
| Weir Road culvert | 7.6 | 15 | T. Haddad |
Don't: A grid of divs that looks like a table
Water samples
div elements. Click 7.4, or press Next cell: it is a loose value, with no column or row heading in the code. A sighted person sees the ties and a screen reader user does not.Do: A checkbox column that has a heading
| Select | Site | pH | Collected by |
|---|---|---|---|
| Mill Lane footbridge | 7.4 | R. Okafor | |
| Harbour steps | 7.1 | M. Lindqvist | |
| Quarry pond | 6.8 | R. Okafor | |
| Weir Road culvert | 7.6 | T. Haddad |
Don't: An empty heading over the checkboxes
Why
A sighted person reads a value by glancing up at its column and across to the start of its row. "7.4" means nothing alone; under pH, in the row for Mill Lane footbridge, it is a reading. A screen reader user gets that meaning only if the code holds the same ties. In a real table, moving from cell to cell, the screen reader says the heading of the new column or row, and the person can move down one column or along one row. A grid of div elements styled to look like a table gives them a run of loose numbers instead.
WCAG 2.2, 1.3.1 Info and Relationships requires that relationships shown by the layout can also be read from the code. The Working Group lists a data table whose headings are not marked as headers as a failure (F91). That failure accepts several ways to pass: header elements, scope on the header cells, headers and id on the data cells, or the ARIA roles for tables and their headers. So the criterion asks that the headers can be found in code. It does not ask for the table element, for th or for scope as such; those are the techniques in H51 and H63. 4.1.2 Name, Role, Value supports the rule: the roles a table exposes are what lets software treat it as one.
Do
- A council's page Water samples lists one row per site, with the columns Site, pH, Nitrate (mg/l) and Collected by. It is built with shadcn's
Table: the headings areTableHeadcells insideTableHeader, and the values areTableCellcells inTableBody.TableHeadrenders athandTableCellatd, so the ties are in place. - Our own advice, beyond the requirement: give each column heading
scope="col"and render the cell that names the row as a row header,<TableHead scope="row">Mill Lane footbridge</TableHead>, styled like the other body cells withfont-normal. A screen reader then says the site's name as the person moves along its row. The criterion needs a row header only where the first column is what gives the other cells their meaning, which in a data table it usually is. - With TanStack Table, map each header group to a
TableRowofTableHeadcells. Give each onecolSpan={header.colSpan}, and renderflexRender(header.column.columnDef.header, header.getContext())inside it, or nothing whenheader.isPlaceholderis true. Map the rows oftable.getRowModel()toTableRowwithTableCell, or withTableHead scope="row"for the column that names the row. - Give every header cell text, also over a column of checkboxes or row actions. When the column needs no visible heading, put "Select" or "Actions" in an
sr-onlyspan, so the column still has a name. - Keep the header row and the body in one table. To keep the headings in view while the body scrolls, let the table scroll in its own area of limited height and make
TableHeadersticky withsticky top-0 bg-background. shadcn'sTablewraps thetablein adivwithoverflow-x-auto, so a sticky header there sticks to thatdiv, not to the page. Give that wrapper a limit such asmax-h-96andoverflow-autoin your copy oftable.tsx; theclassNameyou pass toTablegoes to thetable, not to the wrapper. - When a group heading spans several columns, such as Nitrate (mg/l) over Upstream and Downstream, plain
this not enough. Usescope="colgroup"on the group heading or tie each cell to its headings withheadersandid. - Leave an empty value as a cell, so the columns stay in line in code. Our own advice: write "Not measured" or a dash, which is read as text, rather than leaving the cell blank.
- If the table is too wide for a narrow screen, give it its own scroll area as LAY-001 says, and keep the table structure inside it. shadcn's
Tablealready renders a scrolling wrapper, but with notabIndex, role or name, so add the focus and the name LAY-001 asks for in your copy oftable.tsx. - If a narrow layout turns each row into a card, put the heading text next to each value in the card's markup, such as "pH: 7.4", so each value still carries its label. Our own advice: do not rely on text added with CSS
contentfor this, since screen readers do not all read it. - A table built from
divelements withrole="table",role="row",role="columnheader",role="rowheader"androle="cell"meets the criterion too. Our own advice is to use the native elements, which shadcn'sTablealready renders: one wrapper without its role breaks the whole ARIA structure, and the native one cannot break that way. - In a long table that renders only the rows in view, our own advice is to set
aria-rowcounton the table andaria-rowindexon each row, so a screen reader can say where in the full list the person is.
Don't
- Build the Water samples grid from
div className="grid grid-cols-4", with the heading row as one moredivinfont-medium text-muted-foreground. - Render the TanStack header group as a
TableRowofTableCell, so every heading is atd. - Put the headings in one
Tableabove a second, scrollingTablefor the body, so no value is tied to its heading. - Leave the header cell over the row checkboxes empty.
- Set
display: blockordisplay: flexontable,trortdto make the table fit a phone. Some browsers then drop the table semantics. - Use a table to lay out a form or a page. A table for layout has no
thand nocaption. - Turn rows into cards on a phone and drop the headings, leaving "7.4", "12" and "R. Okafor" with nothing to say what they are.
Exceptions
- Content that is not a grid of values. A set of cards in which every value carries its own visible label, such as "Collected by R. Okafor", already holds the tie in its text and is outside this rule. Choosing a table or cards is not part of it.
- One record. Labels beside the values of a single record can be a description list (
dl) or a two-column table with row headers. Both meet the criterion. - Interactive grids. A grid that people edit in place and move through with the arrow keys has its own roles and keyboard model, and this rule does not cover it.
- Neighbouring rules. Sorting by a column is TBL-002, actions in a row are TBL-003 and the table's title is TBL-004. Reflow is LAY-001, the order of the columns in code is LAY-002, and the names of controls inside cells, such as a checkbox on each row, are FRM-010.
Verdict
Requirement. Only the judges decide; the expert witnesses are shown but do not vote. How verdicts work
| Source | Role | Stance | Page |
|---|---|---|---|
| WCAG 2.2 | Law | Requires | 1.3.1 Info and Relationships |
| WCAG 2.2 | Law | Supports | 4.1.2 Name, Role, Value |
| Material Design | Judge | No position | |
| Apple Human Interface Guidelines | Judge | Supports | Lists and tables |
| Carbon Design System | Judge | Supports | Data table: usage Data table: accessibility |
| GOV.UK Design System | Expert witness | Supports | Table |
| Nielsen Norman Group | Expert witness | Supports | Data Tables: Four Major User Tasks, by Page Laubheimer Mobile Tables: Comparisons and Other Data Tables, by Amy Schade |
Each stance links to the page it rests on, read on the date in the source register. This page never repeats what a source says; read the source itself.
History
- Version 1.0.0, : First version.
Last checked against the other rules for conflicts on .
More in Tables: 3 rules and 1 guideline · All rules
Justified is an independent project. It is not affiliated with, sponsored or endorsed by the organisations whose guidance it cites. Read the full statement.
Apple is a trademark of Apple Inc., registered in the U.S. and other countries and regions.