Accessible focus ring with :focus-visible
Show a clear focus outline for keyboard users without drawing it on mouse clicks.
- CSS
- GreenSnippets
- MIT
:where(a, button, input, select, textarea, summary, [tabindex]):focus-visible {
outline: 3px solid #2563eb;
outline-offset: 2px;
border-radius: 4px;
}
:focus:not(:focus-visible) {
outline: none;
}Example / expected output
/* Tab through the page: every interactive element shows a blue ring. */How it works
Never remove outlines without replacing them. :focus-visible matches when the browser decides the user needs a focus indicator (typically keyboard navigation). The outline uses outline-offset so it does not overlap the element, and a contrasting color so it passes WCAG 2.2 focus appearance guidance.
This snippet has been reviewed but was not executed by an automated test. Test it in your own environment before relying on it.
Related snippets
- CSS
Respect prefers-reduced-motion
Turn off or shorten animations for users who ask their system for less motion.
- HTML
Accessible modal with the dialog element
Use the native dialog element for a modal with built-in focus trapping, Escape to close and a backdrop.
- HTML
Accordion with details and summary
A keyboard-accessible FAQ accordion with no JavaScript, including an exclusive group using the name attribute.
- CSS
Accessible button styles with states
A base button with hover, active, disabled and focus styles that meets minimum target size.

