日本語

Fixing Lighthouse Table Header (th) Warnings

​​Lighthouse​Accessibility,HTML

Lighthouse's accessibility audits report two kinds of table header problems. A warning that a <th> has no data cells below it (or to its right) goes away when you add the missing <td>. A warning that a <td>'s headers attribute does not refer to a header in the same table goes away when you change the headers value to match the header's id. These audits exist so that screen readers can read tables built with HTML <table>, such as price lists, record lists, and comparison tables.

On the page I tested, both warnings appeared, and the accessibility score was 89. After I fixed them, the score was 100. However, only the headers warning lowered the score. The <th> warning does not count toward the score, so you will miss it if you only look at the score.

Note

The test environment was Ubuntu 24.04, Node.js 22, and Lighthouse 13.6.0 (the npm package; the axe-core it uses is 4.14.0). I ran Chrome for Testing 158, which is bundled with Playwright 1.63.0, by setting the CHROME_PATH environment variable. I ran Lighthouse from the command line with the default mobile settings and checked only the accessibility category. I served the test pages from a server running in the test environment (localhost:8080). I did not test the Lighthouse panel in Chrome DevTools, PageSpeed Insights, how to choose between scope attribute values, or how screen readers actually read the tables.

The page with the warnings, and the Lighthouse output

The page I tested shows two tables. The first table has a "memo" column in its header, but its data rows have only two <td> cells. The second table uses headers attributes to assign each cell its header, but morning is misspelled as moring.

html
<table class="weight">
  <tr><th>名前</th><th>体重</th><th>メモ</th></tr>
  <tr><td>たま</td><td>4.2kg</td></tr>
  <tr><td>みけ</td><td>3.8kg</td></tr>
</table>
<table class="food">
  <tr><th id="name">名前</th><th id="morning">朝</th><th id="night">夜</th></tr>
  <tr><td headers="name">たま</td><td headers="moring">40g</td><td headers="night">45g</td></tr>
</table>

I ran Lighthouse on this page and used a small script (summarize.mjs) to pull the table-related audits out of the JSON result.

console
$ npx lighthouse http://localhost:8080/before.html --only-categories=accessibility --output=json --output=html --output-path=./out/before --chrome-flags="--headless=new --no-sandbox" --quiet
$ node summarize.mjs out/before.report.json
lighthouseVersion: 13.6.0  formFactor: mobile  locale: en-US
requestedUrl: http://localhost:8080/before.html
accessibility score: 89
th-has-data-cells: score=1 scoreDisplayMode=informative title="`<th>` elements and elements with `[role="columnheader"/"rowheader"]` have data cells they describe."
  <table class="weight"> | Fix all of the following: Table data cells are missing or empty
    - <th>
td-headers-attr: score=0 scoreDisplayMode=binary title="Cells in a `<table>` element that use the `[headers]` attribute refer to an element `id` not found within the same table."
  <table class="food"> | Fix all of the following: The headers attribute is not exclusively used to refer to other header cells in the table
    - <td headers="moring">

(In practice, I ran this from a small shell script after setting CHROME_PATH. I left out the exit code line, three table audits that do not affect the score, and the two lines that list each audit's weight and the audits that lowered the score.)

td-headers-attr is the headers attribute audit, and it failed (score=0). th-has-data-cells is the header cell audit. It lists the <th> in the "memo" column, but score=1, and its title is the one used when the audit passes. When I added --locale=ja, the titles changed to Japanese. Their English meaning is the same as the English titles above. The "Fix all of the following:" text stayed in English.

In the HTML report, both audits appear in the "Tables and lists" group. The headers audit has a red triangle. The header cell audit has a gray circle and the label "Unscored." When you expand either one, the problem table and cells appear under "Failing Elements."

The Tables and lists group in the Lighthouse report. The first audit has a red triangle and is about the headers attribute, listing td headers="moring". The second audit has a gray circle and the label Unscored, and is about the th element, listing table class="weight" and the th for memo.
The report for the page before the fix. The header cell warning appears as Unscored.

Fixing the misspelled headers value

The headers attribute takes the id of the <th> that is the header for that cell, separated by spaces (MDN's td element reference). This was a typo, so I changed moring to morning.

html
<tr><td headers="name">たま</td><td headers="morning">40g</td><td headers="night">45g</td></tr>

This audit has a weight of 7 and counts toward the score. The score of 89 before the fix was due to this audit alone failing.

I also checked, one page at a time, the cases where the headers value was wrong for other reasons. All of them gave a score of 89, but the explanation text changed.

headers value on a <td> Explanation text
The id of a <th> in a different table The headers attribute is not exclusively used to refer to other header cells in the table
The id of the cell itself The element with headers attribute refers to itself
The id of a <td> in the same table The headers attribute must refer to header cells, not data cells

The explanation text tells you where to look. If the id cannot be found (because of a typo, or because it points to a different table), change the value to the id of a <th> in the same table. If it points to a data cell, either change that cell to a <th> or change the value to point to a header cell.

A cell with an empty value, such as headers="", did not fail this audit. Its score was 100.

Fixing a header with no data cells

Adding the <td> for the "memo" column cleared the header cell warning. An empty <td></td> is fine.

html
<table class="weight">
  <tr><th>名前</th><th>体重</th><th>メモ</th></tr>
  <tr><td>たま</td><td>4.2kg</td><td></td></tr>
  <tr><td>みけ</td><td>3.8kg</td><td>少し増えた</td></tr>
</table>

I ran Lighthouse on the page with both the misspelling and the missing cells fixed. Here are the relevant lines, copied from the output:

console
accessibility score: 100
th-has-data-cells: score=null scoreDisplayMode=notApplicable title="`<th>` elements and elements with `[role="columnheader"/"rowheader"]` have data cells they describe."
td-headers-attr: score=1 scoreDisplayMode=binary title="Cells in a `<table>` element that use the `[headers]` attribute refer to table cells within the same table."

After the fix, the header cell audit showed "Not applicable" (notApplicable). A table where both <td> cells in the "memo" column were empty also showed "Not applicable," and its score was 100.

Why the header cell warning does not count toward the score

In Lighthouse 13.6.0's config file, th-has-data-cells has the same weight of 7 as td-headers-attr. It still does not count toward the score because the audit's source code sets its display mode to "informative." The scoring code sets the weight of informative audits to 0 before it calculates the score. In the JSON output, I confirmed that the weight used in the score calculation was 0.

The underlying axe-core check also does not state a clear pass or fail. This check in axe-core 4.14.0 returns a result that needs a person to review (undefined), not a failure (false), when a header has no data cells. Lighthouse shows this result without counting it toward the score.

Point

The score can be 100 even when the header cell audit still shows a warning. In the empty <tbody> table in the next section, the score was 100, but the report still showed this audit as "Unscored," listing the <table> and <th> under "Failing Elements." Check the "Tables and lists" group as well as the score.

Table layouts that trigger the header cell warning, and those that do not

I also checked other table layouts, one page at a time. All of them scored 100.

Table layout Header cell audit
Only a header row, with an empty <tbody> Warning (the <th> is listed)
A header row and a row with colspan="2" that says "No records yet" Not applicable
A table where every cell is a <th> Warning
Each row starts with a <th>, followed by a <td> to its right Not applicable

A table that shows only its header row, such as one shown before the data loads or one with zero records, gets a warning. Adding a row with colspan that says something like "No records yet" cleared the warning. A <th> that works as a row header is fine as long as a <td> follows it on the right.

I also tested a table where the first row contained a <th> with colspan as the table title. The header cell audit showed "Not applicable," but a different audit, table-fake-caption, failed with the message "The first child of the table should be a caption instead of a table cell." This audit has a weight of 0, does not appear in the HTML report, and could only be seen in the JSON output. As the explanation text says, Lighthouse expects a table title to be written with <caption>.

​Posted by 86