Sub topic 9
Rings and Outlines for Focus Styling
This chapter covers how Tailwind draws focus indicators with ring and outline utilities. It assumes you know basic HTML and CSS, including the box model. It also assumes you have used Tailwind utility classes and variants such as hover: and focus:, and that you have a working Tailwind project or playground.
Why focus styles need care
A focus indicator shows which element will receive keyboard input. Keyboard and screen magnifier users depend on it to know where they are on a page. Browsers draw a default outline on focused elements, but designers often remove it because it clashes with a design. Removing it without a replacement makes a page unusable for keyboard users.
Tailwind gives you two tools for a replacement:
outline-*utilities set the CSSoutlineproperty.ring-*utilities draw a ring with abox-shadow, so they follow the element's rounded corners and can be tinted and layered.
Outlines
The CSS outline property draws a line around an element, outside its border edge. An outline does not take up layout space, so adding one never shifts neighboring elements.
| Class | Effect |
|---|---|
| `outline` | 1px solid outline |
| `outline-2`, `outline-4` | Sets the outline width |
| `outline-offset-2` | Moves the outline 2px away from the element |
| `outline-indigo-500` | Sets the outline color from the palette |
| `outline-dashed` | Sets the outline style |
| `outline-none` | Removes the outline (see the warning below) |
A common pattern is to keep the browser's focus behavior but restyle it:
<a
href="/pricing"
class="rounded-md px-3 py-2 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
>
Pricing
</a>This uses focus-visible: rather than focus:. The two differ in when they apply:
focus:applies whenever the element has focus, including after a mouse click.focus-visible:applies only when the browser decides a focus indicator is helpful. For buttons and links that usually means keyboard focus. For text inputs, browsers show it for mouse focus too, because typing needs a visible caret location.
In Tailwind v3, outline-none sets outline: 2px solid transparent; outline-offset: 2px. The transparent outline stays visible in Windows forced colors (high contrast) mode, which keeps a fallback indicator. In v4, outline-none sets outline-style: none, and the new outline-hidden utility keeps the old transparent-outline behavior. If you rely on that fallback, use outline-hidden in v4.
Rings
A ring is a box shadow with no blur and a spread, drawn outside the element. Because it is a shadow, it follows rounded-* corners, which a plain outline did not do reliably in older browsers.
<button class="rounded-lg bg-indigo-600 px-4 py-2 text-white ring-2 ring-indigo-300">
Ringed button
</button>The width and color are separate utilities, as with borders:
ring-1,ring-2,ring-4,ring-8set the width in pixels.ring-indigo-500sets the color.ring-indigo-500/50sets the color at 50% opacity, using the slash modifier from the opacity chapter.ring-offset-2adds a gap between the element and the ring, filled with the offset color.ring-offset-whitesets the color of that gap.ring-insetdraws the ring inside the element instead of outside.
The default width differs by version. In v3, a bare ring is 3px and its default color is a translucent blue. In v4, a bare ring is 1px and the default color is currentColor. When you upgrade a project, set widths and colors explicitly so the appearance does not change.
How the ring offset works
An offset is not a real gap. Tailwind draws a second shadow in the offset color underneath the ring, which covers the space between the element and the ring. That means the gap is painted with ring-offset-white (or whichever color you choose), not transparent. On a dark background, a white offset looks like a white halo.
This is the main gotcha with offsets. The fix is to match the offset color to the surface behind the element:
<div class="bg-slate-900 p-6">
<button
class="rounded-md bg-indigo-500 px-4 py-2 text-white focus:outline-none focus-visible:ring-2 focus-visible:ring-indigo-400 focus-visible:ring-offset-2 focus-visible:ring-offset-slate-900"
>
Dark surface button
</button>
</div>In v4, ring offsets still work through ring-offset-*. If you want a true transparent gap that does not depend on the background, use an outline with outline-offset-* instead.
Replacing the default focus style on an input
The topic's input example removes the default outline and adds a ring and border change:
<input
type="text"
class="rounded-md border border-slate-300 px-3 py-2 focus:border-indigo-500 focus:outline-none focus:ring-2 focus:ring-indigo-500"
/>Three things happen on focus:
focus:outline-noneremoves the browser outline.focus:ring-2draws a 2px ring.focus:ring-indigo-500colors the ring, andfocus:border-indigo-500colors the border to match.
The ring is not a replacement for the border. The border is part of the layout and stays in place. The ring is painted on top, outside the border, so the input does not change size when focused.
Ring or outline: choosing between them
| Concern | Ring | Outline |
|---|---|---|
| Implementation | `box-shadow` | `outline` |
| Follows rounded corners | Yes | Yes in current browsers |
| Offset gap | Painted in a color | Truly transparent |
| Forced colors mode | Shadows are removed | Outlines are kept |
| Can stack with a drop shadow | Shares the shadow property, but Tailwind composes them | Independent |
| Multiple layers | Possible with a ring plus an offset | One line only |
The forced colors row matters. Windows high contrast mode strips box shadows, so a ring may vanish completely. An outline survives. A robust approach is to use a ring for the visual design and keep a transparent outline as a fallback:
<button
class="rounded-md bg-indigo-600 px-4 py-2 text-white outline outline-2 outline-transparent focus-visible:ring-2 focus-visible:ring-indigo-300 focus-visible:ring-offset-2"
>
Accessible button
</button>In forced colors mode the browser replaces the transparent outline color with a system color, so users still see an indicator.
Combining with group and peer states
Focus styling often belongs on a parent when a child has focus. focus-within: applies when any descendant has focus:
<label class="flex items-center gap-2 rounded-lg border border-slate-300 px-3 py-2 focus-within:border-indigo-500 focus-within:ring-2 focus-within:ring-indigo-500/40">
<span class="text-slate-500">Search</span>
<input type="text" class="flex-1 bg-transparent outline-none" />
</label>The input has no ring of its own. The wrapper shows the ring when the input is focused, so the whole field looks like one control. Here outline-none on the input is acceptable only because the wrapper provides the visible indicator.
Common errors
- Removing the outline with no replacement. Always check by pressing Tab through the page.
- Using
focus:for buttons and links when you only want keyboard focus. Switch tofocus-visible:to avoid a ring after each mouse click. - A ring that is too faint.
ring-indigo-500/20on a white page may fall below the contrast needed to see it. A focus indicator should have at least a 3:1 contrast ratio against the adjacent colors (WCAG 2.2 criterion 1.4.11 for non-text contrast). - Ring offset color mismatch. A white offset on a dark card looks wrong. Set
ring-offset-*to the surface color. - Clipped rings. A ring is painted outside the element. If a parent has
overflow-hidden, the ring is cut off. Usering-insetor add padding to the parent. - Forgetting that the ring and a shadow share a mechanism. In Tailwind,
ring-*andshadow-*compose through CSS variables, so using both on the same element works. Overridingbox-shadowwith an arbitrary value, such asshadow-[...], goes through the same system. Writing your own raw CSSbox-shadowcan wipe out the ring.
Summary of the key utilities
- Use
outline-*for a layout-neutral line that survives forced colors mode. - Use
ring-*for a shadow-based indicator that supports opacity and layering. - Prefer
focus-visible:for buttons and links, andfocus:orfocus-within:for text fields. - Never remove the outline without adding another visible indicator.
- Check the indicator against your background, including in dark mode.
Exercise
A button sits on a bg-slate-900 panel. You apply focus:outline-none focus:ring-2 focus:ring-indigo-400 focus:ring-offset-2, and a white halo appears around the button when it is focused. Why does this happen, and what change fixes it? Also name one change that keeps an indicator visible in Windows forced colors mode.
Show answer
The ring offset is painted as a solid color, and the default offset color is white, so the gap between the button and the ring shows white on the dark panel. Add focus:ring-offset-slate-900 so the gap matches the panel. For forced colors mode, box shadows are removed, so add a transparent outline (for example outline outline-2 outline-transparent) that the browser recolors with a system color, or use outline-* utilities for the focus style instead of a ring.
Exercise
Why is focus-visible: usually a better choice than focus: for a button, and when would you still use focus: or focus-within:?
Show answer
focus-visible: applies only when the browser judges a focus indicator useful, which for buttons is mainly keyboard focus, so mouse clicks do not leave a ring behind. Use focus: for text inputs, where a visible indicator is useful for any focus, and focus-within: when a wrapper element should show the indicator because a child field has focus.