Sub topic 7
Making tables accessible with scope
This chapter assumes you can already write a table with table, tr, td, and th, group rows with thead, tbody, and tfoot, and add a caption. Here you will learn how the scope attribute tells assistive technology which cells each header describes.
Why a screen reader needs more than a grid
Sighted readers find the header for a cell by looking along the row and up the column. A screen reader user cannot do that. They move one cell at a time and need the software to announce the matching headers.
Browsers can often guess which headers apply in a simple table. The guess gets less reliable with empty corner cells, spanned headers, or headers in the middle of a row. The scope attribute removes the guesswork. It states the direction a header applies in.
The scope attribute
scope is valid only on th elements. It takes four values:
| Value | The header applies to |
|---|---|
| `col` | The cells below it in its column |
| `row` | The cells to its right in its row |
| `colgroup` | All columns in a column group, or all columns a `colspan` header covers |
| `rowgroup` | All rows in the row group (`thead`, `tbody`, or `tfoot`) it sits in |
Most tables need only col and row:
<table>
<caption>Quarterly sales in thousands</caption>
<thead>
<tr>
<td></td>
<th scope="col">Q1</th>
<th scope="col">Q2</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">North</th>
<td>120</td>
<td>135</td>
</tr>
<tr>
<th scope="row">South</th>
<td>98</td>
<td>110</td>
</tr>
</tbody>
</table>When a screen reader lands on the cell containing 135, it can announce "North, Q2, 135". The row header and column header come from the scope values.
The top-left cell is a td, not a th. It labels nothing, so an empty header would be noise. Using td keeps it out of the header mapping.
Using colgroup and rowgroup
Use colgroup when a header spans several columns. Use rowgroup when a header labels a whole tbody.
<table>
<caption>Scores by subject</caption>
<thead>
<tr>
<td rowspan="2"></td>
<th colspan="2" scope="colgroup">Term 1</th>
</tr>
<tr>
<th scope="col">Math</th>
<th scope="col">Art</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Ana</th>
<td>90</td>
<td>85</td>
</tr>
</tbody>
</table>"Term 1" covers both columns below it. Math and Art then refine it. This two-level layout is the most that scope handles reliably. Deeper or irregular layouts need the id and headers technique, which a later chapter covers.
How the pieces work together
scope is one of several signals. Each one answers a different question.
A caption lets a user decide whether to enter the table at all. Screen readers usually list captions when users browse tables. Keep the caption short and specific, such as "Quarterly sales in thousands", not "Table 1".
Common errors
- Putting
scopeontd. Current HTML allows it only onth. Useth scope="row"for a row label. - Using
thfor styling. Do not make a cell a header just to get bold, centered text. Use CSS for appearance. - Wrong direction.
scope="col"on a row label makes the reader announce the wrong headers. Check each value against the direction the header points. - Empty
thcells. An empty header gets announced as a blank. Usetdfor empty corner cells. - Skipping
scopebecause the table looks simple. A one-line attribute makes the intent explicit across browsers and screen readers. - Using
scopefor complex headers. If a data cell needs headers thatscopecannot express, such as an irregular merge, switch toidandheaders.
Checklist before shipping a table
- The content is truly tabular, with meaning in both rows and columns.
- A
captionnames the table. - Every column header is a
thwithscope="col". - Every row header is a
thwithscope="row". - Spanning headers use
colgrouporrowgroupwhere they cover several columns or rows. - Empty corner cells are
td, notth.
Exercise
A table has a header cell "Region" at the left of each body row, and a header "2025" that spans the two columns "Q1" and "Q2". Which scope value goes on "Region" cells, and which goes on "2025"?
Show answer
Each "Region" cell is a th with scope="row", because it labels the cells to its right in its row. The "2025" header is a th with colspan="2" and scope="colgroup", because it covers a group of columns. "Q1" and "Q2" each get scope="col".
Exercise
A developer writes <td scope="row">Rent</td>. What is wrong, and how do you fix it?
Show answer
scope is valid only on th, and a td is not a header, so the cell does not label its row. Change it to <th scope="row">Rent</th>.