Lesson 4 of 9
Keyboard Navigation & Focus Management
All interactive elements must be operable with a keyboard alone - many users cannot use a mouse (motor disabilities, screen reader users, power users). HTML natively makes links, buttons, and form inputs keyboard-focusable. Custom interactive elements (div-based buttons, modals) need tabindex and keyboard event handlers.
Keyboard Navigation Rules
- Tab moves to next focusable element. Shift+Tab moves backward. Enter/Space activates buttons and links.
- tabindex='0' - Adds element to natural tab order. Use for custom interactive elements (div-based buttons).
- tabindex='-1' - Focusable by JavaScript only (element.focus()). Useful for managing focus in modals and SPAs.
- tabindex > 0 - AVOID. Creates custom tab order that confuses users. Natural DOM order is almost always correct.
- NEVER remove focus outlines - :focus { outline: none; } is an accessibility violation. Use :focus-visible instead for keyboard-only styling.
- :focus-visible - Shows outline ONLY for keyboard users, not mouse clicks. The modern best practice for focus styling.
- Focus trapping in modals - Tab must cycle within the modal, not reach elements behind it. <dialog> does this automatically.
- Arrow keys for widgets - Tabs, menus, and tree views should use arrow keys internally. Tab enters the widget, arrows navigate within.
Diagram
Loading diagramâŚ
Ensure all interactive elements can be reached and activated via keyboard alone
Key Takeaways
- All interactive elements must be operable with a keyboard alone - many users cannot use a mouse (motor disabilities, screen reader users, power users).
- Tab moves to next focusable element. Shift+Tab moves backward. Enter/Space activates buttons and links.
- tabindex='0' - Adds element to natural tab order. Use for custom interactive elements (div-based buttons).
- tabindex='-1' - Focusable by JavaScript only (element.focus()). Useful for managing focus in modals and SPAs.
Finished this lesson?
0 of 9 in Module 9