References

Beginner-friendly references for web development, with live, editable examples.

The HTML headers attribute

Attribute All modern browsers
In short

The HTML headers attribute associates a data cell with its header cells by their ids, for accessibility in complex tables. It is used on the <td> and <th> elements.

Overview

The headers attribute links data cells to header cells for accessibility. It is used on the <td> and <th> elements.

It defines table structure: spanning cells across rows or columns, and connecting data cells to their headers. These attributes are central to making data tables understandable to screen-reader users.

Syntax

<th id="q1">Q1</th> … <td headers="q1">100</td>

Values

Value
A space-separated list of header cell ids.

Best practices

  • Once a cell has a headers attribute, the table model builds its header list from those ids alone, ignoring scope and the cell's position. List each header that applies, row and column headers alike.
  • Point each id at a <th> in the same table, as the standard requires. An id that names a <td> breaks that rule, and an id for an element outside the table adds no header at all.
  • Header references must not loop. A <th> must not list its own id, and no chain of headers references may lead back to the cell it started from.
  • A <th> whose own headers list points at a broader heading doesn't pass that link on to data cells. Add the broader heading to each data cell's list as well.
  • The headers DOM property returns the attribute as one string, spacing included, not as a token list. Split it on whitespace to read the ids, and assign a whole new string to change them.

Accessibility

In complex tables where scope is not enough, headers explicitly ties each data cell to its header cells so screen readers announce the right context.

Frequently asked questions

What does the headers attribute do?
Links data cells to header cells for accessibility.
How do I merge table cells?
Use colspan to span columns and rowspan to span rows.
How do I make a data table accessible?
Use <th> headers with scope, add a <caption>, and link cells to headers with headers in complex tables.
Which elements use the headers attribute?
It is an element-specific attribute, used on the <td> and <th> elements.