You Asked

Keyboard trap: does a clickable heading that Tab skips count?

6 min read

Daniel asksFront-end developer

I'm checking a sailing school's website against WCAG before its audit. On the FAQ page, each question is an h2 heading, and clicking it opens the answer.

With a mouse, everything works. With the keyboard, Tab jumps from one link to the next without ever stopping on the questions, and the answers stay closed.

The No Keyboard Trap criterion starts with “If keyboard focus can be moved to a component.” My headings should be able to take focus. Should I fail the page on that criterion? If it's not a trap, which criterion do I log it under?

Mikhail Shaymardanov answersWeb Quality and Accessibility Expert

I often find accordion FAQs whose headings only open on click, and they raise the same keyboard trap question. I'll show you which criterion a heading that Tab skips fails, the keyboard test that proves it, and the fix.

Focus skips the questions without getting stuck


Screenshot: the FAQ page of Tern Bay Sailing School. Under the heading “Common questions,” a sentence links to the course prices, then five questions follow, from “Do I need to know how to swim?” to “Can I cancel a course?”. The first one is open on its answer. At the bottom, the “Contact us” link has a red focus outline. On the right, a photo of small sailboats on a bay.Marker 1. The five FAQ questions, h2 headings that open on click. Marker 2. The “Contact us” link, which gets focus right after the “Course prices” link.
On the Tern Bay Sailing School FAQ, the five questionsmarker 1 open on click, but Tab goes from “Course prices” to “Contact us”marker 2 without stopping on them.

#A clickable heading that Tab skips can't trap keyboard focus

From the keyboard, the sailing school's FAQ is a dead end, so it looks like a trap. Keyboard focus marks the element on the page that receives your keystrokes, and nothing holds it here: it goes around the questions without entering them. We followed it. One press of Tab skips all five questions, and one press of Shift+Tab goes back.

Click the demo, then press the key shown, or move on with “Next step”. Escape leaves the demo.
3 steps

On the Tern Bay Sailing School FAQ, one press of Tab goes from “Course prices” to “Contact us,” and Shift+Tab comes back: the five questions are skipped.

The problem is in the script: it reacts to clicks on an h2 heading. By default, the browser gives focus only to links, buttons, form fields and a few other elements, never to a heading. So people who navigate by keyboard can't open a single answer, whether they use a standard keyboard, an alternative one or a device that acts as one.

A keyboard trap is a component of the page that focus can enter from the keyboard and can't leave from the keyboard, so the person gets stuck there. Focus never enters a heading that Tab never reaches, so it can't get stuck in it. WCAG are the international web accessibility guidelines. Their No Keyboard Trap criterion only applies when focus can reach the component from the keyboard.

#What does “can be moved to a component” mean?

The criterion's condition ends with “using a keyboard interface,” and we read it literally: focus has to really get to the component with the keys the page offers today. A heading the page never lets you reach doesn't count. The W3C publishes WCAG, and its test rule for keyboard traps reads it the same way: it applies only to elements that can receive focus.

The rest of the criterion might make you hesitate, since it lists the ways out of a component: Tab, the arrow keys, another standard exit method, or any other method the page explains. Each one describes how focus leaves a component it has already entered. A heading that focus never reaches has nothing to leave.


#Test keyboard navigation to tell a skipped heading from trapped focus

The keyboard test takes two passes: find the elements in the code that react to clicks, then go through the whole page with Tab and Shift+Tab. Do the second pass in Chrome or Firefox, since on a Mac, Safari's Tab key skips links and buttons by default. On an accessible accordion, Tab stops on each question, Enter or Space opens its answer, and Tab then moves to the next element.

For the first pass, select a clickable element in the Elements panel of Chrome's developer tools. The Event Listeners tab lists its listeners, the bits of script that wait for a click or a keypress.

Screenshot: Chrome DevTools in English, Elements panel. In the page tree, the h2 heading with the class question and the id q-swim is selected and expanded on its text, “Do I need to know how to swim?”. On the right, the Event Listeners tab lists a single event, click, attached to h2#q-swim.question and defined in faq.html on line 130.Marker 1. The h2 heading “Do I need to know how to swim?”, selected in the page tree. Marker 2. The heading's click listener, in the Event Listeners tab.
In Chrome's Elements panel, the h2 heading “Do I need to know how to swim?”marker 1 has a click listenermarker 2: a click opens it, Tab never reaches it.

The second pass has two possible outcomes. A clickable element that focus never reaches falls under the Keyboard criterion, since its action can't be triggered from the keyboard. Focus is trapped when it enters a component and can't leave with Tab, Shift+Tab, the arrow keys, a standard exit such as Esc, or a key the page explains.

#What does a real keyboard trap look like?

A real trap holds focus inside a component with no way out from the keyboard. The W3C accepts holding focus on purpose, as long as the person knows how to get out. One of its examples is a dialog box opened by a web application: Tab loops through its controls, and two buttons or the Esc key close it. The W3C also describes a text editor with formatting tools, where Tab sets the indentation. An instruction appears when the editing area gets focus, and Alt+F10 (Option+F10 on a Mac) moves focus back to its toolbar.

#Which WCAG criterion does a heading that Tab skips fail?

In an audit report, I log this heading as a failure of the Keyboard criterion, success criterion 2.1.1. It requires all functionality to work from a keyboard. The only exception is a function whose input depends on the path of the user's movement, like freehand drawing. If the European Accessibility Act covers the site, also cite EN 301 549, the European accessibility standard for digital products and services. Its clause 9.2.1.1 requires web pages to meet this criterion.

The heading would still pass if keyboard users had another way to perform the same action, or a comparable one. The W3C doesn't require every clickable element on screen to receive focus. On this FAQ, no other element opens the answers, so the heading fails.

On the sailing school's FAQ, No Keyboard Trap passes, before the fix and after it. That criterion concerns the elements that receive focus, and we checked that each one lets focus move on.

WCAG audit: how it works and what the report's score counts

#Put a real button in each accordion heading

Put a <button type="button"> holding the question inside each heading, and attach the click listener to it. A button receives focus by default. On a real button, the click listener also fires on the Enter and Space keys, so you write no keyboard code at all. The W3C's accordion pattern gives the markup for an accessible accordion. The button sits alone inside the heading, with aria-expanded set to false or true and aria-controls pointing to the answer.

The aria-expanded attribute says whether the answer is open. The script toggles it along with the hidden attribute, which hides a closed answer. Our article on aria-expanded explains which element it belongs on.

Give the button the heading's font and size, with no border or background. Nothing changes on screen: only the page's focus outline appears when someone uses the keyboard.

Avoid

Screenshot: the question “Do I need to know how to swim?”, in navy blue with a plus sign in a circle, above the question “How old do you have to be?”. No focus outline.Marker 1. The question “Do I need to know how to swim?”, a clickable heading that focus never reaches.
<h2 class="question">Do I need to know how to swim?</h2>
<div class="answer" hidden>…</div>

Correct

Screenshot: the same question inside the page's red focus outline, with a minus sign; its answer is open below: “Yes, at least 25 meters. On the water, everyone wears a life jacket, children and instructors alike.”Marker 1. The question “Do I need to know how to swim?”, now a button, with the page's focus outline. Marker 2. The answer, open under the question.
<h2>
  <button type="button" aria-expanded="false" aria-controls="answer-swim">
    Do I need to know how to swim?
  </button>
</h2>
<div id="answer-swim" hidden>…</div>
Before, the question “Do I need to know how to swim?”marker 1 never gets focus. After, as a button, it gets focusmarker 1 and opens its answermarker 2 with Enter.

For questions inside a .faq block, the script that opens and closes the answers fits in seven lines. Copy it as is.

for (const button of document.querySelectorAll(".faq h2 button")) {
  button.addEventListener("click", () => {
    const expanded = button.getAttribute("aria-expanded") === "true";
    button.setAttribute("aria-expanded", String(!expanded));
    document.getElementById(button.getAttribute("aria-controls")).hidden = expanded;
  });
}

There's no need to code the arrow keys, Home or End to move between questions. The W3C's accordion pattern only asks for Enter, Space, Tab and Shift+Tab. It used to list the other keys as optional and removed them in April 2026. Some guides online still recommend them.

#How do you check the fix with the keyboard?

Run the keyboard pass again on the fixed page.

Click the demo, then press the key shown, or move on with “Next step”. Escape leaves the demo.
4 steps

On the fixed Tern Bay Sailing School FAQ, Tab stops on each question, and Enter or Space opens its answer without moving focus.

On the sailing school's FAQ, getting from “Course prices” to “Contact us” now takes six presses of Tab, up from one. The five extra stops are the questions, which now open from the keyboard as well as with the mouse. The Keyboard criterion passes.

The “tabIndex should only be declared on interactive elements” error: what to use instead

Share this article

Frequently asked questions

No. The tabindex="0" attribute lets the heading receive focus, and that's all it does. On a heading, even with role="button", the click event doesn't fire from the keyboard. Pressing Enter opens nothing, so the answer still can't be opened from the keyboard.

Mozilla's MDN reference advises against this setup: a non-interactive element that you let receive focus is still a fake button. Replace it with a real button, as shown in our article on tabindex.

Yes. The summary element receives focus without tabindex, and clicking it opens or closes the details element around it.

MDN warns that some browsers give summary a button role, which removes the roles of everything inside it. An h2 placed inside it then stops being a heading for assistive technologies such as screen readers. If your questions need to stay headings, keep the button inside the h2.

Yes, for the websites of the services it covers. Its Annex I asks for websites that are “perceivable, operable, understandable and robust.” The Act's own non-binding examples for websites include “making all functionality available from a keyboard.”

EN 301 549, the European accessibility standard, spells this out with WCAG. Its clause 9.2.1.1 requires web pages to meet the Keyboard criterion, and the next clause does the same for No Keyboard Trap. Check first whether the Act applies to your site.