# \[tabindex\]

> The `tabindex` HTML attribute sets the order for keyboard navigation of focusable elements, enhancing accessibility by determining when elements receive focus.

## What does tabindex do and when should I use tabindex 0 or -1?

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.

- Topic: Global Attributes
- Type: attribute
- Browser support: Chrome 1+, Chrome Android 18+, Edge 12+, Firefox 1.5+, Firefox Android 4+, Safari 4+, Safari iOS 3.2+

## Example

Wrong:

```html
<div tabindex="1" onclick="save()">
  Save
</div>
```

Right:

```html
<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.

## Related cards

- [button](https://wonderium.dev/a11y/button.md)
- [.focus()](https://wonderium.dev/js/focus.md)
- [:focus-visible](https://wonderium.dev/css/focus-visible.md)
- [\[inert\]](https://wonderium.dev/html/inert.md)
- [`<button>`](https://wonderium.dev/html/button.md)

---

- Level: [HTML](https://wonderium.dev/html.md)
- HTML version: https://wonderium.dev/html/tabindex
- Last updated: 2026-11-25
