Roving tabindex

On this page 8

Roving tabindex is a keyboard focus management technique in which only one item of a composite widget carries tabindex="0" while every other item carries tabindex="-1", so the whole widget occupies a single stop in the Tab sequence and the arrow keys move focus between its items. In web accessibility, it is the standard way to make toolbars, tab lists, menus, radio groups, grids and tree views behave like their desktop counterparts for keyboard and screen reader users.

The name describes the mechanism: the value 0 roves from item to item as the user navigates. The technique is recommended by the WAI-ARIA Authoring Practices and is one of two accepted patterns for focus inside composite widgets, the other being aria-activedescendant.

How roving tabindex moves focus

Roving tabindex works by swapping tabindex values on keypress and then calling focus() on the newly selected element. When the user presses an arrow key, the script sets the current item to -1, sets the target item to 0 and moves real DOM focus to the target, which makes the browser scroll it into view and announce it through the accessibility tree.

Because exactly one item has tabindex="0", pressing Tab from outside the widget lands on the last active item, not always on the first one. Pressing Tab again leaves the widget entirely. This preserves the expected one-stop-per-widget model that keeps long pages navigable.

The following table shows the values each item holds at every stage of a typical interaction in a three-button toolbar.

StepBold buttonItalic buttonUnderline button
Initial load0 (focusable by Tab)-1-1
User presses Right Arrow-10 (focused)-1
User presses Right Arrow again-1-10 (focused)
User presses Tab, then Shift+Tab-1-10 (focus restored here)
Tabindex state during arrow key navigation

The value -1 does not make an element inert: it removes the element from sequential navigation while keeping it programmatically focusable. That distinction is the foundation of the whole pattern.

Keyboard conventions for composite widgets

Each composite role carries expected key bindings that screen reader users and keyboard users already know from operating systems. Implementing roving tabindex without the matching keys produces a widget that technically works but violates user expectations.

Widget roleMovement keysAdditional keysWraps at edges
toolbarLeft and Right ArrowHome, EndOptional
tablistLeft and Right Arrow (horizontal)Home, End; Enter or Space if manual activationUsually yes
radiogroupAll four arrow keysSpace checks the focused radioYes
menu and menubarUp and Down or Left and Right ArrowHome, End, Escape, type-aheadUsually yes
gridAll four arrow keysHome, End, Ctrl+Home, Ctrl+End, Page Up, Page DownNo
treeUp and Down ArrowRight expands, Left collapses, asterisk expands siblingsNo
Expected keys by widget type

The aria-orientation attribute tells assistive technology which axis applies, and the script should match the declared orientation. A vertical tab list that responds only to Left and Right Arrow confuses users whose screen reader announced it as vertical.

Roving tabindex compared with aria-activedescendant

The main alternative is aria-activedescendant, where DOM focus stays on a container and an attribute points to the visually active child. Both satisfy WCAG 2.1.1 Keyboard, but they differ in where focus physically sits and in how browsers and assistive technology react.

AspectRoving tabindexActive descendant attribute
Location of DOM focusOn the active item itselfOn the container or a text input
Automatic scrollingBrowser scrolls the item into viewScript must scroll the item manually
Focus indicatorNative :focus and :focus-visible styles applyCustom styling of the active item is required
Best fitToolbars, tab lists, radio groups, gridsComboboxes and listboxes where typing continues in an input
Requirement for childrenEach child must be focusableEach child needs a unique id
Two patterns for focus inside a composite widget

As a rule, roving tabindex is the more robust default because it relies on real focus, which every browser and screen reader supports consistently. aria-activedescendant is preferable when focus must remain in an editable field, as in an autocomplete combobox.

Implementing the pattern correctly

A sound implementation has a small number of moving parts: one keydown handler on the container, a stored index of the active item and a function that updates tabindex values. Handling the event on the container rather than on each child keeps the code compact and works for dynamically added items.

  1. Give the container the correct ARIA role, such as toolbar or tablist, and an accessible name.
  2. Set tabindex="0" on the initially active item, usually the selected tab or checked radio, and -1 on the rest.
  3. On arrow keys, update both values and call focus() on the new item; call preventDefault() to stop page scrolling.
  4. When items are added, removed or disabled, recompute the active index so that exactly one item stays at 0.
  5. Keep focus visible with a clear focus indicator that meets the contrast requirements of WCAG 2.2.

Disabled items deserve a deliberate decision. The Authoring Practices allow disabled items to remain focusable in menus and toolbars so that users can discover them, while a tab list usually skips them. Whatever the choice, it must be consistent across the widget.

Common mistakes and how to fix them

Most roving tabindex defects come from state drifting out of sync between the DOM and the script. The table pairs frequent symptoms with their usual causes.

SymptomLikely causeFix
Widget cannot be reached with TabNo item has tabindex 0Guarantee one item at 0 after every update
Tab stops on every itemAll items left at 0Set non-active items to -1
Arrow keys scroll the pageDefault action not canceledCall preventDefault in the handler
Focus lost after item deletionActive item removed from DOMMove focus to a neighbor before removal
Screen reader reads wrong positionMissing role or set sizeUse correct roles; add aria-setsize if virtualized
Roving tabindex defects and remedies

Another frequent error is applying the pattern to content that is not a composite widget, such as a list of links in a navigation bar. Links in navigation should stay in the normal tab order, because users expect Tab to reach each of them and screen readers already offer their own link lists.

Testing roving tabindex

Verification requires manual keyboard testing, because automated checkers such as axe-core or WAVE can detect missing roles but cannot judge arrow key behavior. A tester should Tab into the widget, move with every documented key, Tab out and Shift+Tab back to confirm that focus returns to the last active item.

Screen reader testing with NVDA, JAWS and VoiceOver confirms that the role, name, state and position, such as “tab, 2 of 4, selected”, are announced. Browse mode in Windows screen readers switches to focus mode automatically for most composite roles, so testers should check that this switch happens.

In everyday language

Keyboard users move around a web page with the Tab key, one stop at a time. If a toolbar with twenty buttons gave every button its own stop, getting past it would take twenty presses. Roving tabindex groups the whole toolbar into one stop.

Once inside, the arrow keys move between the buttons, much like moving a highlight across a row of icons on a television remote. The page remembers where the user was, so coming back lands on the same button. The result is faster, more predictable navigation for people who do not use a mouse.

Seen in the real world

A team building a web-based document editor ships a formatting toolbar with eighteen buttons. During an accessibility audit, a keyboard-only tester reports that reaching the document body takes eighteen Tab presses, and a screen reader user complains that the toolbar is announced button by button without any grouping.

The developers give the container role="toolbar" with the label “Formatting”, set the Bold button to tabindex="0" and the others to -1, and add Left Arrow, Right Arrow, Home and End handling. After the change, reaching the document takes one Tab press, the toolbar is announced as a single labeled group, and returning to it restores focus to the last used button. The retest records no remaining keyboard failures for the toolbar under WCAG 2.1.1.