Keyboard, Focus and tabindex
How keyboard focus works in HTML: what takes focus by default, what tabindex 0, -1 and positive values do, and why native controls beat custom ones.
Keyboard, Focus and tabindex
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Focus is the keyboard's cursor. The focused element receives key presses, and Tab and Shift+Tab move focus through the focusable elements in order.
- Links with `href`, buttons, form controls, `summary`, `iframe` and `contenteditable` elements are focusable by default. A `div`, a `span` and an `a` with no `href` are not.
- `tabindex="0"` adds an element to the natural tab order. `tabindex="-1"` makes it focusable by script or click but skips it on Tab. A positive value jumps the queue and should not be used.
- `tabindex` only makes something focusable. It adds no role and no Enter or Space handling, so it does not turn a `div` into a button.
- Elements that are `hidden`, `inert` or `disabled` leave the tab order. `aria-hidden` and `opacity: 0` do not.
Why focus exists
A mouse user can point at anything. A keyboard user can’t, so the browser keeps one element as “the current one” and sends key presses there. That element is focused. Press Tab and focus moves to the next focusable element, Shift+Tab moves back. Press Enter on a focused link and it follows; press Space on a focused button and it clicks.
Screen reader users, switch users and many people with motor impairments depend on this. It is also the reason you see a ring around the thing you tabbed to, covered in accessible CSS.
The core problem is simple: only some elements take focus, and the browser decides which ones, based on your tags.
What is focusable by default
I ran a page of mixed elements through Chromium 159, Firefox 159 and a WebKit build, pressing Tab until the sequence repeated. These took focus without any tabindex:
- an
awith anhref button,input,select,textarea(unlessdisabled)summaryinside adetails- an element with
contenteditable - content inside an
iframe - in Chromium and Firefox, a scrollable region such as
overflow: autowith no focusable children inside it
These did not: a plain div, a span, an a with no href, a disabled button, and anything inside an element with hidden or inert. Calling .focus() on the a with no href did nothing. This is why links need an href and buttons are button.
One engine difference to know about: the WebKit build I tested skipped links and buttons on Tab entirely, which is consistent with Safari’s long-standing default of needing Option+Tab or a setting. I did not test real Safari, so check it yourself before promising anything about it.
The three tabindex values
<div tabindex="0">In the Tab order</div>
<div tabindex="-1" id="results">Focusable by script only</div>
<div tabindex="2">Jumps ahead. Avoid.</div>
0puts the element in the natural order, which is document order. Use it for a genuinely custom control.-1keeps the element out of the Tab sequence but lets scripts and mouse clicks focus it. In the test, clicking atabindex="-1"div focused it, and so did.focus(). Moving focus to a heading after a route change is the classic use.- Positive numbers go first, in ascending order, then everything else follows in document order. In the test,
tabindex="1"and"2"elements were reached before the first link in every engine. A tab order that departs from the reading order can fail WCAG’s focus order criterion (2.4.3), so don’t use them.
tabindex is not a role
The video’s point, and it is the one to remember, is that tabindex handles only the first step: letting a user reach the control. In a test a <div role="button" tabindex="0"> was focusable, but Enter and Space produced zero click events. A real <button> fired both. You would have to write the key handlers yourself. Reach for tabindex only when no native element fits, and then read ARIA roles, states and properties, since the control needs a role and states too.
Two more details. aria-hidden="true" does not remove focusable children from the Tab order, so the buttons inside one stayed reachable in the test. And opacity: 0 leaves the element focusable, so hide with hidden or inert. The dialog element shows the platform managing focus for you: in the same kind of test, a modal kept Tab inside itself.
What the video covers
This is episode 4 of Google’s A11ycasts, “Controlling focus with tabindex” (September 2016, about 5 minutes). Its description says native elements like button give you styling, keyboard support and semantics, that tabindex is how you make a custom control focusable, that the episode covers the states of tabindex, when to use them, and one important gotcha. I could not read a transcript, so I cannot say what the gotcha is. The video is old, though the attribute’s behaviour has not changed. The test results above are mine, and the roving tabindex pattern is in episode 6.
Where this fits
The accessibility tree gave elements their roles. This lesson gives them keyboard access. ARIA roles, states and properties is next, for the cases where native HTML cannot express a widget.
Check yourself
You add `tabindex="0"` to a `div` with a click handler. Can a keyboard user now use it?
They can reach it, but not use it. Focus works, yet Enter and Space do nothing unless you write key handlers, and the element still has no button role. In a test a `div role="button" tabindex="0"` registered zero clicks from Enter and Space, while a real `button` registered two. Use a `button`.
What is the difference between `tabindex="0"` and `tabindex="-1"`?
`0` puts the element in the Tab sequence in document order. `-1` takes it out of the sequence but keeps it focusable through `element.focus()` and mouse clicks, which is what you want for a heading you move focus to after navigation or for a roving widget.
A page has `tabindex="2"` on a footer link and `tabindex="1"` on a sidebar link. Where does Tab start?
At the `tabindex="1"` sidebar link, then the `tabindex="2"` one, then the rest of the page in document order. In a test, positive values went first in every engine. This makes the order differ from the reading order, so use document order instead.
Why might a long scrolling `div` need no `tabindex` in Chrome and Firefox?
Both browsers make a scrollable region without focusable children keyboard focusable in their own right, so keyboard users can scroll it with arrow keys. In a test the scroller took focus in Chromium and Firefox but not in the WebKit build. Add `tabindex="0"` and a label if you must support browsers that don't.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.