Keyboard Navigation and Focus Management
Ensure all interactive elements are reachable by Tab, manage focus programmatically in dialogs and routing, and create visible focus indicators.
Keyboard Navigation and Focus Management is a free Frontend Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Frontend Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Keyboard Navigation Matters
Many users can't use a mouse: motor disabilities, screen reader users, power users who prefer the keyboard. If your app isn't keyboard navigable, it's broken for them.
Tab and Shift+Tab
Tab moves focus to the next interactive element. Shift+Tab moves to the previous. Order follows DOM order by default — don't fight this; let the DOM be your tab order.
What's Focusable by Default
Native interactive elements are focusable: a[href], button, input, select, textarea, summary. Custom elements need tabindex.
tabindex Values
tabindex="0": included in tab order at DOM position. tabindex="-1": focusable via JS only (script focus, not Tab). tabindex=">0": anti-pattern — creates confusing focus order, never use.
<!-- Custom button (still: use real <button>!): -->
<div role="button" tabindex="0" onkeydown="...">Custom</div>
<!-- Programmatically focusable, skipped by Tab: -->
<div tabindex="-1" id="error-summary"></div>Visible Focus Indicator
Never remove focus outlines without providing a replacement. WCAG requires a visible indicator on every focusable element.
/* BAD: hides focus for everyone */
*:focus { outline: none; }
/* GOOD: only show focus from keyboard (not mouse click) */
:focus-visible {
outline: 2px solid #2563eb;
outline-offset: 2px;
}:focus-visible
The :focus-visible pseudo-class fires only when the focus came from a keyboard, not a mouse click. Lets you keep clean mouse styles while showing keyboard focus.
Skip-to-Content Link
The first focusable element should be a skip link, so keyboard users can jump past the header nav directly to main content.
<a href="#main" class="skip-link">Skip to main content</a>
...
<main id="main" tabindex="-1">...</main>
/* CSS */
.skip-link {
position: absolute; left: -10000px;
}
.skip-link:focus {
left: 1rem; top: 1rem;
background: white; padding: 0.5rem 1rem;
}Focus Trap in Modals
When a modal opens, focus should be trapped inside — Tab from the last element wraps to the first, Shift+Tab from the first wraps to the last. Pressing Escape closes the modal.
// On modal open:
modal.querySelector('h2').focus(); // move focus into modal
lastFocused = document.activeElement;
// On Tab in modal: detect leaving and wrap to first focusable.
// On Escape: close modal and restore focus:
lastFocused.focus();Use the dialog Element
HTML's native <dialog> handles focus trap, Escape close, and modal scrim automatically.
<dialog id="signup">
<h2>Sign up</h2>
<form method="dialog">
<input name="email" required>
<button>Submit</button>
<button value="cancel">Cancel</button>
</form>
</dialog>
<script>
document.getElementById('signup').showModal(); // opens with focus trap
</script>Focus Management After Route Change
SPAs don't reload the page, so screen reader users have no signal that navigation happened. Move focus to the new page's h1 or main on route change.
// On route change:
document.getElementById('main')?.focus();
// or focus the new h1:
newPage.querySelector('h1')?.setAttribute('tabindex', '-1');
newPage.querySelector('h1')?.focus();Focus Order Should Match Visual Order
Make sure DOM order matches the visual flow. If CSS reorders elements (flex order, grid placement, position absolute), the tab order won't match — confusing for everyone.
Test by Putting Your Mouse Away
Most useful accessibility test: unplug your mouse and try to use your entire app with Tab, Shift+Tab, Enter, Space, and arrow keys. If you get stuck, your users get stuck.
Quick Check
Which CSS pseudo-class lets you style the focus outline only when focus came from the keyboard, not a mouse click?
Recap: Keyboard Navigation
Native interactive elements are focusable by default. Use tabindex=0 only on custom widgets; never tabindex>0. Always show focus — use :focus-visible. Add a skip-to-content link. Trap focus in modals and restore on close. Use native <dialog> when possible. Move focus to h1/main on route change. Test by putting your mouse away.
Frequently asked questions
Is the “Keyboard Navigation and Focus Management” lesson free?
Yes — the full text of “Keyboard Navigation and Focus Management” is free to read here on the web, and the Frontend Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Frontend Academy course, upgrade to CoddyKit PRO.
What will I learn in “Keyboard Navigation and Focus Management”?
Ensure all interactive elements are reachable by Tab, manage focus programmatically in dialogs and routing, and create visible focus indicators. You practise Frontend Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Frontend Academy?
No prior experience is required. Frontend Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Keyboard Navigation and Focus Management” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Frontend Academy lesson?
Yes. Every Frontend Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- WCAG Levels A AA AAA
- Semantic HTML as the Foundation
- ARIA Roles and Attributes
- Keyboard Navigation and Focus Management