Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 38 ยท Keyboard and Focus

Visible Focus Styles

Learn why keyboard focus must be visible, how to style it with :focus-visible, how to meet contrast and size guidelines, and what to do on dark backgrounds.

When a keyboard user tabs through a page, the only thing telling them where they are is the focus indicator, usually an outline around the current element. If it is invisible, they are navigating blind. WCAG 2.4.7 (Focus Visible, Level AA) requires that any keyboard-operable interface has a visible focus indicator, and WCAG 2.2 adds rules about how clear it must be.

The single most common cause of failure is a CSS reset that removes it: *:focus { outline: none; }. This lesson shows how to style focus well instead of hiding it.

Default focus rings

Browsers provide a default focus ring, and it is usually decent. The problem starts when designers find it ugly and remove it. If you want a different look, replace the outline, never delete it. If you do nothing, the default ring is already compliant in most cases.

:focus versus :focus-visible

Historically :focus styled every focused element, including buttons clicked by mouse, which led designers to remove outlines. The modern pseudo-class :focus-visible matches when the browser decides the focus indicator should be shown: always for keyboard focus, and for mouse clicks only on elements that accept text input. That gives you the best of both worlds.

CSS
/* Remove nothing globally. Add a stronger ring for keyboard users. */
:focus-visible {
  outline: 3px solid #1a4bd6;
  outline-offset: 2px;
}

Because the browser decides when it matches, mouse users do not see a ring on a clicked button while keyboard users always do. Support is universal in current browsers.

Good focus styles

Visible At least 2px thick
Contrast 3 to 1 against neighbours
Offset A gap so it does not blend in
Consistent Same style everywhere

Replace, never remove.

1 / 2

Try it

Compare focus styles

Contrast and size (WCAG 2.2)

The focus indicator should have at least 3:1 contrast with the colours next to it (the element's own state change and the background). WCAG 2.2's AAA criterion 2.4.13 describes the size: at least a 2 CSS pixel thick perimeter. Following it is wise even at AA.

A tricky situation is a page with both light and dark areas, where one outline colour may disappear on one of them. A two-tone ring fixes it: an inner light line and an outer dark line, made with outline plus box-shadow, so one of the two always contrasts.

Focus on dark and light backgrounds

The yellow-and-black pair stays visible on both panels.

Windows forced colours mode

When a user turns on Windows High Contrast (forced colours), the browser overrides your colours. Outlines are kept, but box-shadow is removed. If you build the ring from box-shadow alone, it vanishes. Always keep a real outline (it can be transparent in normal mode and is made visible by forced colours):

CSS
:focus-visible {
  outline: 3px solid transparent;  /* shown in forced-colours mode */
  box-shadow: 0 0 0 3px #1a4bd6;
}
  • :focus-within styles a container when any child has focus, for example highlighting a form row.
  • outline-offset keeps the ring from touching the element edge.
  • scroll-margin and scroll-padding keep focused elements from being covered by sticky headers (WCAG 2.2, 2.4.11).
  • Custom controls (a styled checkbox) must still show focus on the visible element, not only on a hidden input.
  • Programmatic focus on headings or containers (tabindex="-1") usually does not need a ring if it is not interactive. Use :focus-visible rules so it does not flash on click.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which CSS rule is a serious accessibility problem?

    Removing the outline for all focused elements leaves keyboard users without any indicator.
  2. 2. What is the advantage of :focus-visible over :focus?

    The browser decides when a ring is helpful, so keyboard users always see it and mouse users rarely do.
  3. 3. What contrast ratio should a focus indicator have against adjacent colours?

    Non-text elements, including focus indicators, should have at least 3:1 contrast.
  4. 4. Why keep a real outline instead of box-shadow only?

    In Windows High Contrast the shadow disappears, but outlines remain visible.

Summary

  • Visible focus is required by WCAG 2.4.7; never remove the outline without a replacement.
  • Use :focus-visible for a clear ring that shows for keyboard users.
  • Aim for at least 2px thickness and 3:1 contrast, and use a two-tone ring on mixed backgrounds.
  • Keep a real outline so forced colours mode still shows focus.

Next: Skip Links and Bypassing Blocks