Sub topic 3
Header cells with th
This chapter assumes you can write a basic HTML document, you know the table, tr, and td elements from the previous chapter, and you can write simple CSS rules. It covers the th element: what it means, where it goes, how browsers display it, and the mistakes that cause trouble later.
What th is
A th element is a header cell. It labels other cells, while a td element holds the data that is being labeled. Both sit inside a tr, and both take the same attributes for spanning (colspan, rowspan), which later chapters cover.
The difference is meaning, not looks. A td says "this is a value." A th says "this describes a group of values." Screen readers, search engines, and other software that reads your markup use that meaning. They can announce "Rent, Cost, 900" when a user lands on a cell. Without th, they can only announce "900".
Here is the smallest useful table with headers:
<table>
<tr>
<th>Item</th>
<th>Cost</th>
</tr>
<tr>
<td>Rent</td>
<td>900</td>
</tr>
<tr>
<td>Food</td>
<td>300</td>
</tr>
</table>The first row is made of th cells. Each one labels the column of td cells below it.
Column headers and row headers
A header can label a column or a row. Many tables need both.
- A column header sits at the top and labels the cells beneath it.
- A row header sits at the start of a row and labels the cells to its right.
In the example above, only the columns have headers. The words "Rent" and "Food" identify each row, but they are td cells, so software does not know they are labels. If the first cell in each row identifies that row, make it a th as well:
<table>
<tr>
<th scope="col">Item</th>
<th scope="col">Cost</th>
</tr>
<tr>
<th scope="row">Rent</th>
<td>900</td>
</tr>
<tr>
<th scope="row">Food</th>
<td>300</td>
</tr>
</table>The scope attribute states which direction a header applies to. The value col means the header labels the cells below it, and row means it labels the cells to its right. Two other values, colgroup and rowgroup, exist for headers that cover several columns or row groups. A later chapter on accessibility covers scope in full. For now, treat it as a habit: write scope="col" on column headers and scope="row" on row headers.
What browsers do with th by default
Browsers apply a default style to th: bold text, centered horizontally. Everything else about the cell, including padding and borders, is the same as a td.
That default is only a style, so you can change it. If you prefer left-aligned headers, write a rule:
th {
text-align: left;
font-weight: normal;
}The cell is still a header after that rule. Its meaning comes from the element, not from how it looks. This point matters for the next two sections.
Do not fake headers
A common error is to build a header out of a td:
<tr>
<td><strong>Item</strong></td>
<td><strong>Cost</strong></td>
</tr>This looks like a header row, but software sees two ordinary data cells. A screen reader will not announce "Item" when the user reaches the cell below. Use th and style it with CSS if you want it to look different.
The reverse mistake is also common. Some authors use th on cells that are not headers, because they want bold, centered text. For example, a th around a total value in a footer, or around a note spanning the whole table. That tells assistive technology that the cell labels something when it does not. If you only want the look, use a td and add a class or a CSS rule.
A simple test: does this cell describe other cells in the table? If yes, use th. If it is a value, use td.
The empty corner cell
When a table has both column headers and row headers, the top-left cell has nothing to label. It sits above the row headers and to the left of the column headers. Authors handle it in one of two ways.
The first is an empty td. The HTML specification allows this, and it tells software the cell is intentionally blank:
<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>94</td>
</tr>
</table>The second is to give the corner a real label, such as "Student". This helps users, because it names what the row headers are. It is often the better choice when the meaning of the first column is not obvious.
Avoid an empty th. It claims to be a header but has no text, and a screen reader may announce it as "blank" or skip it. Some accessibility checkers flag this.
What can go inside a th
A th can hold text, links, images, and other inline content. Keep it short. A header is read aloud each time a user moves to a cell it labels, so long headers get repeated and become tiring.
The specification does not allow some elements inside a th: header, footer, sectioning elements such as section and nav, and heading elements h1 to h6. A heading inside a cell breaks the page's outline and adds nothing, because the th already acts as a label.
If a visible label must be short but the full meaning is longer, add an abbr element with a title, or put the details in the caption or nearby text:
<th scope="col"><abbr title="Quarter 1">Q1</abbr></th>Headers outside the first row
A header row does not have to be the first row, and header cells are not limited to thead. Two other places are common:
- In a
tfoot, for a row such as "Total". Thethlabels the row, and thetdnext to it holds the sum. - In the middle of a table that has several sections, where each section begins with a row of
thcells.
The thead, tbody, and tfoot elements get their own chapter. What matters here is that the choice between th and td depends on what the cell does. It does not depend on which section contains it.
A table may also have more than one row of headers. Suppose a top row groups columns by year and a second row lists quarters. Both rows use th. The year header needs colspan to cover its quarters, and that case is in the chapters on spanning and complex headers.
Row headers and the first column
Not every first column is a header column. Ask what the first cell does:
- If it names the thing the row is about (a person, a product, a month), make it a
thwithscope="row". - If it is just a counter or a checkbox, leave it as a
td.
Tables of records usually have one identifying column. Putting th on every cell in a row, to make them all bold, defeats the purpose. A row should have a small number of headers, and they should be the cells that identify it.
Styling headers separately from data
Because th is its own element, CSS can target it without extra classes. You can also distinguish column headers from row headers using the scope attribute as a selector:
th[scope="col"] {
background: #f0f0f0;
text-align: left;
}
th[scope="row"] {
font-weight: normal;
text-align: left;
}This keeps the markup clean. The scope attribute does accessibility work and also gives CSS a hook. Table styling in general has its own chapter, so this is only a preview.
Common errors
| Error | What goes wrong | Fix |
|---|---|---|
| Using `td` with bold text for headers | Software does not know the cell is a label | Use `th` and style it with CSS |
| Using `th` for non-header cells to get bold, centered text | Software announces labels that do not exist | Use `td` and a CSS rule |
| Leaving out `scope` in a table with merged cells | The browser may guess the direction wrongly | Add `scope="col"` or `scope="row"` |
| Writing an empty `th` in the corner | Screen readers announce a blank header | Use an empty `td` or give the cell a label |
| Putting a heading element inside a `th` | Breaks the document outline | Put plain text in the `th` |
| Writing very long header text | Repeated for every cell read aloud | Shorten it, or explain in the caption |
Check that headers work
You do not need a screen reader to find basic problems. These checks catch most of them:
- Look at each
thand ask what it labels. If you cannot answer, it should probably be atd. - Look at each
tdthat contains a name, date, or category used to identify its row. It may need to be ath. - Add
scopeto everythin the table and confirm that each one points the right way. - Turn off your CSS, or read the page in a browser's reader mode, and see whether the table still makes sense.
- Run an accessibility checker, such as the one in your browser's developer tools. It will flag empty headers and missing header cells.
Summary
Use th for any cell that labels other cells, and td for everything else. Add scope="col" or scope="row" so the direction is explicit. Browsers show th as bold and centered, but that look is a default you can change. The element's job is to carry meaning. In the next chapter, you will add a caption, which names the table as a whole and works together with headers to help readers find their way.
Exercise
A colleague writes a price table where every cell in the first row is <td><b>…</b></td> and every cell in the first column is a plain td. Rewrite the structure of the first row and the first-column cells so that assistive technology can tell which cells are labels. State what attribute you add to each kind of header, and what you would do with the top-left cell if it has no label.
Show answer
Change the first-row cells to <th scope="col">…</th> and the first-column cells to <th scope="row">…</th>. Remove the <b> elements and use CSS (for example th { text-align: left; }) if the look needs adjusting. The top-left cell can be an empty <td></td>, or a th with a real label such as "Product". It should not be an empty th.
Exercise
Why is it wrong to use th for a "Total" value in a footer row, such as <th>1200</th>, just to make it bold? What should the markup be?
Show answer
The value 1200 is data, not a label, so marking it as th tells software it describes other cells. Use <th scope="row">Total</th> for the word "Total", which labels the row, and <td>1200</td> for the value. Make the value bold with CSS if you want it to stand out.