CHAPTER 29 · HTML

Accessible tables: clear header relationships and usable small-screen views

Help people understand a table when they use a small screen, keyboard or screen reader. Start with simple relationships, learn explicit header references when needed, and make a wide table scroll without hiding its data.

Download example files (ZIP)

Ask what each value means

Imagine hearing only the number 4 from a practice report. Four what? Planned examples, checked examples or a room number? A useful table lets a person connect each value with its relevant row and column labels.

Accessibility means making content usable by people with different needs and ways of interacting. A screen reader is software that presents interface and document information through speech or other output. It can use table relationships; visual position alone is not enough to express all of them.

Start with a caption, real th headers, useful wording and scope="row" or scope="col" where appropriate. Do not add complex attributes to a simple table merely to make it look advanced.

Explicit links between a cell and its headers

Less regular tables can need explicit associations. Give the relevant th elements unique ids, then put a space-separated list of those ids in a data cell's headers attribute.

HTML fragment — part of a document
<th id="planned" scope="col">Planned examples</th>
<th id="lists-topic" scope="row">Lists</th>
<td headers="lists-topic planned">4</td>

This is a syntax fragment, not one complete row. The headers values contain ids without #. They must refer to th elements in the same table. The attribute does not create visible labels or change the displayed number.

Complete program: named-table-headers.html

This small table intentionally makes the relationships explicit for practice. Scope alone would normally be enough for its simple shape. Learn the mechanism here before using it to maintain a more complex table.

named-table-headers.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Read Each Practice Count in Context</title>
</head>
<body>
  <h1>Connect every count with its labels</h1>
  <table>
    <caption>Practice counts with explicit header references</caption>
    <thead>
      <tr>
        <th id="topic-column" scope="col">Topic</th>
        <th id="planned-column" scope="col">Planned examples</th>
        <th id="checked-column" scope="col">Checked examples</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th id="lists-row" scope="row">Lists</th>
        <td headers="lists-row planned-column">4</td>
        <td headers="lists-row checked-column">3</td>
      </tr>
      <tr>
        <th id="links-row" scope="row">Links</th>
        <td headers="links-row planned-column">3</td>
        <td headers="links-row checked-column">3</td>
      </tr>
    </tbody>
  </table>
</body>
</html>
Browser previewDocument title: Read Each Practice Count in Context

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result: an ordinary-looking table. For the first value 4, read the ids listed in its headers: lists-row and planned-column. Find those exact th elements, then read their words: Lists and Planned examples. Repeat the exercise for all four values.

For genuinely multi-level tables, a value might refer to a broader category header, a smaller category header and a row header. Keep those references accurate as the table changes. Often the better solution is to split a difficult table into simpler tables with meaningful captions.

A caption and a description are different

A caption names the table. A nearby paragraph can explain units, abbreviations, unusual layout or how to read it. Do not cram an entire instruction manual into the caption. The obsolete table summary attribute is not the modern place for essential instructions.

If a table needs a description relationship, aria-describedby can refer to the id of an explanatory paragraph. ARIA attributes provide additional accessibility information where native HTML alone does not express what is needed. They do not repair wrong values, missing headers or an invalid grid. Prefer native table elements first.

Give a wide table its own scrolling space

A wide table should not force the whole article far off a small screen. Wrap it in a div with overflow-x: auto. A div is a generic grouping container; a class gives us a way to target that container with CSS.

Overflow-x controls extra horizontal content. Auto allows horizontal scrolling when needed. It does not delete columns. Min-width on the table keeps a readable minimum width for this demonstration, so narrower containers will need to scroll.

Make every value reachable and understandable
  1. 1Caption: name the table
  2. 2Headers: explain each value
  3. 3Scroll container: preserve wide content
  4. 4Keyboard focus: reach and operate that container

Explicit headers values refer to th ids without a #. Check the experience instead of relying on appearance alone.

Complete program: small-screen-table.html

small-screen-table.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>A Scrollable Practice Timetable</title>
  <style>
    .table-window { overflow-x: auto; max-width: 100%; }
    .table-window:focus-visible { outline: 3px solid #31573d; outline-offset: 3px; }
    table { border-collapse: collapse; min-width: 560px; }
    th, td { border: 1px solid #687466; padding: 10px; text-align: left; }
  </style>
</head>
<body>
  <h1>Read all the timetable columns</h1>
  <p id="table-help">On a narrow screen, scroll the table sideways. Keyboard users can focus its container and use the arrow keys.</p>
  <div class="table-window" tabindex="0" role="region" aria-labelledby="plan-caption" aria-describedby="table-help">
    <table>
      <caption id="plan-caption">Practice sessions and preparation</caption>
      <thead>
        <tr><th scope="col">Session</th><th scope="col">Room</th><th scope="col">Start</th><th scope="col">Bring</th></tr>
      </thead>
      <tbody>
        <tr><th scope="row">HTML notice</th><td>Room 6</td><td>10 am IST</td><td>Notebook and saved example</td></tr>
        <tr><th scope="row">Link practice</th><td>Room 8</td><td>11 am IST</td><td>Two linked HTML files</td></tr>
      </tbody>
    </table>
  </div>
</body>
</html>
Browser previewDocument title: A Scrollable Practice Timetable

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result: a four-column table. When the available space is below its minimum width, the table's container can scroll sideways while the rest of the page remains readable. On a wide view there may be no horizontal overflow, so no scrollbar is needed. Some systems hide scrollbars until scrolling begins.

Explain the wrapper's attributes

Tabindex="0" includes the wrapper in the normal keyboard focus order. It does not put every table cell in the Tab sequence. Ordinary data cells usually do not need individual tab stops.

Role="region" identifies the scroll area as a named region. Aria-labelledby refers to the caption's id to name it; aria-describedby refers to the paragraph that explains scrolling. These id references have no #. The table itself keeps its native table role: we have not replaced it with an application-style grid.

The :focus-visible selector draws the outline when keyboard focus needs to be shown. Outline-offset moves that outline slightly away from the container edge. Do not remove focus indication just because it changes the appearance.

What to check manually

Read the caption and headers before individual values. Use Tab to reach the scroll area, then test arrow-key scrolling when it overflows. Check touch or trackpad scrolling where available. Keep all columns available rather than hiding important values to make a screenshot fit.

If you test with a screen reader, use that tool's own table-navigation commands; Tab alone does not read every cell. The exact announcements vary by browser and reading tool. Correct markup is the starting point, not a promise of identical speech everywhere.

Problem and solution: the last column cannot be reached

Problem: a container clips overflow with hidden and has no working scrolling route, so small-screen readers lose the final column.

Solution: use a tested overflow-x: auto container, supply a useful name and focus route when needed, and preserve readable text. Test reaching the last column. A clean-looking edge is not success if data has disappeared.

Common mistakes and useful tips

  • Using headers="#planned" rather than an id value without #.
  • Referring to an id on a paragraph instead of a th in the same table.
  • Renaming header ids without updating dependent cells.
  • Adding role="grid" without implementing grid keyboard behaviour.
  • Hiding columns or shrinking all text to avoid scrolling.
  • Making every ordinary cell a tab stop.
  • Claiming an accessibility audit based only on visual alignment.

Practice questions

  1. What information does headers="lists-row planned-column" supply?
  2. Does headers visibly print those labels inside the cell?
  3. When might splitting a complex table be better than adding more header references?
  4. What does overflow-x: auto do?
  5. Why give the scroll container a focus route and visible outline?
  6. Test small-screen-table.html at a narrow width. Reach its last column with a pointer and keyboard, then trace every headers reference in the first program to a matching th.
Check your practice answers
  1. It explicitly identifies the header cells that label that data cell.
  2. No. It describes associations for tools; the visible value stays the same.
  3. When simpler separate tables express the information more clearly and are easier to maintain and navigate.
  4. It allows horizontal scrolling when content exceeds the container's width.
  5. Keyboard users need a way to reach and operate the overflowing area and see where focus is.
  6. Reduce the browser width until scrolling is needed. Check that Bring remains reachable. In the first program, each referenced id must identify the intended th within the same table.

Interview questions

When would you use headers instead of relying only on scope?

When a less regular or multi-level table needs explicit relationships for individual cells. Use document-unique header ids and accurate references, or simplify the table if that would be clearer.

Does a responsive table have to hide columns?

No. A horizontal scroll container can retain all columns. Choose a presentation that preserves meaningful relationships and keeps the information reachable.

Why is a native table different from an ARIA grid?

A native table presents data with built-in table semantics. A grid describes an interactive pattern with additional keyboard expectations. Do not change the role simply to make data look more advanced.

Does adding ARIA guarantee accessibility?

No. It can express additional relationships but cannot repair wrong data, an invalid table or unusable interactions. Test the actual reading and navigation experience.

Quick revision

  • Keep native table meaning and simple headers where possible.
  • Explicit headers values refer to th ids in the same table.
  • A wide table can scroll without losing columns.
  • Test focus, reading order and access to the final column.