Sub topic 2
Text, background, and border colors
This chapter assumes you can write basic HTML and CSS, know the box model, and have a working Tailwind CSS project or playground. It also assumes you know what a utility class is and can read hex color notation such as #6366f1.
The color utility pattern
Every color utility follows one pattern: a property prefix, a color name, and a shade number.
| Prefix | CSS property it sets |
|---|---|
| `text-` | `color` |
| `bg-` | `background-color` |
| `border-` | `border-color` |
So bg-indigo-600 sets the background to shade 600 of indigo. Shades run from 50 (lightest) to 950 (darkest). A few colors, such as white, black, and transparent, have no shade number.
<button class="rounded-md bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-500">
Save changes
</button>The button uses a dark indigo background with white text. On hover, hover:bg-indigo-500 swaps in a lighter shade. The hover: prefix is a variant. It applies the utility only while the pointer is over the element.
Borders and color
Border color has a common trap. A border-slate-300 class sets only the color. It does not create a border. You also need a width, such as border for 1px or border-2 for 2px.
<div class="rounded-lg border-2 border-dashed border-slate-300 border-b-emerald-500 p-4">
Dashed border with a green bottom edge
</div>Here border-2 sets the width, border-dashed sets the style, and border-slate-300 sets the color on all four sides. Then border-b-emerald-500 overrides the color of the bottom side only. Per-side classes use t, r, b, l after border-.
Common errors
- Colored border that does not show. You set
border-red-500but no width. Addborder. - Class name built from variables. Tailwind finds classes by scanning your source for complete strings. A string like `
bg-${color}-500is never seen as a full class, so no CSS is generated. Write whole names such asbg-red-500`, or map values to full class names. - Wrong shade pairing. Light text on a light background is hard to read. Pair a low shade with a high shade, such as
bg-slate-900withtext-slate-100.
Reading shades
Use the shade number as a rough lightness scale. Shades 50 to 200 suit backgrounds and subtle fills. Shades 500 to 600 suit buttons and accents. Shades 700 to 950 suit body text and dark surfaces. This is a convention, not a rule, but it keeps designs consistent.
Exercise
A div has the classes border-red-500 p-4, but no red border appears. What is missing, and what would you add to make a 2px red border on the bottom side only?
Show answer
A border width is missing, because border-red-500 sets only the color. Use border-b-2 border-b-red-500. The first class sets the bottom width to 2px, and the second sets the bottom color.
Exercise
A developer writes <div class="bg-${tone}-100"> in a template, with tone set to green. The background never appears. Why?
Show answer
Tailwind scans source files for complete class names. bg-${tone}-100 is never a complete string, so the CSS for bg-green-100 is not generated. Write the full class name, for example by choosing between bg-green-100 and bg-red-100 in code.