- Level
- A
- Since
- WCAG 2.0
2.1.2 No Keyboard Trap
If keyboard focus can move into a part of the page, it must be able to move out again with the keyboard, and if that takes an unusual key, the page has to say which.
/ What it requires
A keyboard trap is a place where Tab, Shift+Tab and the arrow keys stop working: focus goes into an embedded player, an editor, a map or a badly built widget and cannot get out. For someone who uses only a keyboard, or a switch or screen reader that sends keyboard commands, the rest of the page is gone, and often the only way out is to close the tab. That is why WCAG lists this as one of four criteria that apply to all content on a page, including parts not relied upon to meet the other criteria.
The rule: if focus can be moved into a component with the keyboard, it can be moved away with the keyboard alone. When leaving takes more than unmodified arrow or Tab keys or another standard exit method, the user must be told how. WCAG does not define standard exit methods, because they depend on hardware, browser and operating system, but W3C names Escape as the common one on a physical keyboard. Keeping focus inside an open modal dialog is not a trap as long as the user knows how to leave it. A rich text editor that uses Tab for indentation passes if it says which key moves focus out.
/ How pages fail it
- An embedded video player, map or game in a plugin or iframe takes focus and handles every key itself, with no key that hands focus back to the page.
- A modal dialog cycles focus between its buttons, ignores Escape, and its only exit is a close icon that cannot take focus.
- A focus-trap script stays active after its dialog has closed, so Tab keeps returning to an element that is no longer visible.
- A text editor or code field captures Tab for indentation and gives no instruction for moving focus out of it.
- A custom date picker moves focus into its grid when it opens and pulls focus back on every keydown, so neither Tab nor Escape gets out.
/ How to check it yourself
- Start at the address bar and press Tab through every control on the page, including embedded players, maps, editors and widgets, until focus returns to the browser.
- At each component check that Tab moves on and Shift+Tab moves back. If focus stays in place after several presses, try the arrow keys and Escape.
- Open every dialog, menu and popup with the keyboard, leave it with Escape or its close button, and check that focus can then move on through the rest of the page.
- Where a component needs a different key to exit, check that the page says so, in visible text before the component or in an instruction shown when it receives focus.
- When focus is not visible, follow it with a live expression in the Chrome DevTools console set to document.activeElement.
/ How to fix it
- Remove focus-trap listeners when the dialog closes and move focus back to the control that opened it.
- Close modals on Escape with a keydown handler and make the close control a button element. A native dialog element opened with showModal() closes on Escape by default.
- In an editor that takes over Tab, provide a documented key that moves focus out, and show that instruction when the editor receives focus.
- Do not call preventDefault() on Tab keydown events unless the component moves focus itself and lets it leave at both ends.
- Test third-party players and widgets in place before shipping them, and replace any that keep focus. A trap inside embedded content fails the page that includes it.
/ What our scanner checks
The site scan does not press keys, so it cannot prove a trap. Two checks in the OAKMONT Inspector browser extension do, on request, with real key events sent through Chrome's debugger. focus-trap presses Tab up to 60 times and reports an element that keeps focus through three presses in a row. escape-does-not-close opens up to six controls marked aria-expanded or aria-haspopup with Enter, presses Escape and reports what stays open. A loop around several elements and Shift+Tab are not tested, and a widget that ignores Escape is a trap only if no other key gets out.
/ Who fails it, in our own measurements
The weekly index has no figures for this criterion, because none of the checks the site scan runs can test it. A person judges it, which is what a manual audit is for.
One page per body, measured by automated checks on a weekly schedule. Any body that has fixed something since the last sweep will show it at the next one, and every entity page carries a right of reply.
/ Questions about this criterion
Is a modal dialog that keeps focus inside a keyboard trap?
No. W3C says that restricting focus to an open modal dialog does not fail 2.1.2, as long as the user knows how to leave it. The usual exits are Escape and a Cancel or Close button. The dialog fails when none of them works from the keyboard.
What counts as a standard exit method?
WCAG does not define it, because it depends on the user's hardware, browser and operating system. Unmodified Tab and arrow keys always count, and W3C names Escape as the common standard exit on a physical keyboard. Any other key or combination is allowed only if the user is told about it.
Does 2.1.2 apply to third-party content such as embedded players?
Yes. It is one of four criteria that apply to all content on a page, together with 1.4.2, 2.2.2 and 2.3.1, because a failure blocks the use of the whole page. A trap inside an embedded player or chat widget fails the page that includes it, whoever wrote the code.
