Home
Bookmarks
Leagues
LEARN
Courses
Career Paths
Assessments
Tutorials
Arcade
Glossary
GROW
Certifications
Log in
Glossary
Log inSign up
Courses/Accessible Interface Foundations/Keyboard paths and visible focus
Level 1 · Accessible interaction basics

Keyboard paths and visible focus

10 min read
10 exercises
250 XP
Start lesson

Ready to test what you learned?

Complete the lesson quiz and earn 250 XP.

Start quiz

Topics in this lesson

Sequential Focus Navigation & DOM OrderThe Crime of 'outline: none'The Three Tabindex Rules: 0, -1, and Never > 0Skip Links: Bypassing Blocks (WCAG 2.4.1)Modal Dialogues: The Sacred Focus TrapEscape Key Dismissal: WCAG 1.4.13 & 2.1.2Composite Widgets: Arrow Keys vs Tab KeySingle-Page App (SPA) Route TransitionsTarget Size & Spacing (WCAG 2.5.5 / 2.5.8)Master Challenge: Auditing a Broken Modal Dialogue

From Course

📘
Accessible Interface FoundationsBeginner · 3 lessons
Keyboard paths and visible focus

Lesson

Keyboard paths and visible focus

Sequential Focus Navigation & DOM OrderThe Crime of 'outline: none'The Three Tabindex Rules: 0, -1, and Never > 0Skip Links: Bypassing Blocks (WCAG 2.4.1)Modal Dialogues: The Sacred Focus TrapEscape Key Dismissal: WCAG 1.4.13 & 2.1.2Composite Widgets: Arrow Keys vs Tab KeySingle-Page App (SPA) Route TransitionsTarget Size & Spacing (WCAG 2.5.5 / 2.5.8)Master Challenge: Auditing a Broken Modal Dialogue
Start lesson
Complete lesson and earn 250 PX
Exercise #1

Sequential Focus Navigation & DOM Order

Millions of people navigate digital products exclusively using a keyboard—including users with motor disabilities, tremors, power users with wrist fatigue, and screen reader users.

The Primary Mechanics:

  • Tab Key: Moves focus forward to the next interactive element.
  • Shift + Tab: Moves focus backward to the previous interactive element.
  • Enter / Space: Activates buttons, links, toggles, and checkboxes.

The Golden Law of Reading Order: Keyboard tab order is determined strictly by DOM order in the HTML, NOT visual CSS layout. If you use CSS flex-direction: row-reverse or grid-template-areas to make a button appear first visually when it is last in the DOM, a keyboard user will experience jarring, disorienting jumps across the screen.

Exercise #2

The Crime of 'outline: none'

For decades, naive designers complained that the default browser focus ring was 'ugly' and demanded developers write:

*:focus { outline: none; } /* NEVER DO THIS */

Stripping focus rings without an accessible replacement is the digital equivalent of turning off the computer screen for keyboard users. They press Tab and have zero idea where on the page their focus is located. Clicking blind can trigger irreversible actions like account deletion or unverified charges.

The Modern Standard: WCAG 2.4.7 (Focus Visible) & 2.4.11 (Focus Appearance):

  • Focus rings must be clearly visible.
  • They must have at least a 3:1 contrast ratio against the adjacent background.
  • Use :focus-visible in CSS so rings appear when navigating by keyboard, but stay subtle on mouse clicks.
Exercise #3

The Three Tabindex Rules: 0, -1, and Never > 0

The tabindex HTML attribute controls programmatic and sequential focus behavior:

  1. tabindex="0": Inserts an element into the natural keyboard tab order in its current DOM position. (Used when creating accessible custom interactive widgets).
  2. tabindex="-1": Removes an element from the sequential keyboard tab order, but makes it programmatically focusable via JavaScript (element.focus()). (Used for modal containers, error alert banners, and custom roving focus widgets).
  3. tabindex="1" or greater (POSITIVE TABINDEX):

    Strict Anti-Pattern: Positive tabindex values hijack the browser's tab flow, forcing focus to jump arbitrarily before all natural elements. Never use positive tabindex!

Exercise #4

Skip Links: Bypassing Blocks (WCAG 2.4.1)

Imagine visiting a news website where the header contains a top banner, 45 navigation links, a currency converter, and a weather widget.

Every time a keyboard user visits a new article, they would have to press the Tab key 52 times just to read the first sentence of the article!

The Skip Link Pattern: A 'Skip to Main Content' link is the very first focusable element in the HTML:

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

In CSS, it is visually hidden off-screen by default, but slides smoothly into view when focused via keyboard Tab (.skip-link:focus { top: 0; }). Pressing Enter instantly jumps focus directly into <main>.

Exercise #5

Modal Dialogues: The Sacred Focus Trap

When a modal or dialogue opens, sighted users see a dark dimmed backdrop obscuring the background. But for a keyboard user, unless you trap focus, pressing Tab will navigate behind the modal into hidden background links and buttons!

The Three Mandatory Modal Focus Rules:

  1. Initial Focus Transfer: When the modal opens, immediately move keyboard focus into the first focusable element inside the modal (or the modal container itself).
  2. The Focus Trap: When focus reaches the last interactive element inside the modal, pressing Tab wraps focus back to the first element. Focus must never leak into the background page.
  3. Return Focus on Close: When the user closes the modal (via Escape or Close button), immediately restore focus to the trigger button that originally opened it.
Exercise #6

Escape Key Dismissal: WCAG 1.4.13 & 2.1.2

Any transient overlay, dropdown menu, modal dialogue, or tooltip triggered by user interaction must be dismissible via the Escape key without moving the pointer.

Why?

  • Users using screen magnification (zoomed in to 400%) often see only a tiny corner of an overlay. They cannot see or reach the visual 'X' close button in the opposite corner.
  • Keyboard users trapped in an accidental dropdown must have a reliable, standardized escape hatch.

Implementation Checklist: Listen for keydown where e.key === 'Escape'. Close the active overlay, clean up event listeners, and return focus cleanly.

Exercise #7

Composite Widgets: Arrow Keys vs Tab Key

In modern web applications, not every interactive item should be reached via the Tab key.

The W3C WAI-ARIA Authoring Practices Guide (APG) defines the Composite Widget Pattern:

  • Tab Key: Moves focus into and out of the widget container.
  • Arrow Keys (Left/Right/Up/Down): Navigate between items within the widget.

Examples of Composite Widgets:

  • Tab Lists (role="tablist"): Tab key focuses the active tab; Left/Right arrow keys switch between tabs.
  • Radio Groups (<fieldset> with radio buttons): Tab key enters the group; Up/Down arrow keys select options.
  • Dropdown Menus (role="menu"): Tab key exits the menu; Up/Down arrow keys move between menu items.

This pattern prevents a tablist with 15 tabs from cluttering the global tab order.

Exercise #8

Single-Page App (SPA) Route Transitions

In traditional multi-page websites, clicking a link triggers a full browser reload. The browser automatically resets focus to the top of the new document and announces the <title>.

In Single-Page Apps (React, Next.js, Vue), client-side routing updates the DOM dynamically without a page refresh.

The Blind Navigation Trap: If an app does not manage focus during route changes:

  • The keyboard user clicks 'Profile', the URL changes, and new content appears.
  • But focus remains trapped on the clicked link in the header or gets lost in the body.
  • Screen readers stay completely silent, leaving blind users wondering if the click worked.

The Fix: On route change, update document.title, move programmatic focus to the new page's <h1> (using tabindex="-1"), or announce the new page name via an aria-live region.

Exercise #9

Target Size & Spacing (WCAG 2.5.5 / 2.5.8)

Accessibility is not limited to desktop keyboards; it directly impacts touch screens and motor control.

Users with hand tremors, arthritis, parkinson's, or situational constraints (walking, riding a bumpy bus) struggle with microscopic buttons.

The Strict Target Size Standards:

  • WCAG 2.2 Level AA (Criterion 2.5.8 - Target Size Minimum): Interactive targets must be at least 24 x 24 CSS pixels, or provide sufficient spacing so the target plus margin reaches 24px.
  • WCAG Level AAA / Apple HIG / Google Material (2.5.5): Targets should be at least 44 x 44 CSS pixels (Apple) or 48 x 48dp (Google).

CSS Technique: An icon can visually appear as a 16px glyph, but by adding padding or pseudo-elements (::after), its clickable touch target expands to 48px.

Exercise #10

Master Challenge: Auditing a Broken Modal Dialogue

Put all keyboard and focus principles into practice in a real-world debugging audit:

A QA tester files a Critical P0 Accessibility bug for your team's newsletter subscription popup:

  1. When the modal opens, keyboard focus stays on the page background.
  2. When tabbing inside the modal, focus leaks out into the footer links behind the dark backdrop.
  3. Pressing the Escape key does nothing; users must use a mouse to find a tiny 12px 'X' button.
  4. When the modal is finally closed via mouse, focus jumps to the top of the body, disorienting the user.

How do you architect the complete, robust remediation?