Sub topic 7
Making tables accessible with scope
This chapter assumes you can write a basic table with table, tr, td, and th. It also assumes you know what caption and thead/tbody/tfoot do, and that you can use attributes. Here you will learn how scope tells assistive technology which cells a header describes. You will also learn when scope is enough and when it isn't.
Why a table needs more than a visual grid
A sighted reader looks at a table and sees that "Rent" sits in a row, "Cost" sits in a column, and the number 900 belongs to both. A screen reader user can't take in the grid at a glance. They move through it one cell at a time, usually with the arrow keys or with table navigation commands. When they land on "900", the software has to say something like "Cost, Rent, 900". It can only do that if the markup states which headers belong to which data cells.
That mapping is the accessibility value of a table. If the mapping is missing, the user hears a stream of numbers with no meaning. If it is wrong, the user hears the wrong labels. Both are worse than a plain list.
Three pieces of markup carry the mapping:
thmarks a cell as a header, not data.scopesays which cells that header applies to.idandheadershandle layouts wherescopecan't express the relationship. A later chapter covers them.
What scope does
scope is an attribute for th elements. It declares the direction in which a header applies.
| Value | The header applies to | Typical use |
|---|---|---|
| `col` | The cells below it in its column | Header across the top of a column |
| `row` | The cells to its right in its row | Header at the start of a row |
| `colgroup` | All columns in the column group it spans | A header spanning several columns with `colspan` |
| `rowgroup` | All rows in the row group it belongs to | A header labeling a `tbody` section |
The attribute has no visual effect. Browsers don't draw anything differently because of it. Its effect appears in the accessibility tree, the structure browsers expose to screen readers and other tools.
Here is a table with both column headers and row headers:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Monthly expenses</title>
</head>
<body>
<table>
<caption>Monthly expenses</caption>
<thead>
<tr>
<td></td>
<th scope="col">Cost</th>
<th scope="col">Paid on</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Rent</th>
<td>900</td>
<td>1st</td>
</tr>
<tr>
<th scope="row">Food</th>
<td>300</td>
<td>Weekly</td>
</tr>
</tbody>
</table>
</body>
</html>When a screen reader user moves to the "900" cell, a typical reader announces "Cost, Rent, 900". The exact wording differs between NVDA, JAWS, and VoiceOver. The pairing of one column header and one row header is the same.
Notice the empty td in the top-left corner. That cell is a corner with nothing to label, so it should not be a th. An empty th is an unlabeled header, and some screen readers announce it as "blank" every time. Use an empty td, or put visually hidden text in a th if the corner needs a name such as "Item".
Browsers infer scope, so why write it?
The HTML specification defines a header-assignment algorithm. When a th has no scope, the browser guesses from position. A th with only data cells below it is probably a column header. A th at the start of a row followed by td cells is probably a row header. For simple tables the guess is usually right.
Writing scope anyway has three advantages:
- It removes the guesswork for tables with unusual layouts, such as a
thin the middle of a row. - It documents your intent for the next person who edits the table.
- Some assistive technology and older software do not guess well, and explicit values give them a clear signal.
Treat scope as cheap insurance. It costs a few characters per header and prevents the cases where inference fails.
How a header gets matched to a cell
The order matters. An explicit headers attribute on a cell overrides everything else. scope is the next most reliable signal. Position-based inference is the fallback.
Row headers: the most skipped case
Most developers remember scope="col" and forget row headers. Consider a table where the first column identifies each record:
<table>
<caption>City statistics</caption>
<thead>
<tr>
<td></td>
<th scope="col">Population</th>
<th scope="col">Area (km²)</th>
</tr>
</thead>
<tbody>
<tr>
<td>Oslo</td>
<td>700000</td>
<td>454</td>
</tr>
</tbody>
</table>This version is a common mistake. "Oslo" is a td, so a screen reader announces "Population, 700000" with no mention of which city. The fix is to make the first cell a row header:
<tr>
<th scope="row">Oslo</th>
<td>700000</td>
<td>454</td>
</tr>Now the announcement includes "Oslo". A good test is to read a data cell aloud with only its headers. If you can't tell what the number describes, a row header is missing.
Not every table needs row headers. A table of readings where each row is an anonymous observation may have none. The rule is that each data cell needs a header for every dimension that identifies it.
Using colgroup and rowgroup
scope="colgroup" and scope="rowgroup" apply a header to a whole group of columns or rows. The first is used with a th that spans several columns through colspan. The second is used with a th that labels a section of tbody rows.
<table>
<caption>Staff by office</caption>
<thead>
<tr>
<td></td>
<th scope="col">Staff</th>
<th scope="col">Open roles</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="rowgroup" rowspan="2">Norway</th>
<th scope="row">Oslo</th>
<td>40</td>
<td>3</td>
</tr>
<tr>
<th scope="row">Bergen</th>
<td>12</td>
<td>1</td>
</tr>
</tbody>
</table>This table has an extra header column for the country, so the thead row needs a matching extra cell. Add one more empty td before "Staff" if you want the column headings to line up. As written, the header cells are shifted by one column, which is a good illustration of how rowspan and colspan break the grid when the counts are off. Count cells per row before you trust the layout.
Support for colgroup and rowgroup is less consistent across screen readers than support for col and row. If a table needs more than one level of header in both directions, prefer the id and headers approach from the later chapter. It states the mapping explicitly for every cell.
What scope cannot fix
Some layouts defeat scope alone:
- Two header levels in one direction. A year row above a quarter row, as in the "Sales by region" table, creates ambiguity for
scope.headersandidremove it. - Headers in the middle of a table. A
ththat appears halfway down to start a new section works only if its scope is clear to the software. - Merged cells that break the grid. Overlapping
rowspanandcolspanproduce undefined results, because the cell no longer sits in a clean row or column.
If you need those layouts, ask first whether two simpler tables would serve better. A table that is easy to build is usually easy to hear.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Using `td` with bold text for headings | Looks like a header but is announced as data | Use `th` |
| Using `th` to center or bold a data cell | Reader announces a false header for the cells it spans | Use `td` and style with CSS |
| Putting `scope` on `td` | Invalid in current HTML and ignored | Use `th`, or use `headers` on `td` |
| Leaving an empty `th` | Announced as a blank header | Use an empty `td`, or hidden text |
| Using `role="presentation"` on a data table | Removes all table semantics | Keep it only for layout tables, and avoid those |
| Changing `display` on table elements | Can drop table semantics in some browsers | Add `role` attributes back, or avoid changing it |
The last item deserves care. Styling a table with display: block or display: flex for a responsive layout once removed table semantics in several browsers. Current browsers have improved, but the behavior varies by version. If you restyle table elements that way, test with a screen reader and add role="table", role="row", role="columnheader", and role="cell" where needed.
Testing your table
Do not rely on the markup alone. Three checks catch most errors:
- Open the accessibility tree. Chrome, Edge, and Firefox developer tools show the computed role of each element. A header cell should show a role of
columnheaderorrowheader, notcell. - Use a screen reader. NVDA on Windows, VoiceOver on macOS, and Orca on Linux are free. Move through the table with the arrow keys and listen to what is announced.
- Run an automated checker. Tools such as axe or Lighthouse flag
tdwithscope, empty headers, andheadersattributes that point to missing ids.
Automated checks find structural errors. They can't tell you whether a header name makes sense, so a human pass is still needed.
A checklist
Before you ship a data table, confirm each of these:
- Every header cell is a
th, and every data cell is atd. - Each column header has
scope="col", and each row header hasscope="row". - The corner cell is an empty
td, or athwith real text. - The table has a
captionthat names it. - Any layout with two header levels in one direction uses
idandheaders. - You heard the table read aloud, or inspected its accessibility tree.
Exercise
A table lists products down the first column ("Pen", "Notebook") and prices and stock counts in the next two columns. The first column cells are td. What does a screen reader announce when the user lands on the price for "Pen", and what change fixes it?
Show answer
It announces only the column header and value, such as "Price, 2.50", with no product name. The product names are td cells, so they are treated as data and are not linked to the row. Change each product cell to <th scope="row">, and make the column headings <th scope="col">. The reader then announces the row header with the value, such as "Price, Pen, 2.50".
Exercise
Find the two errors in this markup, and explain the effect of each: <tr><th></th><th scope="col">Cost</th></tr><tr><td scope="row">Rent</td><td>900</td></tr>
Show answer
First, the empty th in the corner is an unlabeled header and may be announced as "blank". Use an empty td or give it real text. Second, scope="row" is on a td, where it is obsolete and ignored, so "Rent" is not linked to the row. Change that cell to <th scope="row">Rent</th>.
Exercise
A table has a year header spanning two quarter columns, so each data cell needs both a year and a quarter header. Why is scope alone a weak choice here, and what do you use instead?
Show answer
Two header levels in one direction form a layout where scope="colgroup" and scope="col" are not read consistently across screen readers. Give each header an id, and give each data cell a headers attribute listing the ids of every header that applies, such as headers="north y25 q1". That states the mapping explicitly.