日本語

How to Fix Lighthouse “tabindex greater than 0” Warning

​​Lighthouse​Accessibility,HTML,Playwright

The Lighthouse audit "Some elements have a [tabindex] value greater than 0" (the Japanese UI shows a translated version of this message) goes away once you remove every tabindex with a value of 1 or more. If you added tabindex="1" so that the Tab key lands on the search box first, the focus order no longer matches the visual layout for people who navigate with the keyboard.

On a test page, I compared the Tab order and scores before and after the fix. I also checked when tabindex="0" and tabindex="-1" are appropriate, and whether a tabindex on an element that is hidden at page load gets flagged.

Note

The test environment was Ubuntu 24.04 with Node.js 22. I ran Lighthouse 13.6.0 (which uses axe-core 4.14.0 internally) from the command line with the default settings (mobile), limited to the accessibility category. I pointed Lighthouse at Chrome for Testing 153, which ships with Playwright 1.63.0, through CHROME_PATH. For the user flow that inspects the state after interactions, I used puppeteer-core 25.18.1. The Tab order was checked with Playwright (chrome-headless-shell 157) by pressing Tab and printing the element that had focus. The server was a small one written with Node.js's http module, running on localhost on the same machine. I did not test the Lighthouse panel in DevTools, PageSpeed Insights, screen readers, Safari or Firefox, or Lighthouse 12 and earlier.

Any single element with a value of 1 or more fails the audit

This audit uses the result of the tabindex rule in axe-core, which Lighthouse runs internally. In the axe-core 4.14.0 code, it collects elements that have a tabindex attribute, reads the leading number of the value, and passes the element if the number is 0 or less and fails it if the number is 1 or more.

With the Lighthouse 13.6.0 settings, this audit is in the "Navigation" group under Accessibility and has a weight of 7. On the test page, when only this audit failed, the accessibility score was 94.

Adding tabindex="1" to the search page and checking for the warning

I built a product search page with a logo and menu links in the header, and a search form below them. To make the Tab key reach the search box first, I gave the search box tabindex="1", the search button tabindex="2", and the "New arrivals" link tabindex="3".

html
<header>
<a href="/" class="logo">ねこ雑貨店</a>
<nav><a href="/news">お知らせ</a> <a href="/help">ヘルプ</a></nav>
</header>
<main>
<h1>商品をさがす</h1>
<form role="search" action="/search">
<label for="q">キーワード</label>
<input id="q" name="q" type="search" tabindex="1">
<button tabindex="2">検索</button>
</form>
<p><a href="/new" tabindex="3">新着の商品</a> <a href="/sale">セールの商品</a></p>
</main>
<footer><a href="/contact">お問い合わせ</a></footer>

I ran Lighthouse with Chrome 153 specified through CHROME_PATH, then pulled out the relevant audit from the JSON result with a small script (summarize.mjs).

console
$ npx lighthouse http://localhost:8400/broken --only-categories=accessibility --output=json --output-path=./out/broken.json --chrome-flags="--headless=new --no-sandbox" --quiet
exit: 0
$ node summarize.mjs out/broken.json
lighthouseVersion: 13.6.0  formFactor: mobile  locale: en-US
url: http://localhost:8400/broken
accessibility score: 94
tabindex: score=0 scoreDisplayMode=binary title="Some elements have a `[tabindex]` value greater than 0"
  in accessibility: weight=7 group=a11y-navigation
  failing: <input id="q" name="q" type="search" tabindex="1">  (body > main > form > input#q)
  failing: <button tabindex="2">  (body > main > form > button)
  failing: <a href="/new" tabindex="3">  (body > main > p > a)
  other failed binary audits: none

The three elements with a tabindex were listed as the failing elements.

When I opened this page and pressed Tab repeatedly, focus moved as follows. taborder.mjs is a Playwright script that prints the focused element each time Tab is pressed.

console
$ node taborder.mjs http://localhost:8400/broken 10
browser: 157.0.8103.78  url: http://localhost:8400/broken
Tab  1: <input> キーワード  [tabindex=1]
Tab  2: <button> 検索  [tabindex=2]
Tab  3: <a> 新着の商品  [tabindex=3]
Tab  4: <a> ねこ雑貨店
Tab  5: <a> お知らせ
Tab  6: <a> ヘルプ
Tab  7: <a> セールの商品
Tab  8: <a> お問い合わせ
Tab  9: (body)
Tab 10: <input> キーワード  [tabindex=1]

The search box did receive focus first, as intended. However, after "New arrivals" at step 3, focus jumped back to the logo at the top of the page at step 4, went through the menu, and only then reached "Sale products", which comes right after "New arrivals" in the HTML. Focus did not move from top to bottom; it jumped back up to the logo partway through. (body) means no element has focus (document.activeElement is body).

As the MDN description of tabindex explains, elements with a value of 1 or more come first, in ascending order of their values. Elements without a tabindex, or with tabindex="0", come after them in the order they appear in the HTML. Only the elements that have a value are pulled to the front, regardless of their position on the page.

Remove the tabindex and let the HTML source order decide

I removed tabindex from the three elements.

diff
-<input id="q" name="q" type="search" tabindex="1">
-<button tabindex="2">検索</button>
+<input id="q" name="q" type="search">
+<button>検索</button>
 </form>
-<p><a href="/new" tabindex="3">新着の商品</a> <a href="/sale">セールの商品</a></p>
+<p><a href="/new">新着の商品</a> <a href="/sale">セールの商品</a></p>

Tab now moves from the logo downward, following the page from top to bottom.

console
$ node taborder.mjs http://localhost:8400/fixed 10
browser: 157.0.8103.78  url: http://localhost:8400/fixed
Tab  1: <a> ねこ雑貨店
Tab  2: <a> お知らせ
Tab  3: <a> ヘルプ
Tab  4: <input> キーワード
Tab  5: <button> 検索
Tab  6: <a> 新着の商品
Tab  7: <a> セールの商品
Tab  8: <a> お問い合わせ
Tab  9: (body)
Tab 10: <a> ねこ雑貨店

The screenshots below number each element in the order that focus actually reached it with the Tab key. The pink numbers mark the elements that had a tabindex of 1 or more.

Search page before the fix, with the Tab order shown as numbers. The search box is 1, the search button is 2, and "New arrivals" is 3. The logo at the top is 4, "Notices" is 5, "Help" is 6, "Sale products" is 7, and "Contact" is 8.
Before: numbers show the order in which Tab moved focus (1–3 are the elements with tabindex)
Search page after the fix, with the Tab order shown as numbers. The logo is 1, "Notices" is 2, "Help" is 3, the search box is 4, the search button is 5, "New arrivals" is 6, "Sale products" is 7, and "Contact" is 8, following the page from top to bottom.
After: numbers show the order in which Tab moved focus

On the fixed page, the accessibility score was 100. The audit did not become a pass (score=1); instead, its scoreDisplayMode became notApplicable, because no element on the page has a tabindex attribute, so there is nothing to check. Its weight also dropped to 0, so it does not count toward the score.

console
$ node summarize.mjs out/fixed.json
lighthouseVersion: 13.6.0  formFactor: mobile  locale: en-US
url: http://localhost:8400/fixed
accessibility score: 100
tabindex: score=null scoreDisplayMode=notApplicable title="No element has a `[tabindex]` value greater than 0"
  in accessibility: weight=0 group=a11y-navigation
  other failed binary audits: none

To change the order in which elements receive focus, change the order of the elements in the HTML, not their tabindex values. The MDN description also recommends using only 0 and -1 for tabindex, and writing elements in a sensible order in the source. The same page advises against using CSS properties such as order to rearrange the visual order of focusable elements.

0 and -1 are not flagged

To make an element that is not normally focusable reachable with the Tab key, such as a div used as a button, use tabindex="0". For an element you want to focus from a script with focus() but that should not stop when the user presses Tab, use tabindex="-1". I added both to the fixed page.

html
<div role="button" tabindex="0" id="fav">お気に入りに追加</div>
<p id="msg" tabindex="-1"></p>

The div button is set up in others.js so that it responds to a click, the Enter key, and the Space key. When it is activated, it writes the message "Added to favorites" into the paragraph and moves focus to that paragraph with focus().

The Lighthouse result was a pass (score=1), and the accessibility score was 100. Because there are elements with a tabindex attribute, the audit has something to check this time, and it counts toward the score with a weight of 7.

console
$ node summarize.mjs out/others.json
lighthouseVersion: 13.6.0  formFactor: mobile  locale: en-US
url: http://localhost:8400/others
accessibility score: 100
tabindex: score=1 scoreDisplayMode=binary title="No element has a `[tabindex]` value greater than 0"
  in accessibility: weight=7 group=a11y-navigation
  other failed binary audits: none

With Tab, the div with tabindex="0" stopped at its position in the HTML (right after the search button), and focus did not stop at the tabindex="-1" paragraph. The output below is only the excerpt for Tab presses 4 through 7.

console
$ node taborder.mjs http://localhost:8400/others 10
browser: 157.0.8103.78  url: http://localhost:8400/others
Tab  4: <input> キーワード
Tab  5: <button> 検索
Tab  6: <div> お気に入りに追加  [tabindex=0]
Tab  7: <a> 新着の商品

When I pressed Enter on the div button, focus moved to the paragraph. Pressing Tab from there went to "New arrivals", the element after the paragraph.

console
$ node minus1.mjs
Tab 6回: <div id=fav> お気に入りに追加
Enter: <p id=msg> お気に入りに追加しました
Tab: <a> 新着の商品

Elements that are hidden at page load can't be checked for tabindex until they are shown

I also tested a page where two checkboxes with tabindex="1" and tabindex="2" sat inside a panel that was hidden with the hidden attribute and appeared when the "Filter" button was pressed.

html
<button id="open" aria-expanded="false" aria-controls="filter">絞り込み</button>
<div id="filter" hidden>
<label><input type="checkbox" name="stock" tabindex="1"> 在庫あり</label>
<label><input type="checkbox" name="sale" tabindex="2"> セール中</label>
</div>

The Lighthouse result for this page was notApplicable, the same as the fixed page, and the accessibility score was 100. As described below, checking after the panel is opened does report the issue, so the check at page load did not include the hidden checkboxes.

console
$ node summarize.mjs out/hidden.json
lighthouseVersion: 13.6.0  formFactor: mobile  locale: en-US
url: http://localhost:8400/hidden
accessibility score: 100
tabindex: score=null scoreDisplayMode=notApplicable title="No element has a `[tabindex]` value greater than 0"
  in accessibility: weight=0 group=a11y-navigation
  other failed binary audits: none

However, after I pressed the "Filter" button to open the panel and then pressed Tab, focus skipped the newly shown checkboxes and went to "New arrivals". The checkboxes were reached only after focus had gone past the last element on the page and wrapped back to the top.

console
$ node taborder.mjs http://localhost:8400/hidden 6 '#open'
browser: 157.0.8103.78  url: http://localhost:8400/hidden
(#open をクリックしてから Tab を押す)
Tab  1: <a> 新着の商品
Tab  2: <a> セールの商品
Tab  3: <a> お問い合わせ
Tab  4: (body)
Tab  5: <input> 在庫あり  [tabindex=1]
Tab  6: <input> セール中  [tabindex=2]

As noted earlier, elements with a value of 1 or more come before elements without a tabindex. So even though the panel sat right after the "Filter" button in the HTML, focus did not go from the button to the checkboxes. When I removed the tabindex attributes, focus moved from the button to the checkboxes.

console
$ node taborder.mjs http://localhost:8400/hidden-fixed 6 '#open'
browser: 157.0.8103.78  url: http://localhost:8400/hidden-fixed
(#open をクリックしてから Tab を押す)
Tab  1: <input> 在庫あり
Tab  2: <input> セール中
Tab  3: <a> 新着の商品
Tab  4: <a> セールの商品
Tab  5: <a> お問い合わせ
Tab  6: (body)

To check the state of a page after interactions such as a button press with Lighthouse, use the snapshot mode described in the Lighthouse user flows documentation. Snapshot mode audits the page at a given moment, and the docs suggest it for menus and other content that appears after an interaction. With Node.js, I passed a page opened with puppeteer-core to Lighthouse's startFlow(), loaded the page, pressed the "Filter" button, and ran flow.snapshot(). The following code is an excerpt from snapshot.mjs.

js
await flow.navigate(process.argv[2]);
await page.click('#open');
await flow.snapshot({ name: '絞り込みを開いた後' });

In the state after the panel was opened, both checkboxes were flagged, and the accessibility score was 96. On the page with the tabindex attributes removed, there were no flags even after the panel was opened.

console
$ node snapshot.mjs http://localhost:8400/hidden
navigation: accessibility=100 tabindex score=null (notApplicable)
snapshot: accessibility=96 tabindex score=0 (binary)
  failing: <input type="checkbox" name="stock" tabindex="1">
  failing: <input type="checkbox" name="sale" tabindex="2">
$ node snapshot.mjs http://localhost:8400/hidden-fixed
navigation: accessibility=100 tabindex score=null (notApplicable)
snapshot: accessibility=100 tabindex score=null (notApplicable)
Point

A page can pass Lighthouse when it loads and still contain values of 1 or more in panels or menus that open on a button press. Search your source for tabindex (tabIndex in JavaScript or JSX) and remove every value of 1 or more.

​Posted by 86