Sub topic 15
Contrast and accessible focus indicators
This chapter assumes you can already apply color, border, ring, and shadow utilities, and that you know how hover:, focus:, and dark: variants work. Here you will check whether the styles you write can be read and seen by everyone, including keyboard users and people with low vision.
Which contrast numbers matter
Contrast ratio compares the relative luminance of two colors. It runs from 1:1 (identical) to 21:1 (black on white). The WCAG 2.x guidelines set these minimums at level AA:
| What you are styling | Minimum ratio | WCAG criterion |
|---|---|---|
| Body text | 4.5:1 | 1.4.3 |
| Large text (18pt, or 14pt bold) | 3:1 | 1.4.3 |
| Borders of inputs, icons, and other UI parts needed to identify a control | 3:1 | 1.4.11 |
| Focus indicator against its surroundings | 3:1 | 1.4.11 |
Each ratio is measured against the color next to it. For text, that is the background. For an input border, that is the page behind the input. For a focus ring, that is both the page and the control.
Decorative borders, such as a divider between list items, have no minimum. If a border is the only thing that shows where a text field is, it must reach 3:1.
Reading the shade scale
The Tailwind palette is a lightness scale, so shade numbers give a rough guide to contrast on a white page. These values are approximate. Verify the real ratio for any pair you ship.
| Class | Approximate ratio on white | Use |
|---|---|---|
| `slate-300` | 1.5:1 | Decorative dividers only |
| `slate-400` | 2.6:1 | Fails for text and for input borders |
| `slate-500` | 4.8:1 | Passes for body text and borders |
| `slate-600` | 7.6:1 | Comfortable body text |
| `indigo-600` | 6.3:1 | Good for white text on a button |
Two rules of thumb follow. On a white background, text should usually be shade 600 or darker. On a white background, a border that identifies a control should be shade 500 or darker.
The border-slate-300 input in the earlier examples looks neat, but it fails the 3:1 requirement for a control boundary. Switch to border-slate-500, or keep the light border and give the field a background that sets it apart from the page.
Hover states can fail too
Check every state of a component, not only the resting one. The button from the start of this tutorial uses bg-indigo-600 with white text. On hover it becomes hover:bg-indigo-500. White on indigo-500 is about 4.5:1, right at the AA limit. Many calculators report it as slightly below. Lightening a background on hover can push white text under the minimum. Darkening is safer:
<button class="rounded-md bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-700">
Save changes
</button>Opacity makes contrast depend on what is behind
The slash modifier produces a color that changes with its backdrop. Take bg-black/50 text-white. Over a dark photo, the panel is nearly black and the text is clear. Over a white page, the panel becomes mid-gray (about #808080) and white text on it is only about 4:1. You can't tell the ratio from the class names alone. Test the worst-case backdrop, or use a solid color when the content behind the panel is unknown.
Focus indicators
Keyboard users need to see which element has focus. Browsers draw a default outline for this. Removing it without a replacement makes a page unusable for people who can't use a mouse.
Use focus-visible for buttons and links
focus: applies whenever an element has focus, including after a mouse click. focus-visible: applies only when the browser decides the indicator is useful, which in practice means keyboard focus. For buttons and links, focus-visible: avoids a ring appearing on every click.
<a
href="/settings"
class="rounded-md bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-700 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
>
Settings
</a>The pieces are:
outlinesets the outline style to solid.outline-2sets a two-pixel width.outline-offset-2leaves a gap between the element and the outline. The outline then contrasts with the page rather than with the button's own fill.outline-indigo-600sets the color.
The outline class is needed in Tailwind v3, where outline-2 sets only the width. It is harmless in v4.
Text inputs: focus is correct
Text fields show a text cursor on any focus, mouse or keyboard, and browsers treat them that way. Use focus: here. The ring approach from earlier in the tutorial works well:
<input
type="text"
class="rounded-md border border-slate-500 px-3 py-2 focus:border-indigo-600 focus:outline-none focus:ring-2 focus:ring-indigo-600"
/>Here the resting border is slate-500 (about 4.8:1) and the focus ring is indigo-600 (about 6.3:1), so both pass the 3:1 minimum.
Outlines versus rings
Outlines and rings look alike but behave differently.
| Property | Outline utilities | Ring utilities |
|---|---|---|
| Implemented with | `outline` CSS property | `box-shadow` |
| Follows `rounded-*` corners | Yes, in current browsers | Yes |
| Shown in forced-colors mode | Yes | No, shadows are removed |
| Takes up layout space | No | No |
Forced-colors mode (Windows High Contrast) replaces your colors with a small system palette and removes box shadows. A ring-only focus style can vanish for those users. Prefer a real outline. If you use a ring, keep a transparent outline underneath it. Windows draws a transparent outline in a visible system color.
Ring widths also changed. In v3, ring is 3px. In v4, ring is 1px, and ring-3 gives the old width. Setting an explicit ring-2 avoids the difference.
Making the indicator visible on any background
A ring that works on white may disappear on a dark card or a colored banner. Three habits help:
- Set a color for each context with variants, for example
focus-visible:outline-indigo-600 dark:focus-visible:outline-indigo-400. - Use
outline-offset-2so the indicator sits on the page background instead of touching the control. - Use a two-tone indicator when the background varies, such as a light ring with a dark offset ring in contrast to the page.
Dark mode needs its own checks. text-slate-100 on bg-slate-800 is strong, but text-slate-500 on bg-slate-800 is only around 3:1, which fails for body text. Muted text that passes on white often fails on dark backgrounds. Choose lighter shades, such as dark:text-slate-300, and test again.
A review process
To run the check in practice:
- Open the component in a browser and press Tab. Confirm each interactive element shows a clear indicator.
- Open DevTools and use the color picker, which shows the contrast ratio and a pass/fail mark for text. For borders and rings, copy the hex values into a contrast checker.
- Toggle dark mode and repeat.
- Turn on forced-colors emulation in the DevTools rendering panel and repeat.
Automated tools such as axe or Lighthouse catch text contrast failures well. They miss many focus and border problems, so keep the manual Tab pass.
Common mistakes
- Using
focus:outline-nonewith no replacement. The control has no visible focus at all. - Using
placeholder:text-slate-400as the only label. Placeholder text still needs 4.5:1, and it disappears when typing. - Showing errors by color alone, such as a red border. Add an icon or message text, because some users can't distinguish the colors.
- Using
disabled:opacity-50and assuming it is exempt. Disabled controls are exempt from the minimums, but low opacity can still make them hard to identify. - Trusting shade numbers without measuring. Palette shades are a guide, and opacity, gradients, and images change the result.
Exercise
An input uses border border-slate-300 focus:outline-none focus:ring-2 focus:ring-indigo-300 on a white page. List the contrast and focus problems and give a corrected class list that works in Tailwind v4.
Show answer
First, border-slate-300 is about 1.5:1 against white, below the 3:1 minimum for a control boundary. Second, ring-indigo-300 is a pale color, also well below 3:1 against white, so the focus indicator is hard to see. Third, in v4 focus:outline-none removes the outline entirely, and a box-shadow ring is dropped in forced-colors mode, so those users get no focus indicator. A corrected list is border border-slate-500 focus:outline-hidden focus:ring-2 focus:ring-indigo-600. The border is about 4.8:1, the ring about 6.3:1, and outline-hidden keeps a transparent outline that forced-colors mode renders visibly.
Exercise
A hero banner uses bg-black/40 text-white over a photo that is sometimes bright white. Explain why a contrast check on one photo is not enough, and name one change that makes the text readable on any photo.
Show answer
With a 40% black layer, the final background color depends on the photo underneath. Over a dark photo the text has high contrast. Over a white photo the background becomes light gray and white text falls well below 4.5:1. Checking one image proves nothing about the others. A change that fixes it is using a more opaque layer, such as bg-black/70, after testing against a white image, or placing the text on a solid panel like bg-slate-900 inside the banner.