CSS Reference ยท Pseudo-classes
The :focus-visible pseudo-class
Reference for the CSS :focus-visible pseudo-class: show focus rings when the browser decides they help, with syntax, examples, usage notes and browser support.
The :focus-visible pseudo-class matches a focused element only when the browser thinks a focus indicator is useful. The usual rule of thumb: it matches when you reach an element with the keyboard, and for text fields where typing is expected, but not when you click a button with the mouse.
That lets you keep a strong focus ring for keyboard users without showing it on every mouse click, which is the main reason designers used to remove outlines.
:focus-visible { outline: 3px solid #2563eb; outline-offset: 2px; }
Try it
Syntax
:focus-visible { /* declarations */ }
No arguments; specificity (0,1,0). An element that matches :focus-visible also matches :focus, never the other way around.
Syntax and arguments
| Part | Description |
|---|---|
:focus-visible |
Matches a focused element when the browser's heuristics say the focus should be shown. |
| Typical matches | Keyboard focus on any element; focus in text inputs and textareas, even after a click. |
| Typical non-matches | A button, link or checkbox focused by a mouse or touch click. |
| Scripted focus | After element.focus(), it follows the input method that was used just before. |
Usage notes
- Use it instead of removing outlines. A common pattern is to leave default focus styles alone, or to style
:focus-visibleand not touch:focusat all. - Don't hide the default ring on
:focus. If you setoutline: noneon:focus, make sure:focus-visiblebrings a visible indicator back. - The decision is the browser's. Heuristics differ slightly between browsers, and users can change the behaviour in settings. Never use
:focus-visibleto detect "keyboard user". - Fallback is rarely needed. Every current browser supports it. If you must support a very old one, put a
:focusrule first and cancel it with:focus:not(:focus-visible). - Contrast and offset. Use an
outlinewithoutline-offsetso the ring does not sit on the element's own border and stays visible on any background.
Browser support
Supported in all current browsers and Baseline widely available.
Related
:focusโ any focus:focus-withinโ a container of the focused element:activeโ pressed state- Lessons: Outline, Accessibility