HTML Focus, Tabindex, Autofocus and Inert
Start with natural focus order
Keyboard focus identifies the current interaction target. Links with href and enabled buttons are naturally focusable. Tab usually follows document order; tabindex="0" adds an otherwise focusable-by-tab element to that order, but it does not give it button behavior. Prefer native controls.
<button type="button" id="first">First action</button><a href="#notes">Read notes</a><p id="notes">Practice keyboard navigation.</p>Tab moves from the button to the link in ordinary sequential navigation.
Programmatic focus and autofocus
tabindex="-1" allows programmatic focus without adding an ordinary Tab stop. The focus() method moves focus to the selected element. autofocus requests initial focus when appropriate; use it sparingly because unexpected focus can disorient users. Avoid positive tabindex values because they reorder the natural sequence.
<button type="button" id="jump">Focus result</button><p id="result" tabindex="-1">Review completed.</p><script> document.getElementById("jump").addEventListener("click", () => { document.getElementById("result").focus(); });</script>The button transfers focus to the paragraph, which is not an ordinary Tab stop.
Temporarily deactivate a region
The boolean inert attribute makes a region and its ordinary descendants noninteractive, excluding them from sequential focus and the accessibility tree. It does not automatically hide or visibly dim them. Explain inactive state visibly; the example uses text and opacity, with a control outside the inert region.
<p>Editing is temporarily unavailable.</p><div inert style="opacity: 0.5"> <button type="button" id="edit">Edit draft</button></div><button type="button" id="back">Return</button>Edit cannot receive focus; Return remains usable.
Common mistakes and practice
Never make all available recovery controls inert. Inert is not equivalent to disabled or hidden and does not by itself remove form controls from submitted data. Keep a visible focus indicator and test the actual keyboard order.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Focus, Tabindex, Autofocus and Inert — Knowledge Check
Apply the interaction concepts taught in this lesson.
Which is naturally keyboard focusable?
What does tabindex="-1" enable?
Does tabindex="0" give a div button behavior?
What does inert do to ordinary descendants?
What does autofocus request?