EthicsComputerCourses
Join training +91 90532 10052

Table Headers & Captions

HTML · Lesson 27 of 234 Beginner 8 min read Updated

By the end of this lesson you will be able to label any data table so that a screen reader user hears the right heading with every number. You will use <caption>, all four values of scope, and the headers attribute for tables that scope cannot describe.

HTML Tables Fundamentals introduced <th>, scope="col" and scope="row". Here you will see why they matter and how to handle harder tables.

Why header cells matter

Imagine Rohan is reading a mobile recharge chart to his grandmother over the phone. If he only says "28, 56, 84", she has no idea what those numbers mean. He has to say "the ₹299 plan, validity, 28 days". The heading and the value must travel together.

A sighted user does this with their eyes: they glance up to the column heading and left to the row heading. A screen reader user hears one cell at a time. The screen reader can only announce the right headings if your HTML says which cells are headings and what they apply to.

That is the job of <th> (table header cell) and its scope attribute. Here is roughly what a screen reader such as NVDA says while moving right along a row, with and without proper headers:

Text
Without th:   "28"   "56"   "84"
With th:      "₹299 plan, 28 days"   "₹449 plan, 56 days"   "₹599 plan, 84 days"

In NVDA, a free screen reader for Windows, you move from cell to cell with Ctrl+Alt+arrow keys. When you enter a table it usually announces the caption and the number of rows and columns.

The caption: a title for the table

<caption> gives the table its title. It must be the first child of <table>. Because it belongs to the table, assistive technology links the two, which a heading placed above the table cannot do as reliably.

A good caption says what the data is and, if useful, when or where it applies: "Prepaid plans, November 2026" is better than "Plans". By default the caption appears above the table. CSS caption-side: bottom moves it below without changing the HTML.

One exception: if a table is the only content of a <figure>, use the figure's <figcaption> instead and leave out <caption>.

The four values of scope

scope tells the browser which cells a header cell describes:

ValueThe header applies to
colthe cells below it in its column
rowthe cells to its right in its row
colgroupevery column in its column group
rowgroupthe rest of the rows in its row group (<thead>, <tbody> or <tfoot>)

If you leave scope out, the browser uses an "auto" mode and guesses from the position of the cell. The guess is often right for simple tables, but writing scope removes the doubt.

Step 1: a simple comparison table. Plans run across the top, so those headers get scope="col". Features run down the left, so those get scope="row".

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Recharge Plans</title>
  <style>
    table { border-collapse: collapse; }
    th, td { border: 1px solid #888; padding: 6px 12px; }
    caption { font-weight: bold; padding-bottom: 6px; }
    thead th { background: #e3f0ff; }
  </style>
</head>
<body>
  <table>
    <caption>Prepaid plans, November 2026</caption>
    <thead>
      <tr>
        <td></td>
        <th scope="col">₹299 plan</th>
        <th scope="col">₹449 plan</th>
        <th scope="col">₹599 plan</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th scope="row">Validity</th>
        <td>28 days</td><td>56 days</td><td>84 days</td>
      </tr>
      <tr>
        <th scope="row">Data per day</th>
        <td>1.5 GB</td><td>2 GB</td><td>2 GB</td>
      </tr>
      <tr>
        <th scope="row">Calls</th>
        <td>Unlimited</td><td>Unlimited</td><td>Unlimited</td>
      </tr>
    </tbody>
  </table>
</body>
</html>

You should see three plan columns with blue headings and three feature rows. The empty top-left corner is a <td>, because it labels nothing.

Step 2: headings over groups. Now a marks sheet with two terms. "Term 1" sits above two columns, so it gets scope="colgroup", and matching <colgroup> elements tell the browser where each group starts. "Section A" heads a whole <tbody>, so it gets scope="rowgroup". The colspan and rowspan attributes stretch cells across columns or rows; they are explained in Table Merging: Colspan & Rowspan.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Two Terms</title>
  <style>
    table { border-collapse: collapse; text-align: center; }
    th, td { border: 1px solid #888; padding: 6px 10px; }
    th[scope="rowgroup"] { background: #f4f4f4; text-align: left; }
  </style>
</head>
<body>
  <table>
    <caption>Class 10 science stream results</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">Physics</th>
        <th scope="col">Maths</th><th scope="col">Physics</th>
      </tr>
    </thead>
    <tbody>
      <tr><th scope="rowgroup" colspan="5">Section A</th></tr>
      <tr><th scope="row">Aarav</th><td>78</td><td>81</td><td>85</td><td>88</td></tr>
      <tr><th scope="row">Meera</th><td>91</td><td>87</td><td>94</td><td>90</td></tr>
    </tbody>
    <tbody>
      <tr><th scope="rowgroup" colspan="5">Section B</th></tr>
      <tr><th scope="row">Kabir</th><td>69</td><td>74</td><td>72</td><td>80</td></tr>
    </tbody>
  </table>
</body>
</html>

Now the cell "94" can be announced as "Section A, Meera, Term 2, Maths, 94". Every heading that applies is known.

headers and id for irregular tables

Sometimes headings do not sit in a neat line above or beside the data. In the table below, sub-headings such as "Travel" appear in the middle of one <tbody>. For cases like this, give each <th> an id, and list the ids that apply to a cell in its headers attribute, separated by spaces.

HTML
<table>
  <caption>Meera's Diwali trip costs</caption>
  <tr><td></td><th id="jaipur">Jaipur</th><th id="pune">Pune</th></tr>
  <tr><th id="travel" colspan="3">Travel</th></tr>
  <tr>
    <th id="train" headers="travel">Train</th>
    <td headers="jaipur travel train">₹1,450</td>
    <td headers="pune travel train">₹2,100</td>
  </tr>
  <tr><th id="stay" colspan="3">Stay</th></tr>
  <tr>
    <th id="hotel" headers="stay">Hotel</th>
    <td headers="jaipur stay hotel">₹3,200</td>
    <td headers="pune stay hotel">₹4,500</td>
  </tr>
</table>

The rules are simple. Each id in headers must belong to a <th> in the same table, and a cell cannot list itself. headers overrides any guessing, so it is precise, but it is tedious to keep up to date. Use scope for most tables, and reach for headers only when scope cannot express the structure. If a table needs dozens of headers values, consider splitting it into two simpler tables.

A <th> can also carry an abbr attribute with a shorter label, such as <th scope="col" abbr="Qty">Quantity in kilograms</th>. Some screen readers use it when they repeat the heading for each cell.

Common mistakes

  • Bold <td> cells used as headings. They look like headers but carry no meaning. Use <th>.
  • A heading placed above the table instead of a caption. It is not tied to the table. Use <caption>.
  • scope="colgroup" without <colgroup> elements. The browser needs to know where the group starts and ends.
  • headers pointing at a <td> or a misspelt id. The link silently fails, so check every id.

Interview questions

1. What are the valid values of the scope attribute, and what does each mean?

col and row make a header apply to its column or its row. colgroup makes it apply to all columns in its column group, and rowgroup to the remaining rows of its <thead>, <tbody> or <tfoot>. Without scope, the browser falls back to an automatic mode based on position.

2. When would you use headers and id instead of scope?

When header cells do not sit in a straight line with the data, such as sub-heading rows inside a single <tbody> or very irregular layouts. Each data cell lists the ids of every <th> that describes it. For normal tables scope is simpler and easier to maintain.

3. Why use <caption> instead of a heading above the table?

<caption> is part of the table, so assistive technology announces it as the table's name when the user reaches it. A heading above is just nearby text. The caption must be the first child of <table>.

4. What is the status of the summary attribute?

It is non-conforming in the current HTML standard. Its text was hidden from sighted users, which helped nobody else. Today you describe a table in its caption or in visible text near it.

5. How does a screen reader use <th> cells?

As the user moves from cell to cell, the screen reader announces the column header when the column changes and the row header when the row changes, followed by the value. Without <th>, the user hears bare values with no context.

Key points

  • Use <th> for every cell that labels other cells, and <td> for values and empty corners.
  • <caption> is the first child of <table> and names the table for everyone.
  • scope takes col, row, colgroup or rowgroup; write it rather than relying on auto mode.
  • scope="colgroup" needs matching <colgroup> elements.
  • headers lists ids of <th> cells for irregular tables; use it only when scope is not enough.
  • summary and axis are obsolete; describe tables with visible text.

Exercise

Make a table of the canteen menu at a Pune college for two meals (Breakfast and Lunch) across three days (Monday, Tuesday, Wednesday). Give it a caption. Use scope="col" for the day headings and scope="row" for the meal headings. Then add a <tfoot> row whose heading cell says "Price" with scope="row", and a price for each day.

Show solution
HTML
<table>
  <caption>Canteen menu, Block C, week of 16 November</caption>
  <thead>
    <tr>
      <td></td>
      <th scope="col">Monday</th>
      <th scope="col">Tuesday</th>
      <th scope="col">Wednesday</th>
    </tr>
  </thead>
  <tbody>
    <tr><th scope="row">Breakfast</th><td>Poha</td><td>Idli sambar</td><td>Upma</td></tr>
    <tr><th scope="row">Lunch</th><td>Rajma chawal</td><td>Veg thali</td><td>Chole bhature</td></tr>
  </tbody>
  <tfoot>
    <tr><th scope="row">Price</th><td>₹90</td><td>₹110</td><td>₹100</td></tr>
  </tfoot>
</table>

Each value now has a day heading above it and a meal or price heading to its left, so a screen reader can announce, for example, "Tuesday, Lunch, Veg thali".

Quick Quiz

1. Where must <caption> be placed?

2. A <th> sits above two columns that share one <colgroup>. Which scope fits?

3. What must each id listed in a cell's headers attribute point to?

4. What should replace the old summary attribute?

Learn HTML with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call