focus-visible
A pseudo-class that matches only when the focus position needs to be shown, such as during keyboard navigation. Replaces the outline: none that removed the focus ring for keyboard users just to hide it on mouse clicks.
Modern approach
The browser decides whether the focus ring needs to be shown. Put your custom ring on :focus-visible, and it stays hidden on mouse clicks but appears during keyboard navigation.
button:focus-visible {
outline: 2px solid #06c;
outline-offset: 2px;
}Legacy approach
To hide the ring that appeared on mouse clicks, the :focus ring was removed altogether. Keyboard users can no longer see where the focus is.
button:focus {
outline: none;
}Things to watch when replacing
- Text inputs match
:focus-visibleeven when clicked with a mouse. If you also removed the ring on inputs withoutline: none, replacing it makes the ring appear on clicks too. - The browser’s default focus ring already follows the same rules as
:focus-visible. If you don’t need a custom ring, just removingoutline: noneis enough. - Give a custom ring a contrast of at least 3:1 against the background (WCAG 2.1 Success Criterion 1.4.11).
- In Safari, the Tab key doesn’t move to buttons or links unless “Press Tab to highlight each item on a webpage” is turned on in the Advanced settings. Turn it on when you check button focus rings in Safari.
Fallback
All five target browsers support it, so :focus-visible alone is normally enough. If you also have to support older browsers, put the ring on :focus and remove it with :focus:not(:focus-visible). Browsers that don’t know :focus-visible ignore the whole second rule, so they fall back to showing the ring.
button:focus {
outline: 2px solid #06c;
outline-offset: 2px;
}
button:focus:not(:focus-visible) {
outline: none;
}Browser Support
Chrome
v86+2020-10-20Edge
v86+2020-10-09Safari
v15.4+2022-03-14Safari (iOS)
v15.4+2022-03-14Firefox
v85+2021-01-26Fallback
from v4:-moz-focusring