Sub topic 5
Colors and backgrounds
This chapter covers how Tailwind applies color to text, backgrounds, borders, and rings. It assumes you know the earlier chapters' basics: utility classes in markup, the build step that generates CSS, and the default spacing scale. It also assumes you know CSS color formats such as hex and rgb().
The color naming model
Every color utility follows the same pattern: a property prefix, a color name, and a shade number.
{property}-{color}-{shade}The default palette includes families such as slate, gray, red, amber, green, blue, indigo, and pink. Each family has shades from 50 (very light) to 950 (very dark). A lower number is lighter, and a higher number is darker. Two colors, black and white, have no shade, and transparent and current are keywords.
The property prefix decides what the color applies to:
| Prefix | CSS property | Example |
|---|---|---|
| `text-` | `color` | `text-slate-700` |
| `bg-` | `background-color` | `bg-indigo-600` |
| `border-` | `border-color` | `border-gray-300` |
| `ring-` | ring (focus outline) color | `ring-indigo-500` |
| `outline-` | `outline-color` | `outline-blue-500` |
| `divide-` | border color between children | `divide-slate-200` |
| `fill-` and `stroke-` | SVG paint | `fill-current` |
| `shadow-` | color of a shadow | `shadow-indigo-500/30` |
Some prefixes are ambiguous. text-lg sets font size and text-red-500 sets color. Tailwind tells them apart by the value that follows, so you never need to disambiguate manually. The same holds for border-2 (width) and border-red-500 (color).
Choosing shades
The default scale is built so that shades behave consistently across families. This lets you pick by role rather than by guessing.
- 50 to 100: subtle page or section backgrounds.
- 200 to 300: borders, dividers, and disabled fills.
- 500 to 600: primary actions and accents.
- 700 to 900: body text and hover states for filled buttons.
- 950: near-black text or very dark surfaces.
A reliable habit is to stay in one gray family for a whole interface. Mixing slate, gray, and zinc in the same page produces small tonal mismatches that are hard to name but easy to notice.
Text colors
Text color is inherited in CSS, so you can set it on a container and let children pick it up.
<article class="text-slate-700">
<h2 class="text-xl font-semibold text-slate-900">Release notes</h2>
<p>This paragraph inherits slate-700 from the article.</p>
<p class="text-sm text-slate-500">Secondary text uses a lighter shade.</p>
</article>The hierarchy here, dark heading, mid-tone body, light caption, is a common pattern. Using three shades of one family gives hierarchy without adding new hues.
currentColor
The current keyword maps to CSS currentColor, which is whatever the element's text color is. It is useful when a border, icon, or underline should follow the text.
<a href="/docs" class="border-b-2 border-current text-indigo-600 hover:text-indigo-800">
Read the docs
</a>The underline is always the same color as the link text, including on hover, because it is derived from color. SVG icons work the same way with fill-current or stroke-current.
Background colors
bg-{color}-{shade} sets the background color. It is the most common background utility, but backgrounds in CSS cover more than color. Tailwind exposes the other properties as separate utility groups.
| Goal | Utilities |
|---|---|
| Solid color | `bg-white`, `bg-slate-100` |
| Image | `bg-[url(...)]` (arbitrary value) |
| Size | `bg-cover`, `bg-contain` |
| Position | `bg-center`, `bg-top` |
| Repeat | `bg-no-repeat`, `bg-repeat-x` |
| Gradient | `bg-linear-to-r`, `from-*`, `via-*`, `to-*` |
Gradients
A gradient is built from a direction utility and color stops. In Tailwind v4 the direction utilities are named bg-linear-to-r, bg-linear-to-br, and so on. In v3 the same utilities were called bg-gradient-to-r. Check which version your project uses, because the old name is not guaranteed to work on a new setup.
<div class="bg-linear-to-r from-indigo-500 via-purple-500 to-pink-500 p-8 text-white">
Left-to-right gradient with three stops.
</div>from-* is the start color, via-* is an optional middle stop, and to-* is the end. If you omit to-*, the gradient ends in transparent.
Common mistake: text on gradients and images
When you place text on a photograph or a gradient, contrast changes across the element. Check the lightest and darkest regions behind the text. A common fix is a semi-transparent overlay between the image and the text, such as bg-black/40.
Opacity with the slash modifier
Tailwind lets you set the alpha channel of a color by adding a slash and a number from 0 to 100.
<div class="bg-indigo-600/20 text-indigo-900 border border-indigo-600/40 p-4">
A tinted panel: 20% background, 40% border.
</div>This is different from the opacity-* utility. The slash form changes only that one color. opacity-50 fades the entire element, including its text and children. For a translucent panel with fully readable text, use bg-white/50, not opacity-50.
You can also use a bracket value for an exact alpha, such as bg-black/[0.35], or a percentage-like custom value if you need something not on the scale.
| Class | What changes |
|---|---|
| `bg-black/50` | Only the background alpha |
| `opacity-50` | The whole element and all its children |
| `text-white/70` | Only the text alpha |
Color formats and the default palette
In Tailwind v4 the default palette is defined in the OKLCH color space, a perceptual format where equal steps in lightness look equally different to the eye. In practice this means the shades are more even than older hex-based palettes, and bright colors appear on wide-gamut displays. You do not need to write OKLCH yourself to use the defaults. You use the same class names as before.
If you need a color that is not in the palette, you have two options, covered in later chapters: add it as a theme token, or use an arbitrary value. As a preview, bg-[#1da1f2] produces a one-off background. Prefer a theme token for any color that appears more than once, because it gives the color a name and a single place to change it.
Putting color together: a status badge
The next example combines text, background, and border colors into a small component. Each variant uses a tinted background, a dark text shade, and a matching border, which is a pattern that stays readable and consistent.
<span class="inline-flex items-center rounded-full border border-green-600/30 bg-green-50 px-3 py-1 text-sm font-medium text-green-800">
Active
</span>
<span class="inline-flex items-center rounded-full border border-red-600/30 bg-red-50 px-3 py-1 text-sm font-medium text-red-800">
Failed
</span>Notice that the classes differ only in the color family name. When you later generate these from data, remember the rule from the dynamic classes chapter: write each full class name out literally, and do not build bg-${color}-50 from fragments.
const badgeClasses = {
success: 'border-green-600/30 bg-green-50 text-green-800',
error: 'border-red-600/30 bg-red-50 text-red-800',
};
const badge = (status) => badgeClasses[status];Accessibility and contrast
Color choices have to remain readable. The WCAG guidelines set minimum contrast ratios between text and its background:
- 4.5:1 for normal body text.
- 3:1 for large text (roughly 24px, or 19px bold) and for interface components such as input borders.
Shades 500 and lighter often fail on white as body text. As a rough guide on a white background, text-slate-500 is borderline, text-slate-600 and darker pass for body text, and text-gray-400 fails. Always verify the specific pair with a contrast checker, because the ratio depends on both colors. Also avoid using color as the only signal. A red border on an invalid field should come with an error message, since some users cannot distinguish red from green.
Common errors
- Writing a color class without a shade.
bg-indigodoes nothing in the default setup, becauseindigohas no unshaded form. Usebg-indigo-600. - Using
opacity-*when you meant a translucent background. The text fades too. Use the slash modifier. - Mixing gray families. Pick one of
slate,gray,zinc,neutral, orstone. - Expecting
text-red-500to change a border. Each property needs its own prefix:border-red-500. - Using v3 gradient names in v4. If
bg-gradient-to-rproduces nothing, check your version and usebg-linear-to-r. - Building color names from strings. Tailwind scans source text, so
text-${color}-600is never generated.
Summary of decisions
- Use the prefix to choose the property and the shade to choose lightness.
- Use
text-*on a container to inherit color, andcurrentto keep borders and icons in step with text. - Use the slash modifier for translucent colors and
opacity-*only for fading a whole element. - Keep one gray family, and define repeated custom colors as theme tokens.
- Check contrast for each text and background pair.
Exercise
A card has class="bg-white opacity-60 p-4" and its text looks washed out, though you only wanted a see-through white background over a photo. What is wrong, and what is the corrected class list? Also name one thing to check afterward.
Show answer
opacity-60 fades the whole element, including the text and any children. Replace it with the slash modifier on the background color: class="bg-white/60 p-4". Then check the text contrast against the photo behind the card, because a translucent background lets the image show through and can lower contrast. A darker text shade such as text-slate-900 helps.
Exercise
Write the classes for a "warning" badge in the same style as the green and red badges: tinted background, dark text, and a matching semi-transparent border, using the amber family. Then say why you would store the three variants in an object instead of writing border-${color}-600/30.
Show answer
Classes: border-amber-600/30 bg-amber-50 text-amber-800. Tailwind finds classes by scanning source text for complete class names. A template string like border-${color}-600/30 never contains a full class name, so the CSS is not generated. An object with literal strings, such as warning: 'border-amber-600/30 bg-amber-50 text-amber-800', keeps every class visible to the scanner.