Imagine trying to navigate a website where you can enter information but can’t move to the next field. Or perhaps you’ve opened a dropdown menu but can’t escape it. Frustrating, right?
These situations, known as keyboard traps, create significant barriers for many web users. For people who rely solely on keyboard navigation – whether due to motor impairments, temporary injuries, or personal preference – these roadblocks can render websites completely unusable.
According to the 2024 WebAIM Screen Reader User Survey, lack of keyboard accessibility remains one of the most problematic issues on the web. Surprisingly, this problem has persisted largely unchanged for the past 14 years.
In this article, we’ll explore what keyboard traps are, why they matter for web accessibility, common issues you might encounter, and most importantly, how to identify and prevent them in your web development projects.
What are keyboard traps, and why do they matter for accessibility?
A keyboard trap occurs when a keyboard user can navigate to an interactive element on a webpage but cannot move away from it using standard keyboard controls. Essentially, the focus gets “trapped” within that element, preventing further navigation.
These traps commonly appear in:
- Input boxes where the Tab key doesn’t allow you to exit.
- Drop-down menus that capture all keyboard events.
- Modal dialogues without proper keyboard exit methods.
- Hyperlinks with scripts that override the default keyboard behaviour.
For keyboard-only users, this creates a frustrating dead-end. You can enter the component but can’t leave it without using a mouse, which many people simply cannot do.
Who’s affected? More people than you might think:
- People with motor impairments, including conditions like rheumatism or Parkinson’s disease.
- People who temporarily cannot use a mouse due to injuries.
- Developers testing through keyboard navigation (it’s actually much faster).
- Power users who prefer keyboard shortcuts for efficiency.
The Web Content Accessibility Guidelines (WCAG) directly address this issue in Success Criterion 2.1.2 No Keyboard Trap:
“If keyboard focus can be moved to a component of the page using a keyboard interface, then focus can be moved away from that component using only a keyboard interface, and, if it requires more than unmodified arrow or tab keys or other standard exit methods, the user is advised of the method for moving focus away.“
This criterion is categorised as Level A – meaning it represents basic accessibility compliance that must be implemented, not an optional enhancement.
Common keyboard traps and their impact on users
Most often, keyboard traps show up when developers make decisions about keyboard focus behaviour. This is why, if focus needs to stay within a component (like a modal dialog), always provide users with a clear exit path using keyboard controls alone.
Now, let’s examine some common keyboard traps and understand their specific impact on people who rely on keyboard navigation.
Custom UI controls
Native HTML elements, like <button>
, are fully equipped with built-in keyboard interaction by default. For example, a button element supports keyboard actions such as pressing Enter or Space to activate it, and users can navigate to it using the Tab key.
So why don’t developers always use buttons, which have these features baked in? The reason is often that developers want to create a highly customised or visually unique interface. Standard HTML elements, like <button>
, have a default look and behaviour that might not match the specific design or interaction flow the developer envisions.
However, this customisation comes at a cost. When developers create custom UI components, such as dropdown menus, modals, or navigation elements, they sometimes bypass standard HTML elements in favour of custom JavaScript-based solutions. Sure, this can provide more flexibility in design and behaviour, but it inevitably introduces significant accessibility challenges, including keyboard traps. Without proper focus management and keyboard control, users can become stuck in a custom component, unable to navigate to other parts of the page.
Imagine a modal dialog that opens when a user clicks a button. If the modal disables all interaction outside of itself and doesn’t handle keyboard focus properly, this can lead to a keyboard trap. The user might find that pressing the Tab key keeps cycling them through elements within the modal, unable to close the dialog or move focus away from it, resulting in a frustrating experience.
So, to avoid creating keyboard traps in custom controls:
- Use semantic HTML elements: Whenever possible, rely on native HTML elements like
<button>
,<a>
, and<input>
because they come with built-in keyboard controls that work automatically. And if you’re building custom components, ensure they mimic the expected behaviour of these elements. - Ensure proper focus management: Properly manage focus within your custom controls. When a dialog or dropdown is triggered, move focus to the first interactive element inside the component. After closing the component, return focus to the element that triggered it.
- For modals, trap the keyboard focus inside the modal while it’s open to prevent users from tabbing to other parts of the page.
- For dropdowns, ensure that when the dropdown opens, focus is placed on the first item. When the dropdown is closed, focus should return to the trigger button.
- Provide expected keyboard navigation:
- Arrow keys: For elements like custom dropdowns, arrow keys are expected to navigate through items (up and down to select).
- Escape key: This should close dialogs or dropdowns, providing an intuitive way to exit without using a mouse. But don’t forget also adding a visible “close” button.
- Tab key: This key should move focus between focusable elements, ensuring users can move from one component to another smoothly.
- Employ ARIA roles and states: For custom components, use ARIA roles to ensure assistive technologies understand the purpose and behaviour of the elements. For example, add
aria-expanded="true"
when a dropdown is open, so screen reader users are aware of the current state.
Media players
Media players can be particularly problematic when they capture and don’t release keyboard focus. Many video players implement custom controls that intercept all keyboard interactions, leaving keyboard-only users stuck within the player controls.
When this happens, users are often forced to take drastic measures like refreshing the entire page just to continue navigating, losing their place, and having to start over.
Some embedded players, for example YouTube, have single key commands for play, pause, fullscreen and more. These can interfere with screen reader software.
Solution: Always preserve the fundamental Tab and Shift+Tab functionality in media players, even when implementing custom keyboard controls for playback. Users should be able to enter the player controls, interact with them as needed, and then smoothly tab away to continue exploring the rest of the page. You can disable YouTube embed keyboard shortcuts by adding a query string segment to the embed url.
Form elements
Date pickers and other complex form components frequently create keyboard traps. These elements often capture all keyboard events rather than just the specific keys needed for their functionality. When a keyboard user opens a date picker, they may find themselves unable to exit using the standard Tab or Escape keys.
Solution:
- Limit event capturing to only the specific keys needed for the component’s functionality. For example:
- Arrow keys for navigating within the calendar.
- Enter for selecting a date.
- Escape for closing without selection.
- Implement proper ARIA attributes:
- Set
aria-expanded="true"
on the date picker trigger button when the calendar is open. - Update to
aria-expanded="false"
when closed.
- Set
Hyperlinks
Hyperlinks might seem straightforward, but they can create keyboard traps when improperly implemented. Developers sometimes attach event handlers to links that override standard keyboard behaviour, creating accessibility barriers.
Let’s examine a real-world example from this GitHub test page:
On this page, there are two simple hyperlinks:
- Trap
- Honey
When a keyboard user focuses on the first link (“Trap”) and attempts to navigate away using the Tab key, something unexpected happens. A keydown event triggers the opening of a new window and prevents focus from moving to the next element. This behaviour effectively traps the user, making it cumbersome to reach the second link (“Honey”) using keyboard navigation alone. The user might try using shift+tab to reach the “Honey” link from the other direction, but at this point, they’re basically just hoping for the best.
The problem occurs because the developer has attached a JavaScript event that hijacks the Tab key’s standard behaviour.
Solution:
- Respect default keyboard behaviours.
- Avoid using
event.preventDefault()
on key events like Tab. - Let browser-standard keyboard interactions work as users expect.
- Avoid using
- Maintain logical focus order.
- Place interactive elements in a sensible sequence in the HTML.
- Avoid excessive use of
tabindex
attributes that can disrupt natural focus flow - Let HTML’s built-in focus ordering do its job.
- Provide clear instructions for custom interactions.
- If a component requires unique keyboard controls, explain them clearly.
- For example: “Press Escape to close this dialog” or “Use arrow keys to navigate the calendar.”
How to identify keyboard traps on your website
While automated accessibility testing tools can catch many issues, keyboard traps remain particularly challenging to detect automatically. That’s why manual testing is essential for ensuring keyboard accessibility.
Here’s a methodical approach to test your website for keyboard traps:
Basic testing approach
- Start from the address bar of your browser.
- Press the Tab key repeatedly to move through all interactive elements.
- Watch for the focus indicator (usually a highlight or outline) as it moves through the page.
- Use Shift+Tab to move backwards if needed.
- Test activation of elements using:
- Enter key for links and buttons.
- Spacebar for checkboxes, radio buttons, and sometimes buttons.
- Arrow keys for select menus, sliders, and custom widgets.
- Document any problems you encounter, especially points where focus becomes trapped.
- Return to the address bar by continuing to tab through all elements, or by using Shift+Tab from the first focusable element.
Next, pay special attention to these common trap locations:
Component type | What to watch for |
---|---|
Modal dialogs | Can you close them with Escape or reach the close button with Tab? |
Custom dropdowns | Can you exit the dropdown after opening it? |
Media players | Do video controls release focus after interaction? |
Date pickers | Can you tab away after selecting a date? |
Keyboard trap vs. poor keyboard navigation
It’s important to distinguish between these two problems:
- Keyboard trap = You cannot move focus away from an element at all using keyboard navigation.
- Poor navigation = You can eventually escape, but interaction is difficult or confusing.
Both issues need fixing, but true keyboard traps are more severe as they completely block user progress rather than merely hindering it.
Best practices for preventing keyboard traps
Preventing keyboard traps starts with awareness and proactive planning. Here are some best practices to ensure your websites remain keyboard-accessible:
1. Design with keyboard users in mind
Always approach your page with this fundamental question: “How can I navigate this page if I can’t use a mouse?” This simple mindset shift can help you spot potential keyboard traps before they make it into production.
2. Follow these key principles:
- Use native HTML elements like
<button>
,<select>
, and<input>
, which come with built-in keyboard accessibility – no extra work required! - Test custom components thoroughly, especially when building custom UI components.
- Implement proper focus management for components like modals and ensure there’s always a keyboard escape mechanism.
- Document keyboard shortcuts clearly for users.
- Test regularly throughout development and don’t wait until the end to check for keyboard accessibility.
3. Use proper resources to help you
- Learning proper accessibility techniques is essential. Our Accessible Code course can teach you how to identify and prevent common accessibility issues during development.
- For existing websites, consider a professional website accessibility audit to identify all accessibility barriers, including keyboard traps.
Remember: building accessibility from the start is always more efficient than retrofitting it later!
Take your accessibility skills to the next level with our Accessible Code course
Understanding keyboard traps is just one piece of the accessibility puzzle. Mastering WCAG-compliant development requires comprehensive knowledge of accessible coding practices across many areas.
Our Accessible Code course teaches you to identify and prevent common accessibility issues during development, ensuring your projects are inclusive from the start. You’ll learn practical techniques that make a real difference to users.
Whether you’re a developer looking to enhance your accessibility expertise or an accessibility specialist seeking IAAP certification credits, this platform-independent course provides the practical skills needed to create truly accessible websites.
Join our Accessible Code course today and build better, more inclusive websites for everyone!

Ready to get started?
Build better, more inclusive websites for everyone.