# Keyboard, Focus & Skip Links

> Every user interaction available by mouse must also be reachable and operable by keyboard alone. That requires understanding the tab order, keeping the focus ring visible, and providing a skip link so keyboard users can bypass repeated navigation on every page load.

Subject: HTML | Lesson 35 of 42 | 7 min read
Interactive lesson: https://codewithanimation.in/html/keyboard-and-focus
Previous: [Native-First & ARIA](https://codewithanimation.in/html/native-first-and-aria.md) | Next: [Global Attributes & i18n](https://codewithanimation.in/html/global-attributes-and-i18n.md)

## What keyboard accessibility means

Tab order is the sequence in which the browser moves focus from element to element when a user presses Tab. By default it follows DOM order and only includes natively interactive elements: `<a>` with an `href`, `<button>`, `<input>`, `<select>`, `<textarea>`, and `<details>`. Non-interactive elements like `<div>` and `<p>` are skipped entirely.

The `tabindex` attribute controls whether an element participates in the tab order. `tabindex="0"` adds a custom element (like a div acting as a widget host) to the natural tab order at its DOM position. `tabindex="-1"` removes an element from the tab order while still allowing programmatic focus via JavaScript's `.focus()` method, which is useful for moving focus to a modal or announcement when it opens. A positive `tabindex` (1, 2, 3...) creates a separate priority queue that fires before the natural DOM order, which almost always produces a confusing sequence and should be avoided.

## Why it matters

WCAG 2.1.1 (Keyboard, Level A) requires that all functionality is available via keyboard alone. This is not optional. Users with motor disabilities, power users, and anyone on a keyboard-only device depend on it. A page that hides a feature behind a hover effect, or puts critical actions in a `<div>` without `tabindex`, silently excludes those users.

WCAG 2.4.7 (Focus Visible, Level AA) requires that the focused element is always visually identifiable. The single worst practice in web development is `*:focus { outline: none }` with no replacement. It removes the only visual cue keyboard users have for their position on the page. WCAG 2.4.11 (Focus Appearance, Level AA, added in WCAG 2.2) further requires the focus indicator to have sufficient size and 3:1 contrast ratio against adjacent colors.

## How to implement it

For focus styling, use `:focus-visible` instead of `:focus`. The `:focus` pseudo-class fires on keyboard navigation AND mouse clicks. `:focus-visible` fires only when the browser determines the user is navigating by keyboard or a programmatic `.focus()` call. This lets you show a clear ring for keyboard users while suppressing it for mouse users who find the ring visually noisy. The pattern: `button:focus-visible { outline: 2px solid #color; outline-offset: 2px; }` with `button:focus:not(:focus-visible) { outline: none; }`.

For skip links, add `<a href="#main-content" class="skip-link">Skip to main content</a>` as the absolute first child of `<body>`. Give `<main id="main-content">` the matching id. Style it visually hidden by default and visible on focus: `.skip-link { position: absolute; left: -9999px; } .skip-link:focus { left: 0; top: 0; }`. This satisfies WCAG 2.4.1 (Bypass Blocks) for keyboard users who do not use a screen reader's landmark shortcuts.

Use `tabindex="-1"` on dynamic content containers so JavaScript can move focus there programmatically when the content appears. After a route change in a single-page app, call `.focus()` on the new page's `<h1>` (with `tabindex="-1"` on it) so AT users know new content has loaded. After closing a modal, return focus to the button that opened it, so the user's position in the page is preserved.

## Common mistake: Removing the outline without providing a replacement

CSS resets and UI frameworks commonly include `* { outline: none }` or `:focus { outline: 0 }` to suppress the browser's default focus ring, which was historically inconsistent and visually jarring. Without a visible replacement, keyboard users lose all sense of where they are on the page. The correct approach is `*:focus:not(:focus-visible) { outline: none }` to suppress the ring on mouse clicks only, leaving it intact for keyboard navigation, plus a custom `:focus-visible` style that meets the contrast and size requirements of WCAG 2.4.11.

## Tip: Test keyboard accessibility in 30 seconds

Unplug or disable your mouse. Load any page you built. Press Tab repeatedly and watch where focus moves. Ask three questions: can you reach every interactive element, is there always a visible ring, and does the sequence make logical sense? If focus jumps around unpredictably, the DOM order does not match the visual layout and one of them needs to change. If the ring disappears, find the CSS that removes it. This test requires no tools and takes less than a minute.

## Try it: Try it

Step through the tab order on a page wireframe and see the focus ring walk each element, then watch the skip link appear when it receives focus.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/html/keyboard-and-focus

## Check yourself

### Question 1

An element has tabindex="-1". What does that mean for keyboard users?

- A. It is removed from the tab order but can still receive focus programmatically
- B. It is completely inaccessible to all users
- C. It receives focus before all other elements

Correct answer: A

`tabindex="-1"` means: skip this element when the user presses Tab, but allow JavaScript to programmatically call `.focus()` on it. This is the correct pattern for modals, live region targets, and SPA route-change headings.

- Why not B: tabindex="-1" only removes the element from the sequential tab order. JavaScript can still call .focus() on it, which is exactly what focus management in modals and route changes depends on.
- Why not C: Negative tabindex removes the element from the tab order entirely. Positive tabindex (like tabindex="1") receives focus before elements with tabindex="0" or no tabindex, but this is considered an anti-pattern.

### Question 2

Why is :focus-visible preferred over :focus for custom focus ring styles?

- A. :focus-visible shows the ring for keyboard navigation but not for mouse clicks
- B. :focus-visible has better browser support than :focus
- C. :focus-visible also triggers on hover, giving a larger interactive area

Correct answer: A

`:focus-visible` lets you show a clear focus ring for keyboard users while suppressing it on mouse clicks, which many designers find visually noisy. The ring is still present for accessibility when it is needed, but absent when it is not.

- Why not B: :focus has near-universal support and predates :focus-visible. The reason to prefer :focus-visible is behavioral, not compatibility.
- Why not C: :focus-visible has nothing to do with hover. It fires specifically when the browser determines the user is navigating by keyboard, based on its internal heuristics.

### Question 3

A skip link points to href="#main-content". What element should have id="main-content"?

- A. The <main> element
- B. The first <h1> on the page
- C. A hidden <div> placed at the top of the page

Correct answer: A

The `<main id="main-content">` element is the correct target. The skip link lets keyboard users jump past the repeated header and navigation directly to the primary content landmark.

- Why not B: The target can technically be any element with the matching id, but the correct semantic target for a skip-to-main-content link is the <main> element, which is the page's primary landmark.
- Why not C: The target should be the actual main content area, not a placeholder div. Jumping to a hidden div provides no useful navigation benefit.

## Remember this

- Tab order follows DOM order. Only natively interactive elements are included by default.
- `tabindex="0"` adds an element to the natural tab order; `tabindex="-1"` removes it but allows `.focus()`; positive values are an anti-pattern.
- Never remove `outline` without providing a `:focus-visible` replacement that meets WCAG 2.4.7 and 2.4.11.
- The skip link must be the first focusable element in `<body>` and its `href` must match the `id` on `<main>`.
