Course contentsShow
← Web Development
Lesson 1044 of 2,98219. Keyboard Navigation and FocusFree lesson

The tabindex Attribute Fundamentals

How tabindex controls focus behavior: positive values, zero, and negative one for different focus scenarios.

The tabindex Attribute Fundamentals

What you'll learn: How the tabindex attribute controls which elements can receive keyboard focus and in what order users navigate through them.

What Is tabindex?

While you've learned that certain elements are naturally focusable (like links and buttons), the tabindex attribute gives you fine-grained control over focus behavior. Think of it as a traffic controller for keyboard navigation—it determines whether an element can receive focus and when.

The Three tabindex Values

Positive Values (tabindex="1" or higher)

Setting a positive number creates a custom tab order, where lower numbers receive focus first. Elements with tabindex="1" come before tabindex="2", regardless of their position in the document.

<button tabindex="3">Third</button>
<button tabindex="1">First</button>
<button tabindex="2">Second</button>

Warning: This disrupts natural navigation flow and confuses users. Avoid positive values in nearly all cases.

Zero (tabindex="0")

Makes an element focusable and places it in the natural tab order based on its document position. This is the most common use case—making non-interactive elements keyboard accessible without disrupting flow.

<div tabindex="0">I'm now keyboard focusable!</div>

Negative One (tabindex="-1")

Makes an element programmatically focusable but removes it from the natural tab order. Users can't tab to it, but JavaScript can focus it when needed (like opening a modal).

<div tabindex="-1" id="modal">
  Hidden from tab order but JS can focus me
</div>

The Right Choice

Use tabindex="0" to add elements to keyboard navigation. Use tabindex="-1" for elements that only need programmatic focus. Avoid positive numbers.

Key Takeaway: The tabindex attribute controls focus behavior with three values: positive numbers create custom (problematic) tab orders, 0 adds elements to natural tab order, and -1 enables programmatic-only focus.