Sub topic 15
Contrast and accessible focus indicators
This chapter assumes you know the shade scale, the bg-*, text-*, and border-* utilities, and the hover: and focus: variants from earlier chapters. You should also be comfortable reading a hex color.
Why contrast needs checking
Tailwind's palette makes it easy to pick colors that look fine to you but fail for people with low vision or a glare on their screen. The Web Content Accessibility Guidelines (WCAG) set minimum contrast ratios. A ratio compares the brightness of two colors, from 1:1 (identical) to 21:1 (black on white).
| Element | Minimum ratio |
|---|---|
| Normal body text | 4.5:1 |
| Large text (about 24px, or 19px bold) | 3:1 |
| Input borders, icons, focus indicators | 3:1 against the adjacent color |
Reading the shade scale
Shade numbers give a rough guide. On a white background, shades 600 and above usually pass for text. Shade 400 and below usually fails.
| Classes on white | Approximate ratio | Result |
|---|---|---|
| `text-slate-600` | 7.6:1 | Passes for body text |
| `text-slate-500` | 4.8:1 | Passes, with little margin |
| `text-slate-400` | 2.6:1 | Fails for text |
| `border-slate-300` | 1.5:1 | Fails for an input border |
The last row catches many people. A border-slate-300 input looks tidy, but its edge is too faint to meet 3:1. Use border-slate-500 for form controls and keep light borders for decorative dividers.
Colored backgrounds need the same check. White text on bg-indigo-600 passes. White text on bg-indigo-400 does not. Paste your two hex values into a contrast checker such as the one in browser DevTools instead of trusting your eyes.
Keep the focus indicator visible
Keyboard users depend on the focus indicator to see where they are. If you remove the default outline, you must replace it.
<button
class="rounded-md bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-500 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-indigo-600 focus-visible:ring-offset-2"
>
Save changes
</button>Three choices make this accessible:
focus-visible:instead offocus:. The ring appears for keyboard focus but not for most mouse clicks, so it does not distract pointer users.- A ring color that contrasts with its surroundings.
ring-indigo-600on a white page passes the 3:1 rule for non-text elements. ring-offset-2. This adds a gap between the element and the ring, so the ring stays visible even when the element's background is close to the ring color. In Tailwind v4 the offset utility still works, and the offset color defaults to the page background.
Do not rely on color alone
A red border on an invalid input is not enough for people who cannot tell red from green. Pair the color with a second signal, such as an icon, a text message, or a changed border width.
<input
aria-invalid="true"
class="rounded-md border-2 border-red-600 px-3 py-2 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-red-600"
/>
<p class="mt-1 text-sm text-red-700">Enter a valid email address.</p>The thicker border and the message carry the meaning even when the red is not perceived.
Common errors
- Placeholder text styled with
placeholder-slate-300or lighter, which falls below 4.5:1. - Opacity modifiers such as
text-black/40, which lower contrast and are easy to forget when computing ratios. - Dark mode variants that fix the light theme but leave a dark theme with faint text. Check both.
Exercise
A text input uses border border-slate-300 focus:outline-none and nothing else for focus. Name two accessibility problems and write corrected classes.
Show answer
The border is about 1.5:1 against white, below the 3:1 needed for a control's edge, and removing the outline leaves no focus indicator. Use border border-slate-500 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-indigo-600.