Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 2 of 6 · Worked example · 25 min · revision 3

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

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:

HTML · settings-broken.html (an example with defects, fixed below)
<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:

HTML · settings.html
<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.

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:

CSS · styles.css
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.

CSS · styles.css
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:

JavaScript · settings.html (script)
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:

JavaScript · keyboard-dialog.mjs
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));
}
Expected output
Enter (opens dialog)   -> <button> #cancel Cancel
Tab (inside dialog)    -> <button> #confirm-remove Remove
Escape                 -> <button> #remove-member Remove member

Every 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

Practice check · 3 questions

Keyboard paths and focus

Three situations from the repaired page.

Q1. A keyboard user activates the skip link "Skip to content", which points at <main id="main" tabindex="-1">. Where does the next Tab press land?
Q2. A colleague sets tabindex="1" on the search field so it is the first stop in the tab order. What is the better change?
Q3. While the <dialog> is open, Tab moves only between its two buttons (and the browser toolbar) and never reaches the page behind. What enforces that?
Practice only: not graded, not saved, not evidence.

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.

Sign in to mark reading complete

Prove this skill

8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check