Custom focus styles with :focus-visible in CSS
41 Never remove a focus ring, replace it
easyStyle focus on a link and a button so a keyboard user always sees where they are, a mouse user is not given a ring they did not ask for, and no browser is left with nothing.
Stuck? Three hints, smallest first
- 1.
:focus-visiblematches when the browser thinks a visible indicator is warranted — keyboard, not mouse click. - 2. If you must clear the default, clear it inside
:focus:not(:focus-visible)so the ring survives everywhere else. - 3.
outline-offsetgives the ring room; a negative value draws it inside a clipping parent.
<p><a class="link" href="#somewhere">A link, tab to me</a></p>
<button class="btn" type="button">A button, tab to me</button>
<p class="note">Click them with a mouse: no ring. Tab to them: a ring.</p>
.link { color: #3055d8; }
/* The keyboard case gets a clear ring. */
.link:focus-visible,
.btn:focus-visible {
outline: 3px solid #3055d8;
outline-offset: 2px;
border-radius: 3px;
}
/* The mouse case, and only the mouse case, loses it. Note this is scoped to
:focus:not(:focus-visible) rather than a blanket outline: none. */
.link:focus:not(:focus-visible),
.btn:focus:not(:focus-visible) {
outline-color: transparent;
}
.btn {
margin-bottom: 10px;
padding: 9px 16px;
border: 1px solid #3055d8;
border-radius: 8px;
background: #ffffff;
color: #17171a;
font: inherit;
cursor: pointer;
}
.btn:hover { background: #eef1fd; }
.note { margin: 0; font-size: 0.8rem; color: #5c5c66; }
8 checks that prove this answer
Assertions, run in the browser. The markup and styles above are handed to them
as html and css, so press Run and each check prints
ok. The build fails if a single one fails.
// The comment in the stylesheet names the thing being avoided, so strip the
// comments before asserting about real declarations. Otherwise the check fails
// on the very example that is explaining it.
const rules = css.replace(/\/\*[\s\S]*?\*\//g, '');
test('both controls get a focus-visible ring', /\.link:focus-visible,\s*\.btn:focus-visible/.test(rules), true);
test('the ring is thick enough to see, and offset', /outline:\s*3px solid/.test(rules) && /outline-offset:\s*2px/.test(rules), true);
test('outline: none appears in no rule', /outline:\s*none/.test(rules), false);
test('nor outline: 0', /outline:\s*0[;\s]/.test(rules), false);
test('the mouse case is scoped, not blanket', /:focus:not\(:focus-visible\)/.test(rules), true);
test('and it clears only the colour, leaving the outline in place', /outline-color:\s*transparent/.test(rules), true);
test('the phrase to avoid is still shown in a comment, for recognition', /outline: none/.test(css), true);
test('a real button and a real link, both natively focusable', hasTag(html, 'button') && hasTag(html, 'a'), true);
Output
outline: none is the single most damaging line in front-end CSS: it makes a page unusable by keyboard while looking completely fine, so it survives review. The replacement pattern here is worth adopting wholesale — give :focus-visible a ring you actually like, and if the click ring bothers you, clear it under :focus:not(:focus-visible) so the keyboard case is untouched. Clearing outline-color rather than outline is a small extra safeguard: the outline is still there, so Windows high contrast mode can still draw it.