Sub topic 7
Focus indicators for keyboard users
This chapter assumes you know basic HTML elements and attributes, CSS selectors and pseudo-classes, the box model, and how to inspect elements in your browser's developer tools. It covers how to make keyboard focus visible on buttons and links, which mistakes break it, and how to check your work.
What focus is and why it needs a visible cue
Focus is the state of the one element that currently receives keyboard input. When you press Tab, the browser moves focus to the next focusable element. Pressing Enter on a focused link follows it. Pressing Enter or Space on a focused button activates it.
Mouse users see where the pointer is. Keyboard users have no pointer, so the focus indicator is their only sign of where they are on the page. This group includes:
- people who cannot use a mouse because of a motor impairment
- screen magnifier and switch device users
- power users who prefer the keyboard
- anyone whose trackpad has stopped working
Without a visible indicator, a keyboard user must guess what Enter will do. In the previous chapters, hover feedback told mouse users that an element was interactive. The focus indicator is the matching signal for the keyboard.
Which elements receive focus
Native <a href> and <button> elements are focusable by default and respond to the right keys. A <div> or <span> with a click handler is not focusable, so a keyboard user cannot reach it. This is one more reason to pick the element by what it does, as discussed earlier.
| Element | Focusable by default | Activated by |
|---|---|---|
| `<a href="...">` | Yes | Enter |
| `<a>` without `href` | No | Not applicable |
| `<button>` | Yes | Enter or Space |
| `<button disabled>` | No | Not applicable |
| `<div onclick>` | No | Mouse only |
The last two rows matter. A disabled button drops out of the tab order entirely. The disabled-state chapter returns to this, using aria-disabled when the button must stay reachable.
The default outline and why people remove it
Browsers draw a default focus ring on focused elements. The cause of most focus problems is a reset rule like this one:
/* Do not do this */
a:focus,
button:focus {
outline: none;
}Designers write it because the default ring can look clumsy or clash with the design. The result is that keyboard users lose all feedback. If you remove the default outline, you must replace it with an indicator that is at least as visible.
:focus versus :focus-visible
The :focus pseudo-class matches whenever an element has focus, including after a mouse click. That is why designers disliked the default ring: clicking a button with the mouse would leave a ring on it.
The :focus-visible pseudo-class matches only when the browser decides the focus indicator should be shown. In practice:
- Keyboard focus matches
:focus-visible. - A mouse click on a button usually does not.
- Clicking into a text input does match, because typing needs a visible caret and cue.
This lets you show a strong ring for keyboard users without a ring after a mouse click. Browsers differ slightly in the heuristics, so test in more than one.
.btn:focus-visible {
outline: 3px solid #1d4ed8;
outline-offset: 3px;
}This is the rule from the button example in this tutorial. Because the selector is :focus-visible and no rule removes the default outline for plain :focus, browsers that support :focus-visible (all current ones do) show your ring for keyboard focus only. Do not write a separate :focus rule that sets outline: none to "hide the mouse ring". If you need that behavior, use the pattern in the next section.
A safe pattern for removing mouse-click rings
If you want to suppress the ring on mouse clicks while keeping it for the keyboard, base your rule on :focus-visible being absent:
button:focus:not(:focus-visible) {
outline: none;
}
button:focus-visible {
outline: 3px solid #1d4ed8;
outline-offset: 3px;
}The first rule only applies when focus is present and the browser has decided not to show it. If a browser did not understand :focus-visible, the whole first selector would be invalid and ignored, so the default outline would stay. That makes this pattern fail safe.
Choosing outline, not border or box-shadow
You can build an indicator with outline, box-shadow, or border. Each has different consequences.
| Property | Affects layout | Follows border-radius | Works in forced colors mode | Notes |
|---|---|---|---|---|
| `outline` | No | Yes in current browsers | Yes | Best default choice |
| `box-shadow` | No | Yes | No, shadows are removed | Needs an outline fallback |
| `border` | Yes | Yes | Yes | Changing width shifts neighbors |
Use outline first. It does not take space, so adding it does not move other content. Use outline-offset to place a gap between the element and the ring. A positive offset leaves a visible gap, which keeps the ring readable even when the button has a filled background of the same color.
Windows High Contrast and other forced colors modes replace your colors with a limited system palette. They drop box-shadow but keep outline. If you use a shadow for a decorative glow, pair it with a transparent outline so something remains in forced colors mode:
.btn:focus-visible {
box-shadow: 0 0 0 4px rgba(29, 78, 216, 0.35);
outline: 3px solid transparent;
outline-offset: 3px;
}In normal mode the transparent outline is invisible and the shadow shows. In forced colors mode, the browser recolors the outline to a system color, so the indicator survives.
Making the indicator visible enough
A ring needs enough contrast against what is next to it. The WCAG 2.2 guidelines ask for a contrast ratio of at least 3:1 for the indicator against adjacent colors (success criterion 1.4.11, non-text contrast). The focus appearance criterion (2.4.13, level AAA) goes further and asks for an indicator at least as thick as a 2 CSS pixel perimeter with a 3:1 change between focused and unfocused states. A 3px ring is a sensible target.
Two problems come up often:
- The ring matches the background. A blue ring around a blue button is only visible if there is a gap. That is why the button styles use
outline-offset: 3px: the gap shows the page background between the button and the ring. - The ring is clipped. A parent with
overflow: hiddencan cut off an outline that extends outside the child. Give the child room, use a negative offset so the ring is drawn inside, or move the overflow rule.
On a dark background, a dark blue ring fails. Use a color that contrasts with the surface, or use two rings, one light and one dark, so one of them is visible on any background:
.on-any-background:focus-visible {
outline: 3px solid #ffffff;
box-shadow: 0 0 0 6px #111827;
}Focus indicators for links
The link styles from the previous chapter already include a focus rule:
a:focus-visible {
outline: 3px solid currentColor;
outline-offset: 2px;
}Using currentColor makes the ring match the link's text color, which keeps it consistent when a link appears in different colored sections. Check that the resulting color still has 3:1 contrast with the background around the link. Visited links get a purple text color, so the ring follows it.
Keeping focus order and visibility sensible
A good indicator is wasted if focus lands somewhere unexpected. Keep these points in mind:
- Tab order follows the order of elements in the HTML. Reordering with CSS (
order,position,float) does not change it, so visual and source order can disagree. - Avoid positive
tabindexvalues such astabindex="5". They override the natural order and are hard to maintain. Usetabindex="0"to add an element to the natural order andtabindex="-1"to allow focus by script only. - Sticky headers and cookie banners can cover the focused element. The CSS property
scroll-margin-topon focusable elements, orscroll-padding-topon the scroll container, keeps focused items from hiding under a fixed header.
html {
scroll-padding-top: 5rem; /* height of the sticky header plus a gap */
}A complete example
This page combines the pieces: a native link and button, a visible ring that appears for keyboard focus only, and a fallback for forced colors mode.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Focus indicators</title>
<style>
body { font-family: system-ui, sans-serif; padding: 2rem; }
a {
color: #1d4ed8;
text-decoration: underline;
text-underline-offset: 0.15em;
}
.btn {
display: inline-flex;
align-items: center;
min-height: 44px;
padding: 0 1.25rem;
border: 2px solid #1d4ed8;
border-radius: 8px;
background: #1d4ed8;
color: #ffffff;
font: inherit;
font-weight: 600;
cursor: pointer;
}
a:focus-visible,
.btn:focus-visible {
outline: 3px solid #1d4ed8;
outline-offset: 3px;
}
</style>
</head>
<body>
<p>Read the <a href="#guide">getting started guide</a> first.</p>
<button type="button" class="btn">Save changes</button>
</body>
</html>Open the file, click in the page background, then press Tab. The link shows a ring, and the next Tab moves the ring to the button. Click the button with the mouse and compare: the ring should not appear for the click, but it should for keyboard focus.
Testing focus indicators
Test with the keyboard, not only with DevTools.
- Put the pointer aside and press Tab repeatedly through the page. Every link and button must show a visible ring, and you must always know where you are.
- Press Shift+Tab to go backward and confirm the order makes sense.
- Activate controls: Enter on a link, Enter and Space on a button.
- Check the ring against each background it appears on, including dark sections, images, and hover states.
- In DevTools, use the "Emulate a focused page" and force-state options (
:focus-visible) to inspect the rule without tabbing. - Turn on forced colors mode (a DevTools rendering option emulates it) and confirm the ring is still there.
- Zoom to 200% and check that the ring is not clipped.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| `outline: none` with no replacement | No visible focus | Add a `:focus-visible` rule |
| Ring same color as the fill, no offset | Ring invisible | Add `outline-offset` or a contrasting color |
| Indicator only via `box-shadow` | Disappears in forced colors mode | Add a transparent outline |
| Ring clipped by `overflow: hidden` | Partly hidden ring | Use an inset offset or adjust the parent |
| Clickable `<div>` | Not reachable by keyboard | Use `<button>` or `<a href>` |
| Focus hidden under a sticky header | User loses their place | Set `scroll-padding-top` |
Exercise
A designer asks you to remove the focus ring on buttons because it appears after mouse clicks and looks untidy. Which CSS keeps the ring for keyboard users, and why is it safer than button:focus { outline: none; }?
Show answer
Use button:focus-visible { outline: 3px solid #1d4ed8; outline-offset: 3px; } and, if you want to hide the mouse ring, add button:focus:not(:focus-visible) { outline: none; }. The browser shows the ring only when it decides the user needs one, such as during keyboard navigation. button:focus { outline: none; } removes the ring for every kind of focus, so keyboard users can no longer see where they are.
Exercise
Your button uses box-shadow: 0 0 0 4px rgba(29, 78, 216, 0.35) as its only focus style. A user on Windows High Contrast mode reports they cannot see focus. What is the cause, and what do you add?
Show answer
Forced colors mode removes box shadows, so nothing is left to draw. Add an outline, for example outline: 3px solid transparent; outline-offset: 3px;. It is invisible in normal mode, and the browser recolors it with a system color in forced colors mode.
Exercise
You tab through a page and focus seems to disappear on one link near the top. Name two likely causes and how to check each.
Show answer
One cause is a CSS reset that sets outline: none on links with no replacement. Check the computed styles in DevTools with :focus-visible forced on. The other is that the focused link is hidden behind a sticky header or clipped by a parent with overflow: hidden. Check whether the element scrolls under the header, and fix it with scroll-padding-top or by changing the parent's overflow or the outline offset.