Lesson 26 of 51 · Module 4, lesson 5 of 6 · 3 min

Table headers and captions

Mark table headers so that screen readers can announce which row and column each value belongs to.

What you'll learn

  • How to mark column and row headers with scope
  • How to group rows with <thead>, <tbody> and <tfoot>
  • How a screen reader reads a table

Why this matters

A sighted reader glances up and sideways to see which column and row a number belongs to. A screen reader user cannot glance — the screen reader has to announce the headers. It can only do that if your HTML says clearly which cells are headers and what they describe.

Column and row headers: scope

The scope attribute on <th> says what the header describes:

  • scope="col" — the header of a column.
  • scope="row" — the header of a row.

In the study plan, "Day", "Topic" and "Minutes" are column headers. The day names are row headers: each one describes its whole row.

Groups of rows

  • <thead> holds the header row(s).
  • <tbody> holds the data rows.
  • <tfoot> holds summary rows, such as a total.

The complete, accessible table

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Study plan</title>
</head>
<body>
  <table>
    <caption>Study time this week</caption>
    <thead>
      <tr>
        <th scope="col">Day</th>
        <th scope="col">Topic</th>
        <th scope="col">Minutes</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th scope="row">Monday</th>
        <td>Headings</td>
        <td>30</td>
      </tr>
      <tr>
        <th scope="row">Wednesday</th>
        <td>Links</td>
        <td>45</td>
      </tr>
      <tr>
        <th scope="row">Friday</th>
        <td>Images</td>
        <td>40</td>
      </tr>
    </tbody>
    <tfoot>
      <tr>
        <th scope="row">Total</th>
        <td></td>
        <td>115</td>
      </tr>
    </tfoot>
  </table>
</body>
</html>

What you'll see: the same table as before, now with the day names in bold (they are headers) and a "Total" row at the bottom.

The study-plan table with the caption "Study time this week". The column headers Day, Topic and Minutes are bold, the day names Monday, Wednesday and Friday are bold row headers, and a final row reads Total, empty, 115.

A labelled table. The caption "Weekly study plan" is the table's title. The column headers Day, Topic and Minutes are th elements with scope="col" inside thead. The day names are th elements with scope="row". The other cells are td elements, and each row is a tr. A screen reader on the value 45 says "Wednesday, Minutes, 45": it reads the headers with the cell.

What the screen reader does

When a screen reader user moves to the cell "45", they hear something like "Wednesday, Minutes, 45" — the row header, the column header and the value. Without <th> and scope, they would hear only "45".

Try it yourself

  1. Select the example and choose Try it.
  2. Add a row for Sunday, with its day as a row header, and update the total.
  3. Open the page in your browser, open the developer tools, and in Chrome or Edge choose the Accessibility pane in the Elements panel. Click a cell: you can see how the browser describes it.

Common mistakes

  • Using <td> with bold text as a header. It looks like a header, but a screen reader does not know. Fix: use <th>.
  • No scope on headers. Simple tables often work anyway, but not always. Fix: add scope="col" or scope="row" every time.
  • A caption written as a paragraph above the table. It is not connected to the table. Fix: use <caption>.
  • <tfoot> for normal data rows. Fix: use it only for totals or summaries.

Check yourself

  1. Which attribute tells a screen reader whether a header describes a column or a row?
  2. In a list of days and topics, what scope should the day names have?
  3. Which element holds a row with a total?

Answers

  1. scope.
  2. scope="row" — each day describes its row.
  3. <tfoot>.

Next: sometimes one cell covers two columns or two rows — like a two-hour lesson in a timetable.

Chat with us on WhatsApp