Selector Beginner

Focus styles without annoying mouse users

:focus shows an outline on every click, which looks wrong for mouse users. :focus-visible shows it only when the browser expects keyboard focus.

3 lines
1button:focus-visible {
2  outline: 2px solid var(--focus-color);
3}
Old 2 lines
1button:focus {
2  outline: 2px solid blue;
3}
// Outline appears on mouse click. Often removed with outline: none.
Widely available Since 2022 95% global usage

This feature is well established and works across many devices and browser versions. It has been available across browsers since 2022.

86+
85+
15.4+
86+
click a button, then tab to the next one
Click shows no ring. Tab shows the ring.
Kinsta

Your first month is free

Managed WordPress hosting for faster sites.

Learn more
⚑

Keyboard only

Outline shows when focus comes from Tab, not from a mouse click. Matches user intent.

✦

Accessible by default

You keep visible focus for keyboard users. No need to remove outline and hurt a11y.

∞

Browser decides

Browsers use heuristics (keyboard vs pointer). One selector, correct behavior.

Lines Saved
Same
Better behavior
Old Approach
:focus everywhere
Outline on mouse click
Modern Approach
:focus-visible
Outline when keyboard focusing

How it works

With :focus, the outline appears whenever the element gets focus, including after a mouse click. That looks odd and many sites removed it with outline: none, which hurts keyboard users.

:focus-visible only matches when the browser would normally show a focus ring, e.g. after Tab. Mouse users don't see it, keyboard users do. You get clear focus styles without the old tradeoff.

New CSS drops.

Join 600+ readers who've survived clearfix hacks.

ESC