Home / Fix guides / Accessibility (WCAG)

How to fix a keyboard trap in a modal or popup

A keyboard trap happens when someone using only the keyboard opens a popup and cannot get out of it, or cannot get back to the page. The fix is to make sure Escape and a reachable close button always work, and that focus returns to where the person was.

A modal is any box that appears over your page: a newsletter signup, a cookie notice, a video player, a product quick view, a booking form. A keyboard trap is when a person who uses the Tab key instead of a mouse gets stuck inside one. They press Tab and focus loops around inside the box, or it jumps somewhere invisible, and no key closes it. The only way out is to reload the page or give up.

This is a failure of WCAG success criterion 2.1.2 No Keyboard Trap (Level A, the most basic level). It is one of the few accessibility problems that completely blocks a visitor rather than just annoying them.

What people actually see

People who use keyboards, screen readers, switch devices or voice control on desktop all rely on Tab, Shift+Tab, Enter and Escape. Typical symptoms:

  • A popup opens and the close button (the X) cannot be reached with Tab.
  • Pressing Escape does nothing.
  • Focus slips behind the popup, so Tab moves through links on the page you can no longer see.
  • Focus gets stuck inside an embedded video or chat widget and will not leave.
  • After closing the popup, focus jumps back to the top of the page, so the person loses their place.

Note that a good modal is supposed to keep focus inside while it is open. That is not the trap. The trap is when there is no keyboard way out.

Why it matters

If a visitor cannot dismiss your popup, they cannot buy, book or contact you. That is lost sales and a poor impression of your business. In many countries accessibility rules and complaints reference WCAG, so a Level A failure is the kind of issue that gets noticed first. It also tends to affect more than disabled visitors: people on laptops who prefer the keyboard, or whose mouse has failed, hit the same wall.

How to check your site for free

This test takes about five minutes and needs no tools. Put your mouse aside.

  1. Open your page and press Tab until you reach the button or link that opens a popup. Press Enter.
  2. Press Tab several times. Focus should move to elements inside the popup, and you should be able to see where it is.
  3. Keep tabbing. Focus should loop within the popup, never disappear behind it.
  4. Press Escape. The popup should close.
  5. Reopen it and tab to the close button. Press Enter. It should close too.
  6. After closing, check that focus returns to the button that opened the popup.

Also test popups that open on their own: cookie banners, exit offers, chat bubbles and delayed newsletter boxes. These are the most common culprits because nobody clicks a button to open them.

Automated checkers such as Lighthouse (built into Chrome), the WAVE extension and axe DevTools are useful for other problems, but they cannot reliably detect a keyboard trap. Only a manual keyboard test is dependable here.

How to fix it in general

The right solution depends on how the popup was built, but the requirements are always the same.

  • Give a keyboard way out. The close control must be a real button element (or have role="button" and tabindex="0" with key handling) and Escape must close the popup.
  • Move focus in when it opens. Send focus to the popup's heading, its first field or the close button.
  • Keep focus inside while open, but not forever. Focus should loop within the popup, then be released when it closes.
  • Return focus when it closes. Put it back on the element that opened the popup.
  • Make the page behind unreachable. Use the inert attribute on the rest of the page, or aria-hidden carefully (see aria-hidden on focusable elements).
  • Label it. Use role="dialog", aria-modal="true" and aria-labelledby pointing at the popup title.

If you or your developer can edit the code, the simplest route is the native HTML dialog element opened with showModal(). Browsers then handle focus containment, Escape and the backdrop for you.

<dialog id="signup" aria-labelledby="signup-title">
  <h2 id="signup-title">Join our list</h2>
  <form method="dialog">
    <button>Close</button>
  </form>
</dialog>

Common causes and fixes by platform

Menu names change between versions and themes, so look for the idea rather than the exact label.

  • WordPress: The popup usually comes from a theme or a popup or cookie plugin. Check its settings for options like close on Escape, close button and accessibility. Test with the default theme on a staging copy (not your live site) to find out whether the theme or a plugin is responsible. See the WordPress guide.
  • Shopify: Quick view, cart drawers and newsletter popups come from the theme or an installed app. Disable apps one at a time, ideally on a duplicate or preview theme rather than your live store, to find the offender, and check whether a theme update fixes it.
  • Wix, Squarespace, Webflow: Built in lightboxes and popups are generally better behaved, but custom code, embeds and third party widgets are often not. Look at what you added by hand.
  • Elementor: Popups built in its popup builder need a visible close button, and the setting that prevents closing on Escape should be left off where it is offered. Avoid hiding the close button for design reasons.
  • React and custom code: Use a well tested library such as Radix UI Dialog, React Aria or focus-trap-react rather than writing focus logic by hand, and make sure the trap is turned off when the modal unmounts.

Embedded iframes (YouTube, maps, booking tools, chat) can also capture focus. If one does, check the provider's settings or consider a different embed.

How to confirm the fix worked

Repeat the six step keyboard test above on every popup, in at least two browsers (for example Chrome and Firefox). Then test with a screen reader if you can: NVDA is free on Windows and VoiceOver is built into Mac. The popup title should be announced when it opens. Finally, test on a narrow window, because mobile layouts sometimes show a different popup that behaves differently.

A visible focus ring matters here too. If you cannot see where focus is, you cannot tell whether you are trapped, so check missing focus indicators as well.

How to stop it coming back

  • Run the keyboard test whenever you add a popup, change a theme or update a plugin.
  • Prefer popups from your platform's own builder or the native dialog element over custom scripts.
  • Ask any developer or agency to confirm that Escape, the close button and focus return work before launch.
  • Keep a short list of every popup and embed on your site so nothing is forgotten, especially ones that appear automatically.
  • Remember that cookie banners and chat widgets get updated by their providers, so retest them now and then.

SiteMonday's accessibility audit (WCAG 2.2) in week 7 checks for this kind of problem and lists it in a ranked fix list, but making the change is up to you or your developer.

Step by step for your platform

Questions

Is it a keyboard trap if focus stays inside my modal?

No. Keeping focus inside an open modal is correct behavior. It is only a trap if the person cannot leave using the keyboard, for example because Escape and the close button do not work.

Which WCAG rule covers keyboard traps?

Success criterion 2.1.2 No Keyboard Trap, which is Level A. It says that if focus can move into a component with the keyboard, it must be possible to move away again with the keyboard.

Can an automated tool find keyboard traps?

Not reliably. Tools like Lighthouse, WAVE and axe catch many other issues, but a trap depends on how the popup behaves as you use it, so a manual Tab and Escape test is needed.

Do cookie banners count?

Yes. A cookie banner is a popup like any other, and it often appears automatically, so it is easy to forget to test. It needs a keyboard way to accept, decline or dismiss it.

Should I use Escape or a close button to close a modal?

Both. Escape is what keyboard and screen reader users expect, and a visible, focusable close button helps everyone else, including people who do not know the shortcut.

Who fixes a keyboard trap on my site?

You or your web developer. Simple cases can be solved in plugin or theme settings, while custom popups need a code change, often by switching to the native dialog element or a tested library.

Related problems