tabindex
Decides whether an element can be focused with the Tab key and in what order.
- Applies to
- All HTML elements
Example
#<style>
[tabindex]:focus { outline: 2px solid #a78bfa; outline-offset: 2px; }
.tile { display: inline-block; padding: 8px 12px; margin: 4px; border-radius: 8px; background: #1f1f23; }
</style>
<p>Click the preview and press the Tab key:</p>
<div class="tile" tabindex="0">A tile in the Tab order</div>
<div class="tile" tabindex="-1">A tile skipped by Tab</div>
<button type="button">A regular button</button>Definition and usage
#The tabindex attribute controls keyboard navigation. Links, buttons and form fields are reachable with the Tab key right away. Other elements, such as <div>, have to be opted in.
tabindex="0" adds the element to the natural Tab order, which follows the order in the code. tabindex="-1" makes the element unreachable with Tab, but you can still focus it in JavaScript through focus(). This is useful in dialogs, for example.
Positive values (1, 2β¦) force a custom order ahead of all other elements. This almost always hurts accessibility, because the order stops matching what is visible on screen.
Syntax
#<element tabindex="0 | -1 | number">β¦</element>Values
#0
The element joins the Tab order according to its position in the code.-1
The element is not reachable with the Tab key, but it can be focused from a script.positive number
Forces a custom order. Better avoided.
Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Instead of adding
tabindex="0"to a clickable<div>, just use a<button>. You get Enter and Space handling for free. - A focused element must be visibly highlighted. Do not remove the
outlineborder without a replacement.
Related entries
#- <button>A clickable button: submits a form, clears it, or runs an action on the page.
- inertDisables interaction with an element and its descendants: clicking, focus and screen readers.
- idA unique identifier for an element across the whole document.
- <a>A link to another page, a file, an email address, or a place on the same page.
- autofocusSets focus on an element right after the page loads or after a dialog opens.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.