DEV Community

Cover image for Keyboard Navigation Accessibility: A Complete WCAG Guide
Denis Omerovic
Denis Omerovic

Posted on Edited on Originally published at getaccessguard.com

Keyboard Navigation Accessibility: A Complete WCAG Guide

Originally published at AccessGuard.

Not everyone uses a mouse

It's easy to assume that everyone navigates the web the way you do - clicking with a mouse or tapping on a touchscreen. But many people rely on a keyboard as their primary or only means of interaction.

People with motor disabilities may use a keyboard, a switch device, or voice recognition software - all of which interact with web pages through the same keyboard interface. Screen reader users navigate almost entirely by keyboard. Power users often prefer keyboard shortcuts for speed. And anyone with a temporarily broken mouse or trackpad needs keyboard access as a fallback.

Keyboard accessibility isn't an edge case. It's the foundation that makes all other assistive technologies work. If your site isn't keyboard accessible, it's likely inaccessible to screen readers, voice control, and switch devices as well.

How keyboard navigation works

Understanding the basics of keyboard navigation helps you test and build accessible interfaces.

The essential keys

  • Tab - moves focus to the next interactive element (links, buttons, form fields)
  • Shift + Tab - moves focus to the previous interactive element
  • Enter - activates the focused element (follows links, submits buttons)
  • Space - activates buttons, toggles checkboxes, opens select menus
  • Arrow keys - navigate within components (radio groups, dropdown menus, tabs, sliders)
  • Escape - closes modal dialogs, menus, and popups

With just these keys, a user should be able to reach and operate every interactive element on your page. If they can't, you have a keyboard accessibility problem.

Focus order

When a user presses Tab, focus moves through interactive elements in DOM order - the order elements appear in your HTML source code. This is why it's important that your visual layout matches your source order. If CSS visually rearranges elements (using flexbox order, grid placement, or absolute positioning), the tab order might not match what a sighted user expects.

The fix is to change the source order, not to patch it with tabindex. A positive tabindex (1 or higher) pulls an element ahead of everything else on the page and almost always makes the order worse. WCAG 2.4.3 Focus Order (Level A) asks that focus moves "in an order that preserves meaning and operability" (WCAG 2.2).

<!-- Broken: tabindex forces this link ahead of the whole header -->
<a href="/offer" tabindex="3">Today's offer</a>

<!-- Fixed: move it in the source, and leave tabindex out -->
<a href="/offer">Today's offer</a>

Focus indicators - making focus visible

When a keyboard user tabs through a page, they need to see which element currently has focus. This visual indicator is called the focus ring or focus indicator.

Browsers provide a default focus indicator (usually a blue or dotted outline), but many websites remove it with CSS like outline: none or *:focus { outline: 0; } because designers find it aesthetically displeasing. This is one of the most harmful accessibility patterns on the web - it makes keyboard navigation essentially unusable.

Best practices for focus indicators

  • Never remove focus indicators without providing a replacement. If you remove the default outline, you must add a custom one that's equally visible.
  • Make focus indicators visible against all backgrounds. A thin blue outline works on white but may be invisible on a blue background. Use a combination of outline offset, contrasting colors, or multiple indicators.
  • Use :focus-visible for the best of both worlds. The :focus-visible pseudo-class only applies focus styles when focus was triggered by keyboard (not mouse click), reducing visual noise for mouse users while keeping indicators for keyboard users.

A good custom focus indicator might look like this:

/* Broken: removes the indicator and replaces it with nothing */
*:focus { outline: none; }

/* Fixed: a visible ring for keyboard users, no ring on mouse clicks */
:focus-visible {
  outline: 3px solid currentColor;
  outline-offset: 2px;
}

WCAG 2.2 added a measurable requirement for focus appearance: an indicator at least as large as a 2 CSS pixel perimeter of the component, with a contrast ratio of at least 3:1 between the focused and unfocused states. That is criterion 2.4.13 Focus Appearance, and it is Level AAA, not AA (WCAG 2.2). The AA requirement is 2.4.7 Focus Visible, which asks only that the indicator be visible, so treat 2.4.13 as the bar to design to rather than as an AA failure in a client report. WCAG 2.2 also added 2.4.11 Focus Not Obscured (Minimum) at Level AA, which a sticky header or cookie banner covering the focused element will fail. For a sticky header, scroll-padding-top set to the header's height keeps the focused element in view when the browser scrolls to it:

/* Fixed: leave room for a 72px sticky header when scrolling to focus */
html { scroll-padding-top: 72px; }

Skip links - bypassing repeated content

Imagine having to Tab through an entire navigation menu with 30 links on every single page before reaching the main content. For keyboard users, this is exhausting and time-consuming.

Skip links solve this by providing a link at the very top of the page that jumps directly to the main content area. It's typically hidden until it receives focus, so it doesn't affect the visual design:

<!-- Broken: the target cannot take focus, so the next Tab returns to the nav -->
<a href="#main">Skip to content</a>
...
<div id="main">

<!-- Fixed: the target is a real landmark and can take focus -->
<a href="#main" class="skip-link">Skip to content</a>
...
<main id="main" tabindex="-1">

The CSS hides it visually until focused:

.skip-link { position: absolute; top: -40px; left: 0; }
.skip-link:focus { top: 0; }

This is a WCAG Level A requirement: 2.4.1 Bypass Blocks asks for "a mechanism" to "bypass blocks of content that are repeated on multiple web pages" (WCAG 2.2). It takes minimal effort to implement, and the tabindex="-1" on the target is the detail most versions leave out.

Keyboard traps - the cardinal sin

A keyboard trap occurs when a user can Tab into an element but cannot Tab out of it. They're stuck, unable to navigate to other parts of the page without using a mouse - which they may not be able to do.

Common sources of keyboard traps include:

  • Embedded content - iframes, embedded maps, and third-party widgets can trap focus inside their boundaries
  • Custom rich text editors - editors that capture Tab for indentation can prevent users from tabbing away
  • Modal dialogs without proper focus management - a modal should trap focus within itself while open (this is correct behavior) but must release focus when closed
  • Autofocusing scripts - JavaScript that forces focus back to a specific element whenever the user tries to leave

WCAG criterion 2.1.2 (No Keyboard Trap, Level A) prohibits this. When you build a component that handles Tab itself, such as an editor or a modal, give it a documented way out: Escape, or Tab once the user leaves the last field.

Making custom components keyboard accessible

Native HTML elements like <button>, <a>, and <input> are keyboard accessible by default - they receive focus, respond to Enter/Space, and announce themselves to screen readers. Problems arise when developers build custom interactive elements from non-interactive HTML.

A clickable <div> with a JavaScript click handler is invisible to keyboard users - it doesn't appear in the tab order, and pressing Enter does nothing. To make it accessible, you'd need to add:

  • tabindex="0" - puts the element in the natural tab order
  • role="button" - tells assistive technologies it's a button
  • A keydown handler for Enter and Space
  • Proper focus styles

Or you could just use a <button> element and get all of this for free. The simplest rule for keyboard accessibility: use native HTML elements whenever possible. A <button> instead of a <div onclick>. An <a href> instead of a <span onclick>. Native elements handle keyboard interaction, focus management, and screen reader announcements automatically.

Testing keyboard accessibility

This guide is about building it. To check a finished page, follow our step by step manual keyboard accessibility test: what to press, what to look for at each stop and the WCAG criterion to write in the report.

How AccessGuard helps with keyboard accessibility

Our keyboard and focus checker looks for the causes a machine can find in the rendered page: divs and spans with click handlers that a keyboard cannot reach, outline: none rules that leave no visible focus, sticky headers and banners that can cover the focused element, positive tabindex values, and controls hidden with opacity: 0 that a keyboard still reaches.

It cannot tell you whether the focus order makes sense, whether a widget traps focus or whether a menu works end to end, and no automated tool can confirm a site conforms to WCAG. Run a free accessibility audit on a page to find the countable problems, then do the manual test for the rest.

Top comments (0)