CSS :focus-visible: Making the Keyboard Focus Ring Easy to See
People who use a keyboard look at the focus ring to see which element is currently selected. If you add outline and outline-offset to :focus-visible, a thick, clear ring appears only when the element is selected with the Tab key, and not when a button is pressed with the mouse. If the component also sits on a dark background, a two-tone ring (a dark color plus white) stays visible on both light and dark backgrounds.
The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened HTML and CSS pages in Playwright, moved focus by sending Tab key presses and mouse clicks from Playwright, and recorded getComputedStyle() values and screenshot pixels. Forced colors mode was reproduced with Playwright's emulateMedia({ forcedColors: 'active' }). This is not a real Windows high-contrast setting. I did not test Firefox, Safari, or real smartphones.

Adding an outline with :focus-visible
The ring is drawn with the following CSS: a 3px line, offset 2px outside the button's edge.
.btn:focus-visible {
outline: 3px solid #067a98;
outline-offset: 2px;
}
In the test environment, I opened the page and pressed Tab to move through the buttons. On every button, both :focus and :focus-visible matched, and the ring appeared. When I clicked with the mouse, focus moved to the button, so :focus matched, but :focus-visible did not, and no ring appeared. The last scene in the GIF above shows this state.
Compared with a button that had the same ring on :focus, the only difference was on click.
| Selector | Selected with Tab key | Mouse click |
|---|---|---|
Ring on .btn-focus:focus |
Ring shown | Ring shown |
Ring on .btn:focus-visible |
Ring shown | Ring not shown |
Ring on :focus-visible in a text input |
Ring shown | Ring shown |
Only the text input matched :focus-visible even when clicked. The MDN page for :focus-visible also says that focus is generally not shown after a button is clicked, but is shown for fields where you type text. Whether :focus-visible matches is decided by the browser. CSS can only control how the focus ring looks when it matches.
Even a button with no styling showed Chromium's default ring when selected with the Tab key. Its value was outline: auto 1px rgb(16, 16, 16), a thin ring. The CSS above replaces this default ring with a thicker ring in a clearly visible color.
According to MDN's browser compatibility data (@mdn/browser-compat-data 8.1.8), :focus-visible is supported from Chrome and Edge 86, Firefox 85, and Safari 15.4.
Drawing the ring with outline instead of border
If you show a ring by making border thicker on focus, the button grows and nearby elements move. In the test environment, a button whose border was normally 1px and became 3px on focus grew from 89×46px to 93×50px when selected with the Tab key, and the button to its right shifted 4px to the right. A button whose ring was drawn with outline stayed at 91×46px, and the button to its right did not move.
The CSS specification section defining outline (CSS UI 4) also says that outline takes up no space (it does not affect surrounding layout). The same section says outlines follow the curve of border-radius, and in the test environment, the ring on a button with rounded corners also followed the rounded corners.
outline-offset is the property that sets the gap between an element's edge and its outline. In this example, a 2px gap separates the button's gray edge from the ring. The WCAG explanation of focus appearance also notes that placing the ring slightly away from the component can make it easier to see.
Using a two-tone ring to stay visible on dark backgrounds
A single-color ring can become hard to see depending on the background color. I compared a dark cyan ring (#067a98) on a white background and on a dark background (#1f2a30).
WCAG 2.2 Success Criterion 2.4.13 (Focus Appearance, Level AAA) requires two things of the focus indicator. First, it must have an area at least as large as a 2px line drawn around the unfocused component. Second, the pixels that change must show a contrast ratio of at least 3:1 between focused and unfocused states.
So I took the color of the same pixel just outside the left edge of the button (at its vertical center) in screenshots before and after focus, and calculated the contrast using the WCAG formulas for relative luminance and contrast ratio.
| Ring | Contrast ratio on white background | Contrast ratio on dark background |
|---|---|---|
One color (#067a98) |
4.95 | 2.96 |
Two colors (#1f2a30 and white) |
14.66 | 14.66 |
The one-color ring measured 2.96 on the dark background, below 3:1. The two-tone ring is written like this:
.btn-2tone:focus-visible {
outline: 3px solid #1f2a30;
outline-offset: 0;
box-shadow: 0 0 0 6px #fff;
}
A dark 3px outline is drawn on the inside, and a white box-shadow is drawn outside it. The box-shadow extends 6px from the button's edge. In Chromium in the test environment, the outline was drawn over the inner 3px. Counting outward from the button's edge, 1–3px were dark and 4–6px were white. On a white background, the dark line is visible. On a dark background, the white line is visible. Each is 3px thick.

When drawing with box-shadow, keep a transparent outline
Forced colors mode replaces the page's colors with colors the user has chosen, like Windows high-contrast settings. With outline: none and a ring drawn only with box-shadow, the ring disappeared in forced colors mode.
The CSS specification section on forced colors says that in forced colors mode, box-shadow becomes none, and colors such as outline-color are replaced with system colors. So, even when drawing with box-shadow, keep a transparent outline.
.btn-shadow-safe:focus-visible {
outline: 3px solid transparent;
box-shadow: 0 0 0 3px #067a98;
}
In normal display, the transparent outline is invisible and only the box-shadow ring shows. When I reproduced forced colors mode and selected a button with the Tab key, box-shadow became none. For the button with outline: none, zero pixels changed color within 6px around the button, so its appearance did not change on focus. For the button that kept a transparent outline, the outline color changed from rgba(0, 0, 0, 0) (transparent) to the system color chosen by Chromium, and the ring was visible.

The two-tone ring above also draws its inner line with outline. When I reproduced forced colors mode and selected it with the Tab key, the white box-shadow became none, and the outline line remained in a system color.
Inside an overflow: hidden box, draw the ring inward
If you draw an outer ring on a link that spans the full width of an overflow: hidden box, the part that extends beyond the box gets clipped. I drew a ring with outline-offset: 2px on the first link of a menu with rounded corners and counted pixels. The ring color appeared 0 times on the top, left, and right sides. Only the bottom side remained.
As described on the MDN page for outline-offset, a negative value for outline-offset draws the ring inside the element.
.menu {
overflow: hidden;
}
.menu a:focus-visible {
outline: 3px solid #067a98;
outline-offset: -3px;
}
With outline-offset: -3px, the 3px ring fits exactly inside the link, and the ring color appeared on all four sides: top, bottom, left, and right.






