Sub topic 9
Rings and outlines for focus styling
This chapter assumes you know the basic color and border utilities from earlier chapters, and that you have used hover: and focus: variants. You also need a working Tailwind project. The examples work in v3 and v4 unless noted.
Why focus styles matter
Keyboard users move through a page with the Tab key. The focus indicator shows them which element is active. Browsers draw a default outline, but it often clashes with a design. Tailwind lets you replace it. Always replace it with something visible, and never just remove it.
Rings
A ring is a solid band drawn around an element with box-shadow. It takes up no layout space, so adding one on focus does not move other content.
ring-2sets the ring width. The scale includesring-0,ring-1,ring-2,ring-4, andring-8.ring-indigo-500sets the color, using the same palette and shade numbers as other color utilities.ring-indigo-500/50applies the opacity modifier to the ring color.ring-offset-2adds a gap between the element and the ring.ring-offset-whitecolors that gap so it matches the page background.ring-insetdraws the ring inside the element's border box.
Plain ring has no fixed size across versions. It is 3px in v3 and 1px in v4. Write an explicit width such as ring-2 so the result does not change when you upgrade.
Outlines
An outline is a native CSS property drawn outside the border. Tailwind exposes it with outline-2, outline-indigo-500, outline-offset-2, and outline-dashed. Outlines follow the element's rounded corners in current browsers.
A focus style for form fields
This input swaps the default outline for a two-pixel indigo ring and a matching border.
<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"
/>focus:outline-none removes the browser's outline. focus:ring-2 and focus:ring-indigo-500 add the replacement.
focus versus focus-visible
focus: applies whenever an element has focus, including after a mouse click. focus-visible: applies only when the browser decides the user needs a visible indicator, which usually means keyboard navigation. Buttons and links usually look better with focus-visible:. Text inputs usually keep focus:, because every user needs to see where they are typing.
<button class="rounded-md bg-indigo-600 px-4 py-2 text-white focus:outline-none focus-visible:ring-2 focus-visible:ring-indigo-500 focus-visible:ring-offset-2">
Save changes
</button>Common errors
- Removing the outline with no ring or outline to replace it.
- Using a ring color with too little contrast against the background. The indicator should reach at least a 3:1 ratio against adjacent colors.
- Adding
ring-offset-2on a dark background without setting the offset color, which leaves a white gap. - Relying on the default ring width and getting a different result after a version upgrade.
Exercise
A link should show a 2px indigo ring with a 2px gap only for keyboard users, and should show nothing after a mouse click. Which classes do you add?
Show answer
Use focus:outline-none focus-visible:ring-2 focus-visible:ring-indigo-500 focus-visible:ring-offset-2. The focus-visible: prefix limits the ring to keyboard focus. In v4, replace focus:outline-none with focus:outline-hidden.