Sub topic 5
Colors and backgrounds
This chapter assumes you know basic HTML and CSS, can run npm commands, and have a Tailwind project set up as covered in the earlier chapters. You will learn how Tailwind's color utilities are named, how the default palette works, and how to avoid the most common mistakes.
The naming pattern
Every color utility follows the same shape: a property prefix, a color name, and a shade number.
bg-indigo-600
text-slate-900
border-red-500The prefix chooses what the color applies to. The color name and shade come from the default palette. Shades run from 50 (very light) to 950 (very dark). The same pattern works for many properties.
| Utility prefix | What it colors |
|---|---|
| `bg-` | Background |
| `text-` | Text |
| `border-` | Border |
| `ring-` | Focus ring or outline ring |
| `fill-` and `stroke-` | SVG shapes |
| `divide-` | Lines between child elements |
Because the pattern is consistent, you learn it once and reuse it everywhere. Keeping to the default scale also keeps a design consistent. If every primary button uses indigo-600, you never have to ask which blue you meant.
Building a card with color
The card below uses a white background, dark text for the heading, softer text for the body, and a brand-colored button.
<div class="max-w-sm rounded-xl bg-white p-6 shadow-md">
<h2 class="text-xl font-semibold text-slate-900">Card title</h2>
<p class="mt-2 text-slate-600">Some supporting text for the card.</p>
<button class="mt-4 rounded-lg bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-700 focus:ring-2 focus:ring-indigo-500">
Read more
</button>
</div>Notice the pattern in the text colors. The heading uses slate-900 and the body uses slate-600. Using two shades of one gray family creates hierarchy without introducing a new color. The button uses indigo-600 at rest and indigo-700 on hover, so the hover state is a one-step darker shade of the same color.
Opacity modifiers
You can set a color's opacity by adding a slash and a number from 0 to 100.
<div class="bg-indigo-600/50 text-slate-900/80">
Semi-transparent background and slightly faded text.
</div>This is useful for overlays and subtle tints. It affects only that color. The opacity-50 utility is different, because it fades the whole element, including its text and children.
Backgrounds beyond solid colors
Solid colors are the most common case, but gradients work with a direction utility plus color stops.
<div class="bg-gradient-to-r from-indigo-500 to-purple-500 p-8 text-white">
A left-to-right gradient.
</div>In Tailwind v4 the direction utilities are named bg-linear-to-r and similar, and the older bg-gradient-to-r name remains as an alias. Check the version your project uses. You can also add a middle stop with via-pink-500.
Adding your own colors
When the default palette is not enough, define a color in the theme. In Tailwind v4, a CSS variable in the --color-* namespace inside @theme creates matching utilities.
@import "tailwindcss";
@theme {
--color-brand: #4f46e5;
}You can now write bg-brand, text-brand, border-brand, and so on. A later chapter covers theme customization in depth.
Common errors
- Low contrast. Light text on a light background, such as
text-slate-400onbg-white, is hard to read. Aim for a contrast ratio of at least 4.5:1 for body text. - Invented shades.
bg-indigo-650does not exist, so Tailwind generates nothing and the element shows no background. Use a shade from the scale, or an arbitrary value likebg-[#4a41e0]. - Built class names. Writing `
bg-${color}-600` in JavaScript hides the full class name from Tailwind's scanner, so the CSS is never generated. A later chapter covers this. - Mixing too many hues. Pick one neutral family such as slate, one brand color, and perhaps one or two status colors.
Exercise
A button should have a blue-600 background, white text, and a background that becomes one shade darker when hovered. Which classes do you write? Also, why does ` bg-${tone}-500 ` fail to produce styles?
Show answer
Write bg-blue-600 text-white hover:bg-blue-700. The template string fails because Tailwind scans source files as plain text and looks for complete class names. It never sees bg-red-500 or bg-green-500 written out, so it does not generate the CSS. Use a lookup object that contains each full class name instead.