:focus-visible for Keyboard Focus
Lesson 24 of 41 in the CSS3 learning path.
How do I style keyboard focus without a ring on every mouse click?
:focus matches any focused element, including a button that was clicked with a mouse. :focus-visible matches when the browser decides a focus indicator is needed, which includes keyboard focus and usually includes a text field, because the caret has to be visible. The practical pattern draws a ring for :focus, removes it for :focus:not(:focus-visible), and draws it again for :focus-visible.
Browsers that do not understand :focus-visible drop the newer selectors and keep the plain :focus ring, so focus is never invisible. Do not set outline to none on :focus by itself. That removes the keyboard ring in every browser, including the ones that would have shown :focus-visible.
Syntax & Parameters
:focus:not(:focus-visible) { outline: none; } :focus-visible { outline: 3px solid #0f766e; }
Example
Interactive preview available:focus {
outline: 3px solid #0f766e;
outline-offset: 3px;
}
:focus:not(:focus-visible) {
outline: none;
}
:focus-visible {
outline: 3px solid #0f766e;
outline-offset: 3px;
}
.toolbar {
display: flex;
flex-direction: row;
gap: 0.5rem;
padding: 0.5rem;
}
.toolbar a,
.toolbar button {
padding: 0.45rem 0.75rem;
border-radius: 8px;
background: #ecfdf5;
color: #064e3b;
text-decoration: none;
}
.toolbar button {
border: 1px solid #99f6e4;
font: inherit;
}
input[type="search"] {
padding: 0.45rem 0.6rem;
border: 1px solid #cbd5e1;
border-radius: 8px;
}
input[type="search"]:focus-visible {
outline: 3px solid #b45309;
outline-offset: 2px;
border-color: #b45309;
}
Expected Output
Tabbing to the link or the button shows a teal ring offset from the control. Clicking the button with a mouse does not leave that ring in browsers that support :focus-visible. Clicking the search field still shows an amber ring.
Line-by-Line Walkthrough
- The :focus rule draws a teal outline for any focused element, which still helps browsers that do not understand :focus-visible.
- :focus:not(:focus-visible) removes that outline when the browser focused the element but does not want a ring, which covers most mouse clicks on buttons.
- :focus-visible puts the same teal ring back for keyboard focus and for other cases where the browser shows a focus indicator.
- The toolbar lays the link and the button in a row with a small gap.
- Shared padding, radius, and colors make the link and the button look like one control set.
- The button sets font: inherit and a real border so it does not fall back to the tiny default system button.
- The search field has its own border. Its :focus-visible rule uses an amber outline so the field ring is distinct from the toolbar.
- outline-offset moves the ring outside the control so the background and border do not cover it.
- Text fields often match :focus-visible on click as well as on Tab, because the caret needs a visible insertion point.
Common Pitfalls & Security
A missing focus ring fails keyboard users even when the mouse UI looks cleaner. The ring is the indicator. Color inside the control is not a substitute unless the contrast is just as clear.
- outline: none on :focus, with no :focus-visible replacement, removes the keyboard indicator in every browser.
- A custom ring that is teal on a teal button fails the contrast people need against the adjacent pixels. Offset it onto the page background.
- Replacing the outline with a border change can shift layout. Outline does not take up space. A border does.
- :focus-visible on a button is not the same as :focus-visible on a text field. Expect the field to show a ring after a click.
- Applying the mouse exception to every element, including inputs, hides the caret indicator if you are not careful with the search-field rule.
- An author outline does not appear for a control that is not focusable. A div that acts as a button still needs a real button or tabindex and a key handler.
Best Practices & Modern Standards
Keep the :focus fallback, the :focus:not(:focus-visible) exception, and the :focus-visible ring together. Use a 3px outline with an offset, in a color that contrasts with the page behind the control. Give text fields their own ring if the generic teal is not distinct on that surface.
Test with the keyboard: tab to the link, the button, and the field. Then click the button and confirm the ring does not stick. Do not remove the outline until the replacement is already in the same stylesheet.
Real-World Use
The lesson toolbar has a Notes link and a Save button. Mouse users click Save and do not see a ring stuck on the button. Keyboard users tab through the toolbar and get a teal ring on the focused control. The search field keeps an amber ring even after a click, so the caret position stays obvious.
Frequently Asked Questions
Why not set outline none on focus alone?
That deletes the keyboard ring in every browser. Remove the ring only for :focus:not(:focus-visible), and paint :focus-visible yourself.
Do mouse clicks on buttons show the ring?
In browsers that support :focus-visible, an ordinary click on a button focuses it without matching :focus-visible, so the exception rule hides the ring.
Do text fields show the ring on click?
Yes. The browser usually matches :focus-visible for a click in a text field because the caret is the focus indicator.
What if the browser lacks focus-visible?
The :focus-visible rules are ignored and the plain :focus outline remains. Keyboard and mouse focus both keep the ring, which is the safe fallback.