How to Create CSS Outlines and Focus Styles
Outline vs border (measured), when :focus-visible actually matches, what WCAG requires of focus indicators, and why box-shadow focus rings vanish in forced colors mode while a transparent outline survives.
Outline vs Border
A border is part of the box; an outline is drawn on top of it and takes no space. In Chromium a 100 px wide div measured 108 px with a 4 px border and stayed 100 px with a 4 px outline, so adding or removing an outline never shifts the layout. The flip side is that a thick outline can overlap neighboring content; outline-offset moves it out (or, with a negative value, in).
Outlines also follow border-radius now. MDN's compatibility data lists the change in Chrome and Edge 94, Firefox 88 and Safari 16.4; before those versions outlines were always rectangular, which is why older advice says to fake rounded focus rings with box-shadow. That workaround is no longer needed.
When :focus-visible Matches
:focus matches whenever an element has focus. :focus-visible matches only when the browser decides a focus indicator is needed, which is not quite the same as "keyboard only". We checked in Chromium 141:
| How the element got focus | :focus-visible |
|---|---|
| Tab key to a button | yes |
| Shift+Tab back to a button | yes |
| Mouse click on a button | no |
| Mouse click on a link | no |
| Mouse click in a text field | yes |
Text fields show the indicator even after a mouse click, because the user is about to type and needs to know where. So :focus-visible is the right selector for focus styles:
:focus-visible {
outline: 2px solid #00ffd1;
outline-offset: 2px;
}
The CSS Outline Generator previews widths, styles and offsets before you copy the CSS.
What WCAG Asks of a Focus Indicator
- 2.4.7 Focus Visible (level AA): keyboard focus must be visible. Removing the outline with nothing in its place fails this.
- 2.4.11 Focus Not Obscured, Minimum (AA, new in WCAG 2.2): the focused element must not be entirely hidden by author content such as a sticky header or cookie banner.
- 1.4.11 Non-text Contrast (AA): visual information needed to identify a component's state, focus included, needs a 3:1 contrast ratio against adjacent colors.
- 2.4.13 Focus Appearance (AAA): the indicator should be at least as large as a 2 CSS px perimeter around the component and change contrast by at least 3:1 between focused and unfocused states.
Contrast is where a single brand color often fails. The cyan ring above measures 14.97:1 against a dark background like ours but only 1.29:1 on white, well below 3:1; a dark teal such as #0b6e5f reaches 6.16:1 on white. On sites that mix light and dark sections, set the focus color per section, or pair a dark outline with a light ring so one of them always contrasts.
Forced Colors: Why box-shadow Rings Disappear
Windows contrast themes switch the browser into forced colors mode, which replaces author colors with the user's system colors and removes box-shadow entirely. We focused a button with three common recipes and checked the pixel just outside it, with forced colors off and emulated on:
| Focus style | Normal | Forced colors |
|---|---|---|
outline: 2px solid | visible | visible |
outline: none + box-shadow ring | visible | invisible |
transparent outline + box-shadow ring | visible | visible |
The shadow-only ring vanishes for exactly the people who chose high contrast. A transparent outline costs nothing in normal mode and becomes a visible system-colored ring in forced colors, so keep one whenever you style focus with box-shadow. Now that outlines follow border-radius, the simplest fix is to use the outline itself.
Never Remove Outlines Without a Replacement
outline: none or outline: 0 on focus, with nothing in its place, leaves keyboard users with no way to see where they are on the page. If the default ring clashes with your design, restyle it with :focus-visible rather than removing it, and tab through every page once with the mouse unplugged before you ship.
Frequently Asked Questions
What is the difference between outline and border?
How do I make accessible focus styles?
Why use :focus-visible instead of :focus?
Use the CSS Outline Generator โ free, no signup required.
โก Open CSS Outline Generator- [1]W3C, CSS Basic User Interface Level 4: outline properties โ
- [2]MDN browser-compat-data, css.properties.outline (follows border-radius), checked Oct 13, 2026 โ
- [3]W3C, Selectors Level 4: :focus-visible โ
- [4]W3C, Understanding WCAG 2.2 SC 2.4.7 Focus Visible โ
- [5]W3C, Understanding WCAG 2.2 SC 2.4.11 Focus Not Obscured (Minimum) โ
- [6]W3C, Understanding WCAG 2.2 SC 2.4.13 Focus Appearance โ
- [7]W3C, Understanding WCAG 2.2 SC 1.4.11 Non-text Contrast โ
- [8]W3C, CSS Color Adjustment Level 1: properties affected by forced colors mode โ