HTML Keyboard Navigation and Focus
Follow the DOM order
Tab typically moves between sequentially focusable controls in document order when no positive tabindex values intervene. Shift+Tab reverses direction. Native buttons support Enter and Space; links normally activate with Enter.
<a href="#help">Help</a><button type="button">Continue</button><label> Search <input type="search"></label><p id="help">Help text</p>Use Tab from the page start: link, button, search field.
Focus a destination
tabindex="-1" makes a suitable element programmatically focusable without adding it to the usual sequential Tab order. A skip link can target a main region. Use a meaningful destination ID and preserve visible focus indicators.
<a href="#content">Skip to content</a><nav> <a href="#other">Other links</a></nav><main id="content" tabindex="-1"> <h1>Lesson</h1> <p id="other">Practice</p></main>The target can receive focus without becoming a regular Tab stop.
Keep focus visible
The browser usually supplies a focus outline. CSS :focus-visible selects elements when the browser determines that a visible focus indication is appropriate. This minimal CSS adds a clear outline rather than suppressing it. Check contrast against surrounding colors.
<style> button:focus-visible { outline: 3px solid #005fcc; outline-offset: 3px; }</style><button type="button">First</button><button type="button">Second</button>Tab between the buttons and inspect the outline.
Common mistakes and practice
Avoid positive tabindex for rearranging natural order. Check the full workflow without a mouse, including modal closure and focus restoration. A keyboard trap prevents users leaving a component through standard or documented keys; modal containment is acceptable only with a working exit.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Keyboard Navigation and Focus — Knowledge Check
Apply the accessibility concepts taught in this lesson.
Which key usually advances sequential focus?
Which key combination reverses the usual Tab direction?
What does tabindex="-1" permit on a suitable element?
Which key normally activates native buttons?
What should a focus style do?