[tabindex]
Example
<div tabindex="1" onclick="save()">
Save
</div><button type="button" onclick="save()">
Save
</button>A native button is focusable in DOM order and works with Enter and Space without extra code.
Common mistakes
- Using
tabindex='1'and up, which breaks the natural tab order. - Adding
tabindex='0'to a<div>with a click handler instead of using a<button>.
Key points
- No element has a positive
tabindexvalue. - Clickable custom elements with
tabindex='0'also have a role and key handlers, or are replaced by<button>. - Elements focused only from JS, like a dialog heading, use
tabindex='-1'.
Interview questions
What is the difference between tabindex 0 and -1?
'0' adds the element to the tab order in DOM order. '-1' makes it focusable with focus() but skips it when tabbing.
Why are positive tabindex values bad?
They move elements ahead of everything else, so the tab order no longer matches the visual order.
Do I need tabindex on links and buttons?
No. Links with href, buttons and form controls are focusable by default.