[tabindex]

Term side of the Card.
Topic: Global Attributes
Unit title: [tabindex]
Categories: Attribute
Explanation side of the Card

Explanation: The attribute that controls whether an element can receive focus. The '0' value adds it to the Tab order in DOM position and '-1' makes it focusable only by script. Positive values force a custom order and are avoided because they break the expected navigation.

Browser compatibility:
  • The chrome browser supports this feature starting from version 1.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 1.5.
  • The safari browser supports this feature starting from version 4.
Availability: 

Example

Wronghtml
<div tabindex="1" onclick="save()">
  Save
</div>
Righthtml
<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 tabindex value.
  • 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.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy