Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 38 ยท Keyboard and Focus

Focus Order and tabindex

Learn how the browser decides the keyboard focus order, what tabindex 0 and -1 do, why positive tabindex is harmful, and how CSS reordering can break the tab order.

When you press Tab, the browser moves focus to the next element in the focus order. By default that order follows the DOM source order, and nothing else. This is the key idea: if your HTML is in a sensible order, the tab order is sensible too, with no code required. WCAG 2.4.3 (Focus Order, Level A) asks for exactly that: focus order must preserve meaning and operability.

This lesson explains how the order is determined, what tabindex does with its three kinds of values, and how visual tricks such as flexbox order and absolute positioning can silently break it.

Source order rules

Given this markup, Tab visits the elements top to bottom, in the order written:

HTML
<a href="/one">One</a>
<button>Two</button>
<input aria-label="Three">

Visual layout does not change it. If CSS moves the button to the left of the link on screen, Tab still goes link, then button. Keyboard users then jump back and forth across the page, which is disorienting and can fail 2.4.3 and 1.3.2.

The three kinds of tabindex

Value Effect
tabindex="0" The element joins the natural tab order, at its position in the source
tabindex="-1" The element is focusable with script (el.focus()) or a click, but is skipped by Tab
tabindex="1" or higher Avoid. The element jumps to the front of the order, before all zeros

tabindex="0" is for custom widgets that really are interactive. tabindex="-1" is for things you will focus programmatically: a heading after a route change, an error summary, the target of a skip link, or the active item of a roving group. Positive values create a second ordering system that every other part of the page, and every future edit, must be aware of.

tabindex values

tabindex 0 In the natural order
tabindex -1 Focus by script only
Positive Do not use

Fix order in the HTML instead.

1 / 2

See the order

Tab order visualiser

You will find that C, the button with tabindex="-1", is skipped, and the other four follow the source. Click C with the mouse, and it does take focus, because -1 still allows pointer and script focus.

When CSS reorders things

Flexbox order, grid placement, flex-direction: row-reverse, absolute positioning and floats change the visual order without changing the DOM order. WCAG calls this out because it can break the match between what you see and what Tab does.

Visual order vs focus order

Where you must reorder visually, change the DOM instead. If that is impossible, keep reordering modest and test the focus order. Browsers are slowly adding reading-flow properties to align the two, but do not rely on them yet.

Hidden and off-screen content

Elements with display: none, visibility: hidden or the hidden attribute are removed from the focus order. Content merely moved off-screen with left: -9999px remains focusable, so keyboard users can tab to something they cannot see. For closed menus and drawers, use hidden, inert, or visibility: hidden.

The inert attribute is especially handy: it makes an element and its descendants non-focusable, non-clickable and invisible to assistive technology. Use it on the page behind an open modal.

inert removes content from the tab order

Focus order checklist

  1. Write the HTML in the order users should read and operate it.
  2. Use tabindex="0" and -1 only, never positive.
  3. Check that CSS layout does not make Tab jump around.
  4. Remove off-screen or closed content from the focus order.
  5. Test with the keyboard after every layout change.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. By default, what determines the Tab order?

    Browsers follow source order, so write HTML in a sensible sequence.
  2. 2. What does tabindex="-1" do?

    -1 means not reachable by Tab but focusable by script or click, useful for focus management.
  3. 3. Why avoid positive tabindex?

    Positive values jump ahead of everything else and are hard to maintain.
  4. 4. How do you take a closed off-canvas menu out of the tab order?

    Moving or fading leaves elements focusable. hidden, inert or visibility hidden remove them.

Summary

  • Focus order follows source order, so order your HTML meaningfully.
  • Use tabindex="0" to add custom controls and -1 for script-only focus; never positive values.
  • CSS reordering can desynchronise visual and focus order.
  • Use hidden, inert or visibility: hidden for content that must not be reachable.

Next: Visible Focus Styles