Sub topic 3
Header cells with th
This chapter assumes you can write a basic table with table, tr, and td, and that you know how to set attributes and write simple CSS rules. You will learn what a header cell is, how to mark column and row headers, and which mistakes make a table harder to use.
What th does
A th element is a header cell. It uses the same slot in a row as a td, but it says that its content labels other cells. A td holds data. A th describes data.
Browsers and screen readers use this difference. A screen reader can announce "Cost, 900" when a user reaches a data cell. It can only do that if it knows which cell is the header for that column.
Do not use th just to get bold, centered text. If a cell does not label other cells, use td and style it with CSS.
Column headers
The most common pattern is a first row of th cells, one for each column.
<table>
<tr>
<th scope="col">Item</th>
<th scope="col">Cost</th>
</tr>
<tr>
<td>Rent</td>
<td>900</td>
</tr>
<tr>
<td>Food</td>
<td>300</td>
</tr>
</table>The scope="col" attribute states that each header applies to the cells below it. Browsers can often guess this for a simple table. Writing it out removes the guess. The next accessibility chapter covers scope in detail.
Row headers
Some tables also label each row. In that case, the first cell of each row is a th too, with scope="row".
<table>
<tr>
<td></td>
<th scope="col">Math</th>
<th scope="col">Art</th>
</tr>
<tr>
<th scope="row">Ana</th>
<td>90</td>
<td>85</td>
</tr>
<tr>
<th scope="row">Ben</th>
<td>72</td>
<td>91</td>
</tr>
</table>Now each score has two labels. The 85 belongs to row "Ana" and column "Art". A screen reader can announce both when a user lands on that cell.
The empty corner cell
In the example above, the top-left cell is an empty td. It sits above the row headers and beside the column headers. It labels nothing, so it is not a header. An empty th gives assistive technology a header with no name, which is confusing. If you can give the corner a meaningful label, such as "Student", use a th with scope="col".
Default appearance and how to change it
Browsers give th a default style. The text is bold and centered. You can change both with CSS. The markup stays the same, so the meaning is preserved.
th {
text-align: left;
background: #f0f0f0;
}
th[scope="row"] {
font-weight: normal;
}The first rule left-aligns every header and shades it. The second rule uses an attribute selector so that row headers are not bold, while column headers stay bold. This works because scope is real markup that your CSS can select.
Header cells compared with data cells
| Feature | `th` | `td` |
|---|---|---|
| Meaning | Labels other cells | Holds a data value |
| Default text | Bold, centered | Normal, left-aligned |
| Accepts `scope` | Yes | No |
| Announced as a header | Yes | No |
How a reader finds the header
A screen reader follows a short chain of steps as the user moves through the table.
Common errors
- Using
tdwith bold styling for headers. The table looks right, but a screen reader sees only data cells. - Using
thfor every cell in a row of values. This marks data as labels and makes the announcements noisy. - Leaving
scopeout of a table with both row and column headers. Browsers may guess the wrong direction for a row header in the first column. - Empty
thcells. Use an emptytdfor a corner with no label. - Setting
scope="row"on a header that sits in a column header row. The attribute describes what the header labels, so it must match where the header sits.
Exercise
In a table where the first column holds product names and the first row holds month names, which cells should be th, and which scope value does each group get? What should the top-left cell be if it has no label?
Show answer
The month names in the first row are th with scope="col". The product names in the first column are th with scope="row". An unlabeled top-left cell should be an empty td, because an empty th creates a header with no name.
Exercise
A teammate writes <td><strong>Total</strong></td> for the label at the start of a summary row. What problem does this cause, and what is the fix?
Show answer
The bold style looks like a header, but the cell is still a data cell, so assistive technology does not treat "Total" as a label for the numbers beside it. Change it to <th scope="row">Total</th> and use CSS if you want to change its weight.