CSS :focus / :focus-visible
- type
- pseudo-classes
- applies to
- links, buttons, inputs, anything focusable
- inherits
- n/a
- common values
- :focus, :focus-visible, :focus-within
Where it goes
Next to your :hover rules, on buttons, links and inputs. Replace the default ring with your own, like outline: 2px solid #6366f1; outline-offset: 2px. Never just remove it.
Example
button:focus-visible,
a:focus-visible {
outline: 2px solid #6366f1;
outline-offset: 2px;
}
.field:focus-within { border-color: #6366f1; }Common mistakes
- Writing outline: none to get rid of the "ugly ring." That makes the site unusable by keyboard. Restyle the ring. Never delete it.
- Styling only :hover. A keyboard user never hovers. Every :hover state needs a :focus-visible twin, often the same rule: a:hover, a:focus-visible { ... }.
- Using :focus for a fancy ring and getting it on every mouse click. That is what :focus-visible is for.