Keyboard Navigation Done Properly

Many people cannot use a mouse, or prefer not to. If your site doesn't work with a keyboard, it may be completely unusable for them. Luckily, it is one of the easiest accessibility issues to test.

Keyboard Navigation Done Properly — Troiana insight cover

In short

A site works with a keyboard when people can reach and operate every control using Tab, Shift+Tab, Enter, Space, arrow keys and Escape, in a logical order, and can always see where they are. Use real HTML buttons and links, don't remove focus outlines, manage focus in modals, and add a skip link. Test by putting your mouse away and using your site.

Who relies on it

People with motor impairments, repetitive strain injuries or a temporarily broken arm. Blind people who use screen readers, which rely heavily on keyboard commands. Switch-device users, whose equipment often behaves like a keyboard. And power users, who find it quicker.

Keyboard access is also required by WCAG 2.1.1 Keyboard, at Level A. That's the most basic level of accessibility, and it can have legal implications. See is your site legally required to be accessible?

The five-minute test

Put your mouse aside, and try the following:

  1. Press Tab, from the top of the page. Can you see where you are at each step?
  2. Is the order logical? Does it follow the visual layout?
  3. Can you reach everything? Menus, buttons, form fields and links.
  4. Can you operate everything? Enter on links, Enter or Space on buttons.
  5. Can you get out of everything? Modals, menus and embedded widgets. Esc should close overlays.
  6. Complete your key task. Sign up, or buy something.

If you get stuck, then so will your users. See how to test accessibility without a specialist.

On a Mac, you may need to turn on keyboard navigation in System Settings, and in Safari's settings, for Tab to reach all controls.

The keys people expect

KeyAction
Tab / Shift+TabMove forward or back between controls
EnterFollow a link, or press a button
SpacePress a button, or toggle a checkbox
Arrow keysMove within menus, tabs, radios and selects
EscClose a modal or a popover
Home / EndJump to the start or end of a list, or similar

Rule 1: Use real HTML

The most common failure is a clickable div:

<!-- Bad: can't be reached or activated by keyboard -->
<div onclick="save()">Save</div>

<!-- Good: keyboard support comes for free -->
<button type="button" onclick="save()">Save</button>

A real button, a href, input or select gets focus, keyboard behaviour and correct screen reader roles automatically. To make a div do this, you'd have to add tabindex, a role, and handlers for Enter and Space. Why would you?

And use the right element: a link goes somewhere, and a button does something. AI-generated code often gets this wrong, so check.

A clickable div
Save
A real button
Both rendered live. Press Tab: focus lands on the real button and skips the div, however similar they look.

Rule 2: Never hide where people are

Never do this without providing a replacement:

/* Removes the focus indicator for everyone */
*:focus { outline: none; }

Use :focus-visible for a clear style that appears for keyboard users:

:focus-visible {
  outline: 3px solid #1a56db;
  outline-offset: 2px;
}

See focus states that actually work.

Rule 3: Keep the order logical

Tab order follows the order of your HTML, so make the source order match the visual layout. Problems come from:

  • CSS that reorders content, such as order in flexbox, or positioning. See CSS grid or flexbox.
  • Positive tabindex values. Never use tabindex="1". It breaks the natural order.
  • Hidden elements that can still receive focus, such as off-screen menus. Use display: none or inert on things that aren't available.

Here's a guide to tabindex:

  • tabindex="0": add it to the natural tab order, for custom widgets only
  • tabindex="-1": focusable with script, but not by Tab; useful for moving focus to a heading
  • Positive numbers: avoid

Don't make people tab through 40 menu items on every page.

<a class="skip-link" href="#main">Skip to main content</a>
<!-- Navigation here -->
<main id="main" tabindex="-1">...</main>

Hide it visually until it receives focus. See what a skip link is.

Rule 5: Handle modals properly

When a modal opens:

  1. Move focus into it.
  2. Keep Tab inside it.
  3. Close it with Esc.
  4. Return focus to the button that opened it.

The native dialog element, opened with showModal(), handles much of this for you. See how to build an accessible modal from scratch.

A modal, by keyboard
OpensFocus moves into it
TabStays inside it
EscCloses it
ClosedFocus returns to the button
The native dialog element, opened with showModal(), handles much of this for you.

Rule 6: Move focus when the page changes

In single-page apps, content can change with no page load, leaving keyboard users stranded. Move focus to the new heading after route changes. Likewise, when you delete an item from a list, move focus to something sensible instead of letting it vanish. After submitting a form with errors, move focus to the error summary. See how to design error states users can recover from.

Rule 7: Make menus and custom widgets behave

Dropdown menus: make sure that they open with a button, and aren't just hover-only. Esc should close them, returning focus to the button. See how to design a navigation menu.

Tabs, comboboxes and similar: these have expected arrow-key patterns. The WAI-ARIA Authoring Practices Guide documents them. Use a well-tested library, such as Radix or React Aria, instead of building your own. These are harder than they look.

Carousels: provide controls that can be reached, and allow people to pause any movement.

Drag and drop: offer an alternative using buttons. WCAG 2.2 requires a single-pointer alternative for dragging, and keyboard access is required separately.

Avoid keyboard traps

A trap is when focus enters something and can't get out. Embedded widgets, video players and maps are common culprits, as are rich text editors that capture Tab. WCAG 2.1.2 forbids this. Test all third-party embeds, too, as you're responsible for your page as a whole.

Other things to check

  • Focus not hidden by sticky headers: add scroll-margin-top, so that focused items aren't covered. WCAG 2.2 addresses this as well.
  • Hover-only content: tooltips and menus should also open on focus.
  • Custom shortcuts: single-letter shortcuts can interfere with assistive technology, so let users turn them off.
  • Forms: real labels, logical order, and a submit button that's reachable. See how to build an accessible form.
  • Tables: interactive rows should use real links or buttons. See accessible data tables.

Add it to your process

  • Include a keyboard check in your design QA.
  • Build these behaviours into your components, so that every team gets them automatically. See building a design system that scales.
  • Automate what you can, but know that automated tools can't judge logical order or usable flows. End-to-end tests can check that key journeys work with a keyboard.

See also how to make a website accessible.

If you'd like an accessibility review of your website or product, book a call.

Common questions

How do I test keyboard navigation on a website?

Put your mouse away and press Tab from the top of the page. Check that you can see the focus, that the order is logical, and that you can reach and operate everything, including closing modals. Then try completing your main tasks.

What is a keyboard trap?

It's when keyboard focus enters a component and you can't move out using the keyboard. Embedded widgets and rich text editors are common culprits. It's a failure of WCAG 2.1.2 and can make a page unusable, so test all third-party embeds.

Should I remove focus outlines from buttons?

No, not without a clear replacement. Keyboard users need to see where they are. Use the :focus-visible selector, to show a strong focus style to keyboard users, without necessarily showing it on mouse clicks.

When should I use tabindex?

Rarely. Use real interactive elements first. Use tabindex 0 for custom widgets that need focus, and tabindex -1 when you're moving focus with a script. Avoid positive numbers, because they break the natural tab order and confuse users.

Have something worth building right?