Keyboard trap: does a clickable heading that Tab skips count?
Daniel asksFront-end developerI'm checking a sailing school's website against WCAG before its audit. On the FAQ page, each question is an
h2heading, 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
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.#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.
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.
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.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
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
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>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.
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


