tabindex attribute

Use tabindex to control whether an element can receive keyboard focus. Preserve the natural document order whenever possible.

What you will learn

What does tabindex do?

Interactive elements such as links, buttons, and form controls are naturally focusable. tabindex can add or remove keyboard focus from other elements, or place them in the document's focus order.

The recommended values

tabindex="0"
Adds an otherwise non-focusable element to the natural Tab order. Use this only when a custom control truly needs it.
tabindex="-1"
Allows focus from JavaScript, but skips the element during normal Tab navigation. Useful for moving focus to a dialog or heading after an update.
Positive values
Values such as 1 and 2 create a separate priority order. They become hard to maintain as the page changes, so avoid them.

Move focus to a result heading

<h2 id="results" tabindex="-1">Search results</h2>

// After displaying results:
document.querySelector('#results').focus();

Prefer native controls

Do not add tabindex to a div just to make it behave like a button. A native button already supports focus, keyboard activation, and the correct accessibility semantics. If a custom widget is unavoidable, implement its full keyboard behavior and visible focus style.

Natural order is usually enough

<button type="button">Previous</button>
<button type="button">Next</button>
<a href="/help.html">Help</a>

Checklist

Related pages