Sub topic 2
Text, Background, and Border Colors
This chapter covers the three color utility families that you use most often in Tailwind CSS: text-*, bg-*, and border-*. It assumes you know basic HTML and CSS, including the box model. It also assumes you have a working Tailwind project or playground, and that you know hex and RGB notation. The previous chapter introduced the default palette and its shade scale. This chapter shows how to apply those colors to elements.
The shared naming pattern
All three families follow one pattern:
{property}-{color}-{shade}- The property prefix chooses what gets colored:
textfor the font color,bgfor the background,borderfor the border. - The color name is a palette name such as
slate,indigo,emerald, orred. - The shade is a number from 50 (lightest) to 950 (darkest).
So text-slate-700 sets the text color, bg-slate-100 sets the background, and border-slate-300 sets the border color. Once you know the pattern for one family, you know it for all of them.
Some colors have no shade. black, white, transparent, and current are single values, so you write bg-white, text-black, border-transparent, and text-current. The value current means currentColor, which is the element's computed text color. It is useful for borders and icons that should follow the text.
Text color
text-{color} sets the CSS color property. Because color is inherited, setting it on a parent affects all descendants that do not set their own.
<article class="text-slate-800">
<h1 class="text-2xl font-bold text-slate-900">Release notes</h1>
<p>This paragraph inherits slate-800 from the article.</p>
<p class="text-slate-500">This one is lighter and quieter.</p>
</article>A common technique is to build a hierarchy from shades of one color. Headings use a dark shade, body text a slightly lighter one, and secondary text a lighter one still.
Note that text- is also used for font size (text-lg) and alignment (text-center). Tailwind tells them apart by the value. text-lg is a size because lg is on the size scale. text-slate-500 is a color because slate-500 is on the color scale.
Background color
bg-{color} sets background-color. A solid button needs only a background and a contrasting text color. The hover state comes from the hover: variant, which swaps in a different shade.
<button class="rounded-md bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-500">
Save changes
</button>Here the background is indigo-600 by default. On hover it becomes indigo-500, which is lighter. The text stays white in both states.
Be careful with the bg- prefix, because it also covers other background properties. bg-cover, bg-center, and bg-no-repeat are not colors. They control images. Only values that name a color, such as bg-red-500, set background-color.
Border color
border-{color} sets the border color. It does not draw a border by itself. A border needs a width and a style, and that is the most common reason a colored border does not appear.
<!-- No visible border: only a color is set -->
<div class="border-red-500 p-4">Nothing is drawn</div>
<!-- Visible border: width plus color -->
<div class="border border-red-500 p-4">A red 1px border</div>The class border sets border-width: 1px. Tailwind's base styles (called Preflight) also set the border style to solid on all elements, so you do not need a separate style class in most projects. Without a width, the default is zero, and nothing shows.
Border width and style get a full treatment in a later chapter. For now, remember that a color class works together with a width class.
The default border color changed between versions
If you write border with no color class, the color depends on your Tailwind version.
| Version | Color of `border` with no color class |
|---|---|
| v3 | `gray-200` (set in Preflight) |
| v4 | `currentColor` (the element's text color) |
If you upgrade from v3 to v4, borders without an explicit color can suddenly become dark. The fix is to name the color on each element, for example border border-slate-200. Naming it is also better practice in v3, because it makes the intent clear.
How the pieces combine
The three families are independent. You can mix any text, background, and border color, and each sets one CSS property.
<div class="rounded-lg border border-amber-300 bg-amber-50 p-4 text-amber-900">
Heads up: your trial ends in three days.
</div>This is a standard pattern for alert boxes. All three colors come from the same hue. The background is a very light shade (50), the border is a light-to-medium shade (300), and the text is a very dark shade (900). Using one hue and picking shades from opposite ends of the scale gives a consistent look and enough contrast between text and background.
The following diagram shows how the same pattern maps onto a few color roles.
This is a convention, not a rule. Solid components such as buttons invert it. They use a dark or saturated background (500 to 700) and white text.
Related color utilities
Several other utilities accept the same color values and follow the same naming pattern. They are covered in later chapters, but it helps to recognize them now.
ring-{color}colors a focus ring.outline-{color}colors an outline.divide-{color}colors the lines between sibling elements.shadow-{color}colors a box shadow.fill-{color}andstroke-{color}color SVG shapes.accent-{color},caret-{color}, anddecoration-{color}color form controls, the text cursor, and text underlines.
Because they share the palette, a color you choose for a brand or a status can be reused across all of them.
Per-side border colors
You can color one side of a border separately from the others. The prefixes are border-t-, border-r-, border-b-, border-l-, and the logical border-x- and border-y-.
<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>The general border-slate-300 colors all four sides. The more specific border-b-emerald-500 then overrides the bottom side only. Tailwind orders its generated CSS so that side-specific classes win over the general one, regardless of the order in your class attribute.
Common errors
- A border does not appear. You set
border-{color}but no width. Addborderorborder-2. - A color class has no effect. The shade does not exist (for example
bg-blue-550), or the name is misspelled. Tailwind generates CSS only for valid names, so an invalid class produces nothing and gives no error message. - A class works in one place but not in production. Tailwind scans your source files for complete class names. A name built from pieces, such as `
bg-${color}-500, is never found, so the CSS is not generated. Write the full name, such asbg-red-500`, in your source, or map values to complete strings. - Text is hard to read. The text and background shades are too close together. Contrast is covered in a later chapter, but a quick rule is to put a 50 or 100 shade behind a 800 or 900 shade, or the reverse.
- Borders turned dark after an upgrade. This is the v3 to v4 default change described above.
The dynamic class problem is worth a concrete fix:
// Wrong: Tailwind cannot see the complete class name
const bad = (color) => `bg-${color}-500`;
// Right: every class name appears in full
const badgeColors = {
success: "bg-emerald-500",
warning: "bg-amber-500",
danger: "bg-red-500",
};
const good = (kind) => badgeColors[kind];Summary of the key points
text-,bg-, andborder-share the pattern{property}-{color}-{shade}.black,white,transparent, andcurrenthave no shade.- A border color needs a border width to be visible.
- The default border color differs between v3 and v4, so set it explicitly.
- Side-specific classes such as
border-b-emerald-500override the general color. - Class names must appear in full in your source files.
Exercise
A developer writes <div class="border-indigo-500 p-4">Card</div> and sees no border. Then they write <div class="border border-indigo-500 p-4" > and a border appears. Explain why, and name one more thing they should do if they plan to upgrade from Tailwind v3 to v4 for a <div class="border p-4"> that has no color class.
Show answer
The first element sets only a border color. The border width defaults to zero, so nothing is drawn. The border class sets a 1px width, and Preflight supplies the solid style, so the second element shows an indigo border. For the upgrade, the border class with no color defaults to gray-200 in v3 but to currentColor in v4. The developer should add an explicit color, such as border-slate-200, so the border looks the same in both versions.
Exercise
Write the classes for an info box that has a very light blue background, a medium-light blue border, and dark blue text, all from the sky hue. Then say what is wrong with ` text-${tone}-700 ` in a JavaScript template string.
Show answer
One valid answer is rounded-lg border border-sky-300 bg-sky-50 p-4 text-sky-900. The template string builds the class name at runtime. Tailwind scans source files for complete class names, so it never sees text-red-700 or similar and does not generate the CSS. Use a lookup object whose values are full class names instead.