Websites & Hosting

Keyboard Navigation and Focus: A Practical Guide

How keyboard users move through a page, how to keep focus visible and ordered, and how to test dialogs and menus.

Key takeaways

  • Every function available with a pointer should also be available with a keyboard, with no point where focus gets stuck.
  • Native HTML controls are focusable and respond to the expected keys without extra script.
  • Focus must be visible, move in a logical order and not be hidden behind sticky headers or banners.
  • Dialogs should take focus when opened, keep it while open and return it when closed.
  • A five-minute unplug-the-mouse test finds many of the worst barriers.

To make a site work from the keyboard, use native links, buttons and form controls, keep the tab order matching the visual order, show a clear focus indicator, and manage focus when dialogs, menus or page content change. You can test almost all of it by putting the mouse aside and pressing Tab.

Keyboard support matters to people with motor impairments, screen reader users, people with temporary injuries and anyone who simply works faster that way. Many other input tools, such as switch devices and some voice software, rely on the same foundations.

The keys people expect

Key Usual behavior
Tab / Shift+Tab Move to the next or previous focusable item
Enter Follow a link, activate a button, submit a form
Space Activate a button, toggle a checkbox, scroll the page
Arrow keys Move within radio groups, menus, tabs and sliders
Escape Close a dialog, menu or popup
Home / End Jump to the start or end of a list or page

On macOS, some browsers and system settings don't move to every control with Tab by default. If links or buttons seem to be skipped on a Mac, look in the browser's accessibility or advanced settings and the system keyboard navigation option. Menu names vary by version.

Use elements that already work

A <button> is focusable, fires on Enter and Space, and announces itself. An <a> with an href is a link. Inputs, selects and text areas behave predictably. Recreating these with a <div> and a click handler leaves out focus, keys and meaning.

The tabindex attribute adjusts focus behavior:

  • tabindex="0" places an element in the normal tab order. Use it for a custom control you've also given a role and key handling, or a scrollable region.
  • tabindex="-1" removes it from the tab sequence but lets script focus it, useful for headings or error summaries.
  • Positive values force a custom order that quickly becomes unmanageable. Avoid them.

If the tab order looks wrong, fix the order in the HTML. CSS features such as order, reversed flex directions and absolute positioning can move things visually while focus still follows the source.

Keep focus visible

Browsers draw a focus ring by default. Removing it with outline: none and adding nothing back leaves keyboard users lost. Style it instead:

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

The :focus-visible selector lets browsers show the ring when it's useful, typically for keyboard use, without drawing it after every mouse click on a button. Pick a color with at least 3:1 contrast against adjacent colors, following color contrast and readability, and check it on dark and light sections.

WCAG 2.2 also asks that a focused item not be entirely hidden by other content. Sticky headers, cookie banners and chat bubbles are the usual offenders. Adding scroll-padding-top to the scrolling container helps keep targets below a fixed header.

Offer ways to skip

A keyboard user shouldn't have to press Tab forty times to get past navigation on every page. Add a skip link as the first focusable element that jumps to the main content. It can be visually hidden until focused.

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

Landmarks such as nav and main, and a sensible heading structure, give screen reader users additional shortcuts.

Manage focus in dynamic interfaces

Trouble starts when content appears, disappears or changes without a page load.

Focus through the life of a dialog
1 Trigger User presses a button that opens the dialog. 2 Move in Focus goes to the dialog or its first control. 3 Stay inside Tab cycles within; the page behind is inert. 4 Close Escape or a close button dismisses it. 5 Return Focus goes back to the original button. Trigger User presses a button that opens the dialog. 1 Move in Focus goes to the dialog or its first control. 2 Stay inside Tab cycles within; the page behind is inert. 3 Close Escape or a close button dismisses it. 4 Return Focus goes back to the original button. 5

The native <dialog> element opened with showModal() handles much of this: it blocks the rest of the page and closes on Escape. Test it in your target browsers and still confirm focus lands somewhere sensible and returns. For custom overlays, the inert attribute on background content stops focus and interaction there.

Other situations:

  • Deleting an item: move focus to the next item or a heading, so it doesn't fall back to the top.
  • Single-page route changes: update the page title and move focus to the new heading.
  • Form errors: focus an error summary or the first invalid field, as explained in designing error messages that help.
  • Loading more results: keep the user's place.
  • Status updates: announce them with a live region without stealing focus.

A deliberate focus trap is right only while a modal dialog is open, and there must always be a way out.

Composite widgets usually take one Tab stop, with arrow keys moving inside. This pattern, sometimes called roving tabindex, gives the active item tabindex="0" and the others -1. The W3C's ARIA Authoring Practices Guide describes expected keys for common widgets.

Be careful with menu roles. Ordinary site navigation is just a list of links, perhaps with buttons that expand sections. The ARIA menu role implies application-style keyboard behavior that you would then have to build.

Hover-only menus exclude keyboard and touch users. Open submenus with a button, indicate state with aria-expanded and close them with Escape. Content that appears on hover or focus should be dismissible and stay visible while the pointer moves onto it.

Single-character shortcuts can fire by accident for voice input users, so allow people to turn them off or require a modifier key.

Test it

A quick keyboard test loop
Fix and repeat Start with anything that blocks a task. Open overlays Try Escape and check where focus returns. Unplug the mouse Start at the address bar and press Tab. Watch focus Can you always see where you are? Complete a task Search, buy, sign up or send a form. 1 2 3 4 5 Unplug the mouse Start at the address bar and press Tab. 1 Watch focus Can you always see where you are? 2 Complete a task Search, buy, sign up or send a form. 3 Open overlays Try Escape and check where focus returns. 4 Fix and repeat Start with anything that blocks a task. 5

Watch for these failures: focus vanishing off screen, controls you can't reach, an order that jumps around, invisible menus that still receive focus, embedded players or maps that swallow the Tab key, and custom dropdowns that ignore arrows.

Content hidden only with opacity or off-screen positioning stays focusable. Use display: none, visibility: hidden or the hidden attribute when something should be unavailable.

Order, headings and page structure

A predictable order starts in the HTML. Write the page in the sequence you'd read it aloud: header, navigation, main content, sidebar, footer. Within a form, labels and fields should follow top to bottom and, in rows, left to right in left-to-right languages.

Two-column layouts need a decision. If a sidebar appears on the left visually but later in the source, a keyboard user jumps from the header to the right column and back. Either match the source to the layout or confirm that the jump still makes sense for the task.

Cookie banners and sign-up prompts deserve the same scrutiny. If one appears over the page, it should be reachable early, dismissible by keyboard and should not leave focus stranded behind it. A banner placed last in the source but shown first on screen can force someone through the whole page before they can close it.

Avoid very large numbers of links before the content. Collapsed navigation helps, as long as closed sections are truly removed from the tab order.

A sample test script

Writing the steps down makes the test repeatable by anyone on your team. For a small shop it might read like this:

Step Expected result
Press Tab on page load Skip link appears with a clear outline
Open the main menu with Enter Submenu opens; Escape closes it
Reach the search box and submit Results load and focus is sensible
Open a product and choose a size Arrow keys or Tab select options
Add to cart Confirmation is visible and announced
Open the cart panel and close it Focus returns to where you started
Submit checkout with an empty field Focus moves to the error, with text

Run it in at least two browsers and at a narrow width, since mobile menus often use different code from desktop ones. External keyboards are common with tablets and phones, so small-screen layouts need the same care.

Record failures with the element, the key pressed, what happened and what should have happened. That is enough for a developer to reproduce the problem without a meeting.

Fit it into your work

Add a keyboard pass to every release and every new third-party widget. Many problems come from plugins, so test before installing. If you maintain components, document their key behavior next to the code and cover it in automated browser tests that press real keys.

Keyboard support is one part of the wider picture in web accessibility explained. It also overlaps with layout choices: a design that reorders blocks across screen sizes, as in responsive web design, needs its focus order checked at each width. If your menus rely on script, the DOM explained gives useful background on how elements receive events.

Common questions

Should every element be focusable?

No. Only interactive things and a few scrollable or programmatic targets. Adding focus to plain paragraphs makes tabbing tedious.

Can I move focus automatically when a page loads?

Only with good reason, such as a page whose whole purpose is one search box. Unexpected jumps can skip content and confuse screen reader users.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in Websites & Hosting →

Latest news on this topic