HTML global attributeGlobal attributes

tabindex

Decides whether an element can be focused with the Tab key and in what order.

Applies to
All HTML elements
On this page

Example

#
HTML
<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>
Preview

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

#
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 outline border without a replacement.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.