CSSMar 19, 2026 ยท Updated Oct 13, 2026 ยท 4 min read

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.

๐Ÿ”ฒ
Try the CSS Outline Generator
Free, no signup
โ†’
DG
Derek Giordano
Designer & Developer
In this guide
01Outline vs Border02When :focus-visible Matches03What WCAG Asks of a Focus Indicator04Forced Colors: Why box-shadow Rings Disappear05Never Remove Outlines Without a Replacement

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 buttonyes
Shift+Tab back to a buttonyes
Mouse click on a buttonno
Mouse click on a linkno
Mouse click in a text fieldyes

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

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 styleNormalForced colors
outline: 2px solidvisiblevisible
outline: none + box-shadow ringvisibleinvisible
transparent outline + box-shadow ringvisiblevisible

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?+
A border takes space and an outline does not: a 100px-wide div measured 108px with a 4px border and stayed 100px with a 4px outline. Outlines follow border-radius in current browsers (Chrome and Edge 94, Firefox 88, Safari 16.4 and later) and are the standard way to show keyboard focus.
How do I make accessible focus styles?+
Style :focus-visible with an outline that has at least 3:1 contrast against the colors next to it (WCAG 1.4.11), keep the focused element from being hidden by sticky headers or banners (2.4.11), and never remove the outline without a visible replacement (2.4.7). For the stricter AAA criterion 2.4.13, make the indicator at least a 2 CSS px perimeter with a 3:1 change between focused and unfocused states.
Why use :focus-visible instead of :focus?+
Because it follows the browser's judgement of when an indicator is needed. In our Chromium test it matched after Tab and Shift+Tab and after a mouse click in a text field, but not after a mouse click on a button or link; :focus matched all of them.
Try it yourself

Use the CSS Outline Generator โ€” free, no signup required.

โšก Open CSS Outline Generator
DG
Derek Giordano
Written by the creator of Ultimate Design Tools. BA in Business Marketing.