liveBook · Manning

Tabindex

As explained in Architecting Accessibility

An HTML global attribute that controls an element’s keyboard focusability and position in sequential focus navigation.

If pressing Tab makes focus jump somewhere unexpected—or leaves you unsure where you are—the interface becomes harder to use. The HTML `tabindex` attribute controls whether an element can receive keyboard focus and how it participates in sequential focus navigation. It can add an element to the normal tab order, remove it from that order, or use a positive value to force a custom order. section 5.3.1 section 5.3.2

Prefer native interactive elements: links, buttons, and form fields already receive focus and have browser-provided focus indicators. Adding `role="button"` to a `div` changes how assistive technology identifies it, but does not make it keyboard-focusable or operable. A custom button also needs `tabindex="0"`, a visible focus indicator, and keyboard and mouse behavior. section 2.5.2 section 5.3.1

Choose a value to match the intended interaction:

  • `tabindex="0"` places an element in the normal tabbing sequence. In a tabbed interface, the selected tab can be the tab stop. section 2.5.2
  • `tabindex="-1"` removes an element from sequential tabbing while allowing JavaScript to focus it when needed. An unselected tab can use `-1`; a modal dialog heading can also receive programmatic focus this way. section 2.5.2 section 5.3.2
  • Avoid positive values to repair a confusing layout. They force a custom tab order; instead, make the DOM order logical and check that CSS visual reordering has not made it appear to jump. section 5.3.1 section 5.3.2

One tab stop in a tabbed interface

A selected tab uses `tabindex="0"`, while another tab uses `tabindex="-1"`. When selection changes, JavaScript updates those values, the `aria-selected` states, and which panel is hidden. Arrow-key behavior and focus movement still need implementation: `tabindex` alone does not build a working tab component. section 2.5.2

function setActiveTab(selectedTabIndex, { setFocus = true } = {}) {
    tabs.forEach((tab, i) => {
        const selected = i === selectedTabIndex;
        tab.setAttribute('aria-selected', String(selected)); #A
        tab.tabIndex = selected ? 0 : -1; #B
        panels[i].hidden = !selected; #C
    });

    if (setFocus) {
        tabs[selectedTabIndex].focus({ preventScroll: true }); #D
    }
}

See how selection updates `tabIndex`, panel visibility, and focus together. section 2.5.2

Finally, can someone see where focus has landed? Keep the browser’s focus indicator or replace it with a clearer, high-contrast one. Adding an element to the tab sequence without making its focus visible leaves keyboard users to find their place by trial and error. section 5.3.1

How Architecting Accessibility covers it

How deep it goes
Defines tab-order effects and works through tab selection and programmatic focus, without treating the attribute as a substitute for keyboard behavior.
Examples
Uses a tabbed interface, a programmatically focused modal heading, and a visually reordered navigation header.
What it assumes
Assumes familiarity with HTML elements and introduces the attribute’s focus effects before applying it to ARIA and JavaScript components.
What you can do after
Apply it via HTML and JavaScript to manage tab stops while checking focus visibility and logical navigation order.
How it connects
Connects keyboard focus to semantic HTML, ARIA, JavaScript, focus indicators, DOM order, and CSS visual reordering.

Also explained in

Compare how 2 books explain tabindex →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Architecting Accessibility. The section links point to the parts of the book it draws on.