Keyboard Accessibility: Building a Mouse-Free Web
Ensuring every interaction works without a mouse or touchscreen
On this page
On this page
Why Keyboard Accessibility Matters
Keyboard accessibility is one of the foundational pillars of web accessibility and one of the most commonly violated. When a website requires a mouse for any operation — clicking a dropdown, dismissing a popup, navigating a carousel, submitting a form — it effectively excludes every user who cannot operate a mouse.
The affected population is larger than many developers realize. People with motor disabilities such as muscular dystrophy, cerebral palsy, arthritis, or repetitive strain injuries may be unable to use a mouse with the precision it requires. People with temporary injuries — a broken arm, a sprained wrist — face the same barriers. Screen reader users navigate almost entirely via keyboard commands. Power users and developers often prefer keyboard navigation for speed and efficiency.
WCAG 2.2 Success Criterion 2.1.1 (Keyboard) — a Level A requirement — states that all functionality must be operable through a keyboard interface without requiring specific timing for individual keystrokes. This is not a nice-to-have; it is the minimum standard. Success Criterion 2.1.2 (No Keyboard Trap) further requires that users must be able to move focus away from any component using standard keyboard methods.
Understanding Tab Order
When a user presses the Tab key, the browser moves focus to the next focusable element in the tab order. By default, the tab order follows the DOM (Document Object Model) order — the order in which elements appear in the HTML source code. Natively focusable elements include links (<a> with an href), buttons (<button>), form inputs (<input>, <select>, <textarea>), and elements with tabindex="0".
Maintaining a Logical Tab Order
A logical tab order matches the visual layout of the page. When users tab through a form, focus should move through fields in the order they appear visually — name, then email, then message, then submit. When the tab order diverges from the visual order, users become confused and may miss required fields or controls.
The most common cause of tab order problems is CSS layout changes that reposition elements visually without changing their DOM order. Flexbox order, CSS Grid placement, and position: absolute can all create disconnects between visual and DOM order. The solution is to ensure that your HTML source order matches the intended reading and interaction order, and to use CSS only for visual refinement — not for reordering content.
The tabindex Attribute
The tabindex attribute controls whether and in what order an element receives keyboard focus.
tabindex="0" adds an element to the natural tab order at its DOM position. Use this to make custom interactive elements — such as <div> elements acting as buttons — focusable. However, always prefer native HTML elements (<button>, <a>) when possible, as they include built-in keyboard interaction and accessibility semantics.
tabindex="-1" removes an element from the tab order but allows it to receive focus programmatically via JavaScript's element.focus(). This is useful for managing focus in custom widgets — for example, moving focus to a heading when a page section loads dynamically.
Positive tabindex values (tabindex="1", tabindex="5", etc.) force elements to the beginning of the tab order, overriding the natural DOM sequence. Avoid positive tabindex values. They create unpredictable tab order, are nearly impossible to maintain as pages change, and are considered an accessibility anti-pattern. If you need an element to appear earlier in the tab order, move it earlier in the DOM.
Focus Indicators
A focus indicator is the visual marker that shows which element currently has keyboard focus. Without visible focus indicators, keyboard users cannot determine where they are on the page — the equivalent of hiding the mouse cursor from a mouse user.
Default Browser Focus Styles
Browsers provide default focus indicators, typically a blue or dotted outline around the focused element. These defaults are functional but often minimal in contrast. The critical rule is: never remove default focus indicators without providing a visible replacement. The CSS declaration outline: none or outline: 0 applied globally to interactive elements is one of the most damaging accessibility errors a developer can make.
WCAG 2.2 Focus Requirements
WCAG 2.2 strengthened focus indicator requirements significantly. Success Criterion 2.4.7 (Focus Visible) at Level AA requires that any keyboard-operable user interface has a mode of operation where the keyboard focus indicator is visible. Success Criterion 2.4.11 (Focus Not Obscured — Minimum) at Level AA requires that the focused component is not entirely hidden by author-created content such as sticky headers, footers, or overlay banners. Success Criterion 2.4.13 (Focus Appearance) at Level AAA establishes specific minimum size and contrast requirements for focus indicators.
Designing Effective Focus Indicators
Effective focus indicators should have sufficient contrast against both the element and the surrounding background. A minimum contrast ratio of 3:1 is recommended. The indicator should be visible on all backgrounds — a blue outline that disappears on a blue background fails users. Consider using a combination of outline and offset, or a thick, high-contrast border.
Common techniques include a 2-3 pixel solid outline with an offset to separate it from the element edge, a contrasting background color change, or a combination of outline color and box shadow to ensure visibility on varied backgrounds. Test your focus indicators against light backgrounds, dark backgrounds, and the specific colors used in your design system.
Need help with ADA compliance?
Use our free accessibility tools to check your website for common issues.
Skip Navigation
When a keyboard user loads a new page, focus starts at the top of the document. Without a mechanism to bypass repetitive content — the site header, main navigation, secondary navigation — the user must tab through dozens of links before reaching the main content. On a site with a complex navigation structure, this can mean pressing Tab 30, 50, or even 100 times before reaching the content they came for.
Implementing Skip Navigation Links
A skip navigation link is a link placed at the very beginning of the page that, when activated, moves focus directly to the main content area. The standard implementation is an anchor link pointing to the id of the main content container.
The link can be visible at all times or visually hidden by default and made visible when it receives focus. The latter approach is common and acceptable as long as the link becomes fully visible and usable when a keyboard user tabs to it. Use CSS techniques like positioning off-screen and repositioning on :focus — not display: none or visibility: hidden, which remove the element from the tab order.
Best practices for skip links include placing the link as the first focusable element in the DOM, using clear link text such as "Skip to main content," ensuring the target element (main or a div with the appropriate id) has tabindex="-1" so it can receive focus reliably across all browsers, and testing that focus moves visually and programmatically to the intended location.
Multiple Skip Links
On complex pages, consider providing skip links to multiple landmarks: "Skip to main content," "Skip to search," "Skip to footer." Use a visually hidden list of skip links that becomes visible on focus. This approach is especially valuable on pages with multiple distinct content sections.
Focus Trapping for Modal Dialogs
When a modal dialog opens, keyboard focus should be constrained within the dialog until it is closed. Without focus trapping, a keyboard user tabbing through the dialog will eventually reach the end of its content and then move focus to elements in the page behind the modal — elements that may be visually obscured and contextually irrelevant.
How Focus Trapping Works
A focus trap works by detecting when focus is about to leave the dialog and redirecting it back to the first or last focusable element within the dialog. The standard implementation identifies all focusable elements within the dialog container, listens for Tab and Shift+Tab keypress events, and when focus is on the last focusable element and Tab is pressed, moves focus to the first focusable element (and vice versa for Shift+Tab).
Focus Management When Opening and Closing Modals
When a modal dialog opens, focus should move to the dialog or to the first interactive element within it. The dialog container should have role="dialog" and aria-modal="true" to communicate its purpose to assistive technology. An accessible name should be provided via aria-labelledby pointing to the dialog title.
When the modal closes, focus must return to the element that triggered the modal. If a user clicked a "Settings" button to open a settings dialog, focus should return to that "Settings" button when the dialog closes. Failing to restore focus leaves the user stranded at an unpredictable location in the page.
The Escape key should always close a modal dialog. This is a widely expected convention that keyboard users rely on.
Need help with ADA compliance?
Use our free accessibility tools to check your website for common issues.
Common Keyboard Interaction Patterns
Different types of interactive components require different keyboard behaviors. The WAI-ARIA Authoring Practices Guide (APG) provides detailed keyboard interaction patterns for common widgets.
Menus and Menu Bars
Dropdown menus should open with Enter or Space (or Down Arrow on a menu bar), allow navigation between items with Arrow keys, close with Escape, and support type-ahead (pressing a letter key jumps to the first item starting with that letter). The menu should trap focus within the submenu while open and return focus to the trigger when closed.
Tab Panels
Tab panels use a tab list where Left and Right Arrow keys move between tabs, Enter or Space activates the selected tab (in manual activation mode) or activation is automatic on arrow key press. Tab moves focus out of the tab list and into the tab panel content. Only the active tab should be in the tab order (tabindex="0"); inactive tabs should have tabindex="-1".
Accordions
Accordion headers should be operable with Enter or Space to expand and collapse content. Arrow keys can navigate between accordion headers. The content within an expanded accordion should be part of the natural tab order.
Carousels and Sliders
Carousels should be pausable from the keyboard and provide Previous/Next controls that are keyboard-focusable. Auto-rotating carousels should stop when any element within the carousel receives keyboard focus. Slide content should be navigable and interactive elements within slides should be focusable.
Testing Keyboard Accessibility
The most effective way to test keyboard accessibility is to put down your mouse and navigate your entire site using only the keyboard.
Start at the top of the page and press Tab repeatedly. Observe whether focus moves in a logical order, whether every interactive element receives visible focus, whether you can activate all buttons, links, and controls using Enter or Space, and whether you can open and close menus, dialogs, and popdowns using standard keys.
Try completing key user flows — searching, adding items to a cart, filling out a form, navigating between pages — without touching the mouse. Note any point where you become stuck, lose track of focus, or cannot complete an action.
Automated Testing
Automated tools can detect some keyboard accessibility issues. The axe accessibility engine flags missing focus indicators removed via CSS, focusable elements with no accessible name, and elements with positive tabindex values. However, automated tools cannot verify that tab order is logical, that focus management in custom widgets works correctly, or that all keyboard interaction patterns are properly implemented. Manual testing is essential.
Need help with ADA compliance?
Use our free accessibility tools to check your website for common issues.
Common Keyboard Accessibility Mistakes
Several patterns consistently cause keyboard accessibility failures across the web.
Using <div> or <span> elements as buttons without adding tabindex="0", role="button", and keyboard event handlers. Native <button> elements handle all of this automatically.
Implementing click-only event handlers without corresponding keydown or keypress handlers. Mouse events like onClick fire on keyboard activation for native buttons and links but not for custom elements without the appropriate role and keyboard handlers.
Removing focus outlines globally with *:focus { outline: none } for aesthetic reasons without providing replacement focus indicators.
Creating keyboard traps — components where focus enters but cannot exit via standard keyboard methods. Common culprits include embedded media players, third-party widgets, and custom rich text editors.
Using tabindex values greater than 0, creating unpredictable tab order that conflicts with the visual layout.
Failing to return focus to the trigger element when closing dialogs, tooltips, and other transient UI components.
Building Keyboard Accessibility Into Your Process
Keyboard accessibility should be a first-class concern in design and development, not an afterthought.
Use native HTML elements whenever possible. The <button>, <a>, <input>, and <select> elements come with built-in keyboard support, focus management, and accessibility semantics. Every time you build a custom interactive element from a <div>, you are recreating functionality that browsers provide for free — and you will almost certainly miss some aspect of keyboard or accessibility behavior.
Include keyboard interaction in your design specifications. When a designer specifies a new dropdown component, the specification should include what happens on Tab, Enter, Escape, and Arrow keys. Define focus indicator styles in your design system and enforce them in code review.
Test with the keyboard during development, not just at the end. Make keyboard navigation part of your definition of done for every interactive feature.
Frequently Asked Questions
- Why is keyboard accessibility important?
- Many users cannot operate a mouse or touchscreen. People with motor disabilities, repetitive strain injuries, tremors, or limb differences rely on keyboards, switch devices, or other keyboard-emulating assistive technologies. Screen reader users also navigate primarily via keyboard. If your website requires a mouse for any interaction, those users are completely locked out.
- What is tab order and why does it matter?
- Tab order is the sequence in which focusable elements receive focus when a user presses the Tab key. A logical tab order follows the visual reading order of the page — typically left to right, top to bottom in English-language pages. When tab order does not match the visual layout, keyboard users become disoriented and may miss critical content or controls.
- What is a focus trap and when should I use one?
- A focus trap constrains keyboard focus within a specific container, preventing Tab from moving focus to elements outside that container. Focus traps are essential for modal dialogs, where users should not be able to interact with the underlying page until the dialog is closed. However, focus must never be trapped unintentionally — users must always have a way to exit the trapped region, typically by pressing Escape.
- Do I need visible focus indicators on every interactive element?
- Yes. WCAG 2.2 Success Criterion 2.4.7 (Focus Visible) requires that keyboard focus indicators are visible on all interactive elements. WCAG 2.2 also introduced Success Criterion 2.4.11 (Focus Not Obscured - Minimum) and 2.4.13 (Focus Appearance), which set specific requirements for focus indicator visibility and appearance. Never remove the default browser focus outline without providing an equally visible replacement.