Thuta Learning
HTML
IntermediateWeb DevelopmentBeginner

Table Caption & Scope

HTMLLesson 2025 min

What you'll walk away with

  • Give every table a short caption that states its purpose
  • Use scope="col" on column headers and scope="row" on row headers correctly
  • Predict which headers a screen reader pairs with a data cell
  • Decide when colgroup / rowgroup scope or the headers attribute is needed

Explain it simply

Explain it simply

When you look at a table and spot "6,250", a glance left tells you it is February and a glance up tells you it is Sales. A screen reader user hears one cell at a time and cannot glance anywhere.

So the HTML has to tell the browser plainly: this cell belongs to this row header and this column header. caption gives the table its name, and th with scope says which direction each header points.

Problem → Why → Solution

The problem: a table whose headers are hidden

In a sales report the header row is written with td and b tags, and the month names are in td cells too. There is no caption, so if the page has three tables, the screen reader's table list shows three unnamed "table" entries. Moving through the cells, the user hears bare numbers such as "6,250" and "148" and has to remember which month and which measure each one is.

report.html
<table>
  <tr><td><b>Month</b></td><td><b>Sales</b></td><td><b>Orders</b></td></tr>
  <tr><td>January</td><td>5,000</td><td>120</td></tr>
  <tr><td>February</td><td>6,250</td><td>148</td></tr>
</table>

Why isn't bold text enough?

b only makes text heavier; it does not change the cell's role. With no header cells, the browser cannot pair data cells with any header, so the screen reader reads the number alone. Use th with scope and the same cell can be announced with context, such as "February, Sales, 6,250".

Core concept: caption names it, scope points the way

caption must be the first child of table, and it becomes the table's accessible name. Screen reader users often list the tables on a page by name, so a short, clear caption such as "Monthly sales, January to March" tells them what a table is about before they enter it. The caption is also the visible title for sighted readers, and you can style it with CSS.

scope valueThe header applies to
colThe whole column below it
rowThe rest of its row
colgroupA group of columns defined by colgroup
rowgroupThe thead/tbody/tfoot row group it sits in

In simple tables without scope, browsers guess: th cells in the first row become column headers and th cells in the first column become row headers. But the guess can differ between browsers and screen readers, and it often fails with two header rows or headers in the middle of the table. Adding scope removes the guesswork, and it costs a single attribute.

In tables with two levels of headers, such as Maths and English under "Term 1", the top header gets colspan and scope="colgroup", and colgroup elements define the column groups. Only when relationships are too complex for scope should you give th cells an id and link each td directly with headers="id1 id2"; and by that point, first consider whether the table could be split.

Write header text that makes sense when heard. "Quantity" is clearer than "Qty", and "Sales (USD)" clearer than "$". Screen readers may read an empty cell as "blank", so put a value such as "None" or "—" in it. The old summary attribute is obsolete; if a table needs more explanation, write it in the caption or in a paragraph before the table.

Mental model: every cell has two addresses
  1. caption names the table
  2. Column headers: th scope="col"
  3. Row headers: th scope="row"
  4. Each td = row header + column header

When a data cell is heard, the markup must answer two questions: which row, and which column.

General pattern

html
<table>
  <caption>Purpose of the table</caption>
  <tr>
    <th scope="col">Row label heading</th>
    <th scope="col">Column heading</th>
  </tr>
  <tr>
    <th scope="row">Row heading</th>
    <td>Value</td>
  </tr>
</table>

The th cells in the top row take scope="col", the first cell of each row is a th with scope="row", and every other cell is a td.

Example: a monthly sales report

Borders come from CSS rather than the border="1" attribute. Notice that every data cell has both a row header (the month) and a column header (the measure), and that each th uses scope to say which direction it labels.

sales-report.html
<style>
  table { border-collapse: collapse; }
  th, td { border: 1px solid #999; padding: 0.4rem 0.8rem; }
  th[scope="row"] { text-align: left; }
  td { text-align: right; }
</style>

<table>
  <caption>Monthly sales, January to March</caption>
  <thead>
    <tr>
      <th scope="col">Month</th>
      <th scope="col">Sales (USD)</th>
      <th scope="col">Orders</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">January</th>
      <td>5,000</td>
      <td>120</td>
    </tr>
    <tr>
      <th scope="row">February</th>
      <td>6,250</td>
      <td>148</td>
    </tr>
    <tr>
      <th scope="row">March</th>
      <td>5,800</td>
      <td>133</td>
    </tr>
  </tbody>
</table>

Code breakdown

  1. caption<caption>Monthly sales, January to March</caption>

    The first child of the table, which becomes its accessible name. It says both what the data is and which period it covers.

  2. Column header<th scope="col">Sales (USD)</th>

    This header applies to the whole column below it. The unit (USD) lives in the header, so the cells need no $ sign.

  3. Row header<th scope="row">February</th>

    The first cell of the row is a th, not a td, and scope="row" says it applies to every cell to its right.

  4. Data cell<td>6,250</td>

    This cell needs no label of its own: the row header February and the column header Sales (USD) provide its context.

  5. Alignmentth[scope="row"] { text-align: left; }

    th cells are centred by default, so row headers are set back to left-aligned. Note that the scope attribute doubles as a handy CSS selector.

You should see
Under the caption "Monthly sales, January to March" a table with 3 columns and 4 rows appears:
Month | Sales (USD) | Orders
January | 5,000 | 120
February | 6,250 | 148
March | 5,800 | 133
Headers and month names are bold, month names are left-aligned and numbers right-aligned. In the accessibility tree the months are row headers and the top row holds column headers.

Two levels of headers: colgroup scope

In the table below, "Term 1" and "Term 2" each sit above two columns. The colgroup elements define those column groups, and scope="colgroup" ties each header to its group, so Aye's 90 can be read with three headers: "Aye, Term 2, Maths". Without the colgroup scope, the two Maths columns would sound identical, and a listener could not tell which term a score came from.

exam-results.html
<table>
  <caption>Exam results by term</caption>
  <colgroup><col></colgroup>
  <colgroup span="2"></colgroup>
  <colgroup span="2"></colgroup>
  <thead>
    <tr>
      <th scope="col" rowspan="2">Student</th>
      <th scope="colgroup" colspan="2">Term 1</th>
      <th scope="colgroup" colspan="2">Term 2</th>
    </tr>
    <tr>
      <th scope="col">Maths</th><th scope="col">English</th>
      <th scope="col">Maths</th><th scope="col">English</th>
    </tr>
  </thead>
  <tbody>
    <tr><th scope="row">Aye</th><td>85</td><td>78</td><td>90</td><td>82</td></tr>
    <tr><th scope="row">Ko Ko</th><td>72</td><td>88</td><td>75</td><td>91</td></tr>
  </tbody>
</table>

In real projects

Where this shows up in real projects

Projects that must meet accessibility standards (WCAG), such as government sites, banks and education platforms, audit every data table for a caption and header relationships. WCAG 2.2 success criterion 1.3.1, "Info and Relationships", requires structure that is visible on screen to be expressed in code as well, and th with scope is how a table meets it. Tools such as axe and Lighthouse can flag data tables without headers, but only a person can judge whether a caption is meaningful.

In sortable dashboard tables, each column header contains a button, and the th of the currently sorted column carries aria-sort="ascending" or "descending". That way the meaning of the little arrow that sighted users see reaches screen reader users too.

Common mistakes

Common mistakes

1. Writing row headers as td

Making the column headers th but leaving the first cell of each row (a month or product name) as td is the most common slip. Data cells are then paired only with their column header, and which row they belong to is lost. Change that cell to <th scope="row">.

2. Using a heading outside the table instead of caption

An <h3>Monthly sales</h3> placed above the table does not name it; the table stays unnamed in the table list. Keep the heading if the page outline needs it, but also add a caption inside the table (or point aria-labelledby at the heading).

3. Putting scope on td

scope is not allowed on td (it is obsolete there), so <td scope="row"> does not create a header. Change the cell to th; only then does scope take effect.

Best practices

Best practices

  • Keep the caption to a few words: what the data is, and for which period or group.
  • Avoid abbreviations in headers; if you must use one, give the full form with the abbr element.
  • Listen to the table yourself at least once with a screen reader (NVDA on Windows, VoiceOver on Mac) using its table navigation keys.
  • Never carry meaning with colour alone ("red cells are overdue"); add text or an icon as well.

Mini challenge

Mini challenge: a class timetable

Write a Grade 9 timetable with three days and three time slots. A screen reader user must hear every cell together with both its day and its time slot.

  • A caption that states the table's purpose
  • A top row with "Day" and three time slots (9:00, 10:00, 11:00) as th scope="col"
  • Monday, Tuesday and Wednesday as th scope="row"
  • No empty cells: write "Free period" where there is no class
timetable.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Timetable</title>
</head>
<body>
  <table>
    <!-- TODO: caption -->
    <thead>
      <!-- TODO: one row: Day + three time-slot column headers -->
    </thead>
    <tbody>
      <!-- TODO: three rows, each starting with a day row header -->
    </tbody>
  </table>
</body>
</html>

Expected: Under the caption "Grade 9 timetable, Monday to Wednesday" come a header row Day | 9:00 | 10:00 | 11:00 and one row per day. In the accessibility tree the four top cells are column headers and the day names are row headers, so Tuesday's 10:00 cell ("Free period") is tied to both Tuesday and 10:00.

Show hint

Every row has four cells: one th for the day and three td for the slots. The top-left "Day" cell points down its column of days, so it gets scope="col" too.

Show solution
timetable.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Timetable</title>
  <style>
    table { border-collapse: collapse; }
    th, td { border: 1px solid #999; padding: 0.3rem 0.6rem; }
    th[scope="row"] { text-align: left; }
  </style>
</head>
<body>
  <table>
    <caption>Grade 9 timetable, Monday to Wednesday</caption>
    <thead>
      <tr>
        <th scope="col">Day</th>
        <th scope="col">9:00</th>
        <th scope="col">10:00</th>
        <th scope="col">11:00</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th scope="row">Monday</th>
        <td>Maths</td><td>English</td><td>Science</td>
      </tr>
      <tr>
        <th scope="row">Tuesday</th>
        <td>History</td><td>Free period</td><td>Maths</td>
      </tr>
      <tr>
        <th scope="row">Wednesday</th>
        <td>Science</td><td>Art</td><td>English</td>
      </tr>
    </tbody>
  </table>
</body>
</html>

Quick quiz 1 / 3

How should "January", the first cell of a row in a sales table, be marked up?

Quick quiz 2 / 3

Where must the caption element go?

Quick quiz 3 / 3

Which is the right way to tell screen reader users what a table is for?

What to remember

  • Give every data table a short caption, as the table's first child, that states its purpose.
  • Mark column headers with th scope="col" and row headers with th scope="row" so every td has both row and column context.
  • For two levels of headers use colgroup with scope="colgroup", and keep headers/id as a last resort.
  • Write headers that make sense when heard, avoid empty cells, and do not use the summary attribute.

Web Content Accessibility Guidelines (WCAG) 2.2 — W3C

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Table Caption & Scope | Thuta Learning