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

