Keyboard Access and Focus Management
Repair a dashboard that mouse users love and keyboard users cannot use: real controls, a skip link, visible focus, focus kept in view, and a dialog that gives focus back.
Contentsand lesson path
On this page
- Why it matters
- The starting page
- Step 1: use real controls
- Step 2: let users skip repeated navigation
- Step 3: keep focus visible
- Step 4: keep focus out from under sticky content
- Step 5: manage focus around the dialog
- Ways this repair goes wrong
- Check your understanding
- Apply it
- Review prompt
Lessons of Web Accessibility
- Who Accessibility Serves and How WCAG 2.2 Is Organized Concept · 20 min
- Keyboard Access and Focus Management Worked example · 25 min
- ARIA: When, Why and How Little Concept · 20 min
- Accessible Dialog and Tabs, Step by Step Worked example · 30 min
- Lab: Audit with axe, a Keyboard and a Screen Reader Lab · 30 min
- Check: Triage Accessibility Defects Knowledge check · 8 questions
What you will learn
- Implement keyboard-operable custom widgets (dialog, menu, tabs, combobox) following the WAI-ARIA Authoring Practices, with visible focus and correct focus management.
Prerequisites: Semantic HTML
01. Why it matters
A keyboard is the universal fallback input. People with tremor or repetitive strain injury use one because a mouse hurts; screen reader users use one because aiming a mouse pointer at a screen they cannot see is impractical; power users use one because it is faster. WCAG makes the requirement plain: success criterion 2.1.1 Keyboard says every function must be operable through a keyboard interface, and 2.4.7 Focus Visible says the element with focus must be visible.
The hard part is not pressing Tab. It is focus management: deciding where focus starts, where it goes next, and where it returns. This lesson repairs one small page until a keyboard-only user can do everything a mouse user can, and verifies the result with real key presses.
02. The starting page
Here is the shape of the broken page — a settings card with a remove action and a confirmation prompt. Everything looks right on screen:
<div class="card">
<div class="remove" onclick="removeMember()">Remove</div>
<div class="overlay" onclick="dismiss()">
<p>Remove this member?</p>
<div class="btn" onclick="confirmRemove()">Yes</div>
</div>
</div>Tab through it and the defects announce themselves: the div elements never receive focus, so the remove action and both choices are unreachable; the page ships with outline: none in its stylesheet, so even reachable controls give no clue where you are; the navigation sits above the content, so every page load means tabbing past the same nine links; and the "dialog" is a styled div — keyboard focus stays outside it, behind the overlay.
03. Step 1: use real controls
The single highest-value fix is to stop simulating controls. A <button> element is focusable in the tab order, fires click for Enter and Space, takes part in forms and exposes its disabled state — all for free. The <a href> element handles Enter and announces itself as a link. Every interactive element in the page becomes one of them:
<button type="button" id="remove-member">Remove member</button>
<dialog id="confirm">
<p>Remove this member?</p>
<button type="button" id="cancel">Cancel</button>
<button type="button" id="confirm-remove">Remove</button>
</dialog>The native <dialog> element is more than markup: showModal() makes the rest of the page inert, so Tab can only reach the dialog's controls (and, after the last one, the browser's own toolbar and address bar, before coming back to the dialog), and Escape closes it. That is the browser implementing the modal behaviour for you — reach for it before writing your own focus trap. Focus reaching the browser toolbar is expected, not a leak: the page behind stays unreachable.
04. Step 2: let users skip repeated navigation
A skip link is the first tab stop on the page and jumps straight to the main content, so keyboard users do not walk through the navigation on every page. It stays hidden until it receives focus:
.skip-link { position: absolute; left: -9999px; }
.skip-link:focus { position: static; }<a class="skip-link" href="#main">Skip to content</a>
<!-- navigation ... -->
<main id="main" tabindex="-1">
<!-- page content -->
</main>Two details matter. The <main> target carries tabindex="-1", so it can take focus programmatically when the link is followed, allowing screen readers to move their announcement to the main region. And the link must be genuinely visible when focused — a skip link hidden with opacity: 0.01 fails the spirit of the fix. Here is the keyboard walk of settings.html, driven by real key presses. The snippet assumes a Playwright page already open on the file; the audit lab prints a complete script with the browser set-up and the pinned playwright 1.64.1:
async function describe(page) {
return page.evaluate(() => {
const el = document.activeElement;
if (!el) return '(none)';
const tag = el.tagName.toLowerCase();
const id = el.id ? `#${el.id}` : '';
const text = el.textContent?.trim().slice(0, 30) ?? '';
return [`<${tag}>`, id, text].filter(Boolean).join(' ');
});
}
await page.keyboard.press('Tab');
console.log('Tab 1 ->', await describe(page));
await page.keyboard.press('Enter');
const target = await page.evaluate(() => document.activeElement.id);
console.log(`Enter -> follows the skip link, focus on #${target}`);
await page.keyboard.press('Tab');
console.log('Tab 2 ->', await describe(page));Tab 1 -> <a> Skip to content
Enter -> follows the skip link, focus on #main
Tab 2 -> <button> #remove-member Remove memberRead the walk like a screen reader user would: one stop to skip the header, then straight into the form. The navigation links never interrupt.
05. Step 3: keep focus visible
Many stylesheets delete the focus ring because it appears after mouse clicks. The keyboard-safe replacement is :focus-visible, which the browser applies for keyboard focus and usually not for pointer input:
a:focus-visible, button:focus-visible, input:focus-visible {
outline: 3px solid #1d4ed8; outline-offset: 2px;
}Keep outline: none away from :focus entirely: an invisible focus indicator is a WCAG 2.4.7 failure and it blinds sighted keyboard users. Give the ring real contrast against its surroundings — a faint 1 px grey line technically exists and practically does not.
06. Step 4: keep focus out from under sticky content
The page has a sticky header. When Tab moves focus to a control that scrolled underneath it, the focused element is invisible even though a ring is drawn — the new WCAG 2.2 criterion 2.4.11 Focus Not Obscured (Minimum) exists precisely for this. CSS fixes it in one line: reserve space at the top so scrolled-to elements stop below the header.
html { scroll-padding-top: 4.5rem; }scroll-padding-top applies to every scroll-into-view, including fragment links, so the skip link and the header stop fighting each other.
07. Step 5: manage focus around the dialog
Opening a modal dialog must move focus into it — otherwise the next Tab press wanders off behind the overlay — and closing it must return focus to the control that opened it, or keyboard users are stranded at the top of the document. With the native <dialog>, the wiring is four lines:
const dialog = document.getElementById('confirm');
document.getElementById('remove-member').addEventListener(
'click', () => dialog.showModal());
document.getElementById('cancel').addEventListener('click',
() => dialog.close());
document.getElementById('confirm-remove').addEventListener(
'click', () => dialog.close());Now drive it with real key presses — no mouse, no clicks — and watch where focus lands after each step. The script continues the walk from step 2 on the same page, with the same describe(): Tab 2 left focus on the Remove member button, so the first key is Enter:
const steps = [
['Enter (opens dialog)', 'Enter'],
['Tab (inside dialog)', 'Tab'],
['Escape', 'Escape'],
];
for (const [label, key] of steps) {
await page.keyboard.press(key);
console.log(label.padEnd(22), '->', await describe(page));
}Enter (opens dialog) -> <button> #cancel Cancel
Tab (inside dialog) -> <button> #confirm-remove Remove
Escape -> <button> #remove-member Remove memberEvery promise of the modal pattern, kept without a line of focus-trap code: Enter opens, focus lands on the dialog's first control, Tab never reaches the page behind (after Remove it visits the browser toolbar, then returns to Cancel), and Escape both closes and restores focus to the opener.
08. Ways this repair goes wrong
09. Check your understanding
10. Apply it
In the skill's practice task, the keyboard-only pass over three flows is a hard requirement. Script your walk the way this lesson did: press Tab and Enter through one flow, log document.activeElement at each step, and read the log against two questions — can every control be reached, and is focus always visible and in view? Then unplug the mouse for one full session in your own app and note every moment you reached for it.
11. Review prompt
Without looking back: name the four repairs this lesson made and the WCAG criterion each one serves. Explain what tabindex="-1" does on the skip link's target, what :focus-visible changes compared with :focus, and what showModal() gives you that a hand-built overlay does not.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.