Sub topic 7
Focus indicators for keyboard users
This chapter assumes you know basic HTML, CSS selectors and pseudo-classes, and how to open your browser's developer tools. You should also have read the earlier chapters on button and link styling.
What a focus indicator is
Focus is the browser's record of which element will receive the next keystroke. Mouse users see a pointer. Keyboard users have no pointer, so the focus indicator is their only sign of where they are on the page.
Pressing Tab moves focus to the next interactive element. Pressing Shift+Tab moves it back. Enter activates a link. Enter or Space activates a button. If the indicator is missing, a keyboard user cannot tell which control they are about to activate.
Do not remove the default outline
Browsers draw a default outline on focused elements. A common mistake is to remove it because the designer finds it ugly:
/* Do not do this */
a:focus,
button:focus {
outline: none;
}This rule leaves keyboard users with no cue at all. If you want a different look, replace the outline with a better one. Never delete it without a substitute.
Use :focus-visible
The :focus pseudo-class matches whenever an element has focus, including after a mouse click. That is why designers often dislike it: a button shows a ring after every click.
The :focus-visible pseudo-class lets the browser decide. It matches when focus should be shown, which in practice means keyboard navigation. Clicking a button with a mouse usually does not trigger it. Text inputs still show it, because the user needs to see where they are typing.
The button and link styles from earlier chapters already use it:
.btn:focus-visible {
outline: 3px solid #1d4ed8;
outline-offset: 3px;
}
a:focus-visible {
outline: 3px solid currentColor;
outline-offset: 2px;
}Here outline-offset pushes the ring away from the element's edge. The gap keeps the ring from merging with a button's border, which matters when both are the same blue. Using currentColor on links makes the ring match the text color, so it works on any background the link sits on.
Make the indicator easy to see
A focus ring is a visual cue, so the contrast rules from the earlier chapter apply to it. The WCAG 2.2 guidance expects the indicator to have at least a 3:1 contrast ratio against the colors next to it. Keep these points in mind:
- Use an outline at least 2px thick. 3px is easier to see.
- Check the ring against the page background, not only against the button.
- On dark sections, a dark blue ring may vanish. Override the color there.
- Do not rely on a subtle color shift alone, such as a slightly darker background.
A two-color ring works on any background. This version draws a white inner ring and a dark outer ring:
.btn:focus-visible {
outline: 2px solid #ffffff;
outline-offset: 2px;
box-shadow: 0 0 0 5px #1e3a8a;
}Whatever the page color, one of the two rings stands out. Note that box-shadow can be hidden in Windows High Contrast mode, while outline is kept. That is why the main ring stays an outline here.
Check that nothing hides the ring
Even a good indicator can be clipped. A parent with overflow: hidden cuts off an outline that extends past its edge. If a ring looks cut off, increase the parent's padding or reduce outline-offset.
Also check that the focus order matches the visual order. Do not use positive tabindex values such as tabindex="3". They override the natural order and confuse users. Use tabindex="0" only for custom controls that need to join the order. Better still, use a real <button> or <a href>, which are focusable by default.
Test it yourself
- Put your mouse aside and press Tab from the top of the page.
- At each stop, confirm you can see which element has focus.
- Press Shift+Tab to go backward and check again.
- Open developer tools, select an element and force the
:focus-visiblestate in the Styles panel to inspect the ring.
Exercise
A designer asks you to remove the blue ring that appears around buttons after they are clicked with a mouse. What should you change, and what should you avoid?
Show answer
Style the ring with :focus-visible instead of :focus, so it appears for keyboard navigation but usually not after a mouse click. Avoid outline: none on :focus with no replacement, because that removes the cue keyboard users depend on.
Exercise
Why does the example use outline-offset: 3px on .btn:focus-visible?
Show answer
The offset leaves a gap between the button's edge and the ring. Without it, a blue ring touching a blue border blends in and is harder to see.