A keyboard user moves through a page with the Tab key, and each stop is a focus ring.
Where those rings land, and whether they are visible at all, decides whether the page is usable for them.
The good news is that most of it works by default, and the bad news is that it is easy to break with a small amount of CSS or JavaScript.
The previous lesson in this module covered "How Do alt Text and ARIA Help Screen Readers?", and the next lesson in this module is about "What Goes in head and Why Search Engines Care?".
How does focus move?
The focus moves in document order. That is the order in which the elements appear in the HTML, and it skips anything that cannot be focused.
Only a handful of things are focusable by default. There are the links with an href, there are the buttons, there are the form controls, and there is anything with a tabindex.
<a href="/docs">A link</a> <!-- focusable -->
<button>A button</button> <!-- focusable -->
<input> <!-- focusable -->
<div>A div</div> <!-- not focusable -->
<div tabindex="0">Focusable div</div> <!-- focusable -->There are two more keys that matter.
Holding Shift while you press Tab moves backwards, and it is how people escape a widget that they have landed in.
And the arrow keys move within a composite widget, such as the tabs of a tab bar or the options of a menu, while the Tab moves between the widgets.
That distinction is the model behind the ARIA patterns.
Why is the focus ring important?
The focus ring tells a keyboard user where they are.
It is the only indicator that focus is somewhere at all, and it is the one visual element that must never be removed.
The usual way that it gets removed is a well-meaning reset that sets outline: none on every element.
/* never do this */
* {
outline: none;
}
/* do this instead: keep the ring, and only change how it looks */
:focus-visible {
outline: 3px solid #800d4d;
outline-offset: 2px;
}The :focus-visible pseudo-class is the modern answer to a real tension.
A ring that appears on every mouse click looks untidy, but a ring that never appears makes the page unusable by the keyboard.
The :focus-visible shows the ring when the browser decides that the focus is keyboard-driven, and it hides it for a mouse click, which is exactly the behaviour that the people want.
That said, the :focus-visible is not universally supported, so the safe approach is to style the :focus as well, and only to refine it with the :focus-visible.
What is the tabindex for?
There are two values that cover almost every case.
The tabindex="0" makes an element focusable in the normal order, and the tabindex="-1" makes it focusable programmatically but not reachable with the Tab key.
<!-- a custom widget that behaves like a button -->
<div role="button" tabindex="0">Save</div>
<!-- a heading you want to jump to from JavaScript, but not tab through -->
<h2 id="results" tabindex="-1">Results</h2>The positive tabindex values are the trap.
The tabindex="1" puts an element before everything else, and the tabindex="2" after it, so that the tab order no longer matches the visual order.
With several positive values, the order becomes dependent on the numbers and not on the document, and that is disorienting and impossible to maintain.
The rule is simple. You use the 0 or the -1, and you never use a positive number.
There is one exception in the same spirit. A skip link usually uses the tabindex="-1" on the target, and it moves the focus to it with JavaScript, so that the target receives the focus but is not an extra stop in the tab sequence.
How do you make a custom widget keyboard-usable?
If you build a widget out of divs, then you have to implement the keyboard behaviour that the native element would have given you.
For something that behaves like a button, the minimum is the focusability and the two key handlers.
<div role="button" tabindex="0" id="fake-button">Save</div>
<script>
const button = document.querySelector('#fake-button');
button.addEventListener('keydown', (event) => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
save();
}
});
button.addEventListener('click', save);
</script>Notice the preventDefault on the space key. Without it, the page scrolls while the widget is being activated, and that is the most common bug in hand-rolled widgets.
And notice that the click handler is still there, so that a mouse click works too. The keyboard path and the pointer path both end at the same function.
The honest summary is that all of this is the work that the native element would have done for free.
You build with the <button> unless there is a reason that you cannot.
How do you move the focus deliberately?
The tabbing is the default way that the focus moves, and it follows the order of the document.
Sometimes that is not the order that you want. After a dialog opens, for example, the next Tab should stay inside the dialog, and when it closes, the focus should return to the button that opened it.
The tabindex attribute controls all of this, and it has three distinct meanings.
The tabindex="0" puts an element in the natural tab order, at the position where it appears in the document.
The tabindex="-1" removes an element from the tab order but it leaves it focusable programmatically.
A positive value, such as the tabindex="1", jumps the element to the front of the order, and it is almost never what you want.
<!-- a custom control that is reachable but not in the tab order -->
<div id="menu" tabindex="-1" hidden>
<button type="button">Profile</button>
<button type="button">Sign out</button>
</div>
<!-- a skip link, visible only when it is focused -->
<a class="skip-link" href="#main">Skip to content</a>.skip-link {
position: absolute;
inset-inline-start: -9999px;
}
.skip-link:focus {
inset-inline-start: 1rem;
inset-block-start: 1rem;
padding: 0.5rem 0.75rem;
background: #10243a;
color: #ffffff;
}That skip link is the standard first element on a page.
It is off screen until it receives the focus, and at that point it appears in the top corner. That is the way that a keyboard user can jump past a long navigation and straight to the content, instead of tabbing through every link on every page.
The rule for the positive tabindex values is simple. You never use them.
A single tabindex="1" puts that one element at the front of the page, and it quietly destroys the natural order for everybody that arrives after it, and the usual fix means rewriting the order with even more positive values.
Quick reference
- The focus follows the document order, and only the links, the buttons, the form controls, and the elements with a
tabindexare focusable. - You never remove the focus ring without replacing it, and you use the
:focus-visibleto refine it. - You use the
tabindex="0"to add a stop, thetabindex="-1"for the programmatic focus only, and you never use a positive value. - The arrow keys move inside a widget, and the Tab moves between the widgets.
- A custom widget needs the focus, the Enter and the Space handling, and a click handler.
Try it yourself: you put a form on a page, and you tab through it without touching the mouse. Then you tab through the page again, and you watch where the focus ring goes.
Finally, you add outline: none to the body, and you confirm how much harder the page becomes to use by the keyboard alone.