Skip to content
AITroveRead. Build. Understand.
Make this comfortable

CSS focus-visible: keep keyboard location unmistakable

Last updated: 5 Oct 20265 min read
tutorial
BeginnerBy AITrove Editorial

The :focus-visible pseudo-class matches focus when the browser decides a visible indicator is appropriate. It helps distinguish keyboard-oriented focus styling from every pointer click.

How the rule works

A reviewer tabs through receipt links and action buttons. Each needs an obvious outline that remains visible against the page and does not move the layout. Outline offset separates the indicator from a dark button edge. Do not rely on a shadow alone, since forced colors may remove shadows. A scroll container or overflow-hidden wrapper can clip the indicator even when the rule is correct. Test the first and last focusable controls, keyboard navigation into dialogs, and return focus after closure. CSS does not make a div keyboard-operable; use native interactive elements first.

css
.receipt-action:focus-visible,
.receipt-link:focus-visible { outline: 3px solid #205c8a; outline-offset: 3px; }
@media (forced-colors: active) {
  .receipt-action:focus-visible,
  .receipt-link:focus-visible { outline-color: Highlight; }
}

Cost and verification

The outline is inexpensive to paint and does not affect box size. The failure cost is high when focus disappears inside a dense form. Test with Tab and Shift+Tab, not only a mouse click. If a component library removes outlines globally, restore them at the shared control layer rather than patching one route at a time. Keep the control's label visible as focus moves.

Common Mistakes

  • Do not write outline: none without an equally visible replacement.
  • Do not assume :hover covers keyboard use.
  • Do not style a nonfocusable element and expect it to enter the tab order.

Connected lessons

Extend this decision

Property decisions

Further form and type decisions

Further interaction details

Further native-state decisions

css
interaction
web
Storage details