How do :focus-visible and :focus style keyboard focus differently?
Quick answer
:focus matches an element whenever it has focus. :focus-visible matches a focused element when the browser determines that a visible focus indicator is appropriate. It is a more selective styling hook, not a different kind of focus: an element can have focus without matching :focus-visible. Keep a clear visible focus indication so users can locate the active control. [S1] [S2]
The question, the details, the next step.
A practical checklist
Identify standard and custom controls that can receive focus.
Check that focused controls have a visible indicator; do not remove the only one.
Navigate through the controls by keyboard and confirm the active control is easy to locate.
Check pointer interactions and review any styles that reset outlines.
Repeat the checks on custom controls and components with their own styles.
A few more useful answers
Does an element lose focus when it does not match `:focus-visible`?
No. It can still have focus; :focus-visible is more selective about when visible-focus styling applies. [S1]
Can I remove the browser’s default outline?
Do not remove the only visible focus indication. If you change the styling, keep a visible indicator for focused controls. [S2]
Does the CSS example prove the style works in every browser?
No. It is a styling example, not a browser compatibility test. Check the behavior on the browsers and components relevant to your page.
Sources
References used for this guide.
- [S1] MDN: CSS pseudo-classes, including :focus and :focus-visible developer.mozilla.org · Checked
- [S2] MDN: Keyboard accessibility and visible focus styling developer.mozilla.org · Checked
