liveBook · Manning

:focus-visible

As explained in Architecting Accessibility

A CSS pseudo-class that matches an element when the browser determines that a visible focus indicator is appropriate.

If you navigate with a keyboard, you need to see which control has focus. Removing a browser’s default outline without replacing it makes links and other controls harder to use. The CSS :focus-visible pseudo-class lets a browser show a custom focus indicator when it deems one useful—for instance, after keyboard navigation—rather than styling every focused state with :focus. section 5.3.1 section 5.4

*:focus-visible {
  /* Inner focus indicator */
  outline: 3px solid #F9F9F9;
  outline-offset: 0;
  /* Outer focus indicator */
  box-shadow: 0 0 0 5px #193146;
}

This rule gives focused elements a light, 3px inner outline and a dark outer shadow. The two colors help the indicator stand out on both light and dark backgrounds, including image links. section 5.4

TIP: Test the whole page

Navigate with Tab and Shift-Tab. Can you always tell where focus is, including on image links and at different viewport sizes and zoom levels? A custom indicator should be consistently obvious, not merely present. Check its contrast against adjacent colors and make borders or outlines thick enough to notice. section 5.3.1 section 5.4

How Architecting Accessibility covers it

How deep it goes
Shows a working CSS rule and testing guidance, but does not explain the browser’s decision process.
Examples
Uses a two-color CSS focus rule and Tab-and-Shift-Tab testing across light, dark, and image backgrounds.
What it assumes
Assumes familiarity with CSS rules and keyboard navigation; does not teach CSS selectors from zero.
What you can do after
Operate/configure it in CSS to display a conspicuous focus indicator when the browser considers one useful.
How it connects
Connects keyboard navigation and link accessibility with CSS focus selectors, contrast, outlines, and box shadows.

Also explained in

Compare how 2 books explain :focus-visible →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Architecting Accessibility. The section links point to the parts of the book it draws on.