Sub topic 3
Styling with core utilities
This chapter covers the utility classes you will use most: spacing, typography, color, and borders. It assumes you have a Vite and React project with Tailwind CSS v4 installed, as set up in the previous chapter. It also assumes you know the CSS box model and basic JSX. The goal is a working model of how the classes map to CSS and which mistakes to avoid.
How a utility maps to CSS
Most core utilities follow one pattern: a short prefix that names a CSS property, then a value from a scale. p-4 means padding with step 4 of the spacing scale. text-sm means a small font size. bg-white means a white background. Once you know the prefixes, you can read most class lists without looking anything up.
Each class sets one thing, so you combine several classes on one element. The order of classes inside className does not decide which wins. Order in the generated stylesheet does. Chapter 11 covers what that means when two classes conflict.
Spacing
Padding, margin, gap, width, and height share one numeric scale. In Tailwind v4, one step equals 0.25rem (4px at the default root font size). The spacing is computed from a single theme variable, so the scale is consistent across properties.
| Class | Value | Meaning |
|---|---|---|
| `p-1` | 0.25rem | Padding on all sides |
| `p-4` | 1rem | Padding on all sides |
| `px-6` | 1.5rem | Left and right padding |
| `py-2` | 0.5rem | Top and bottom padding |
| `mt-1` | 0.25rem | Margin on top only |
| `mx-auto` | auto | Centers a block with a set width |
| `-mt-2` | -0.5rem | Negative margin on top |
| `size-10` | 2.5rem | Width and height together |
The side letters are t, r, b, l, x (horizontal), y (vertical), and none (all sides). Prefixes p and m take the same letters.
Two habits make spacing easier to maintain:
- Use
gap-*on flex and grid containers, andspace-y-*orspace-x-*on simple stacks, instead of adding margins to every child. - Put margin on the parent's layout, not on the reusable component itself. A component with its own outer margin is hard to place in different contexts.
Sizing uses the same scale (w-64, h-10), plus fractions (w-1/2), keywords (w-full, min-h-screen), and container sizes (max-w-sm, max-w-2xl).
Typography
Text utilities split into several independent properties.
- Size:
text-xs,text-sm,text-base,text-lg,text-xl, up throughtext-9xl. Each size also sets a matching line height.text-smgives 0.875rem with a 1.25rem line height. - Weight:
font-normal,font-medium,font-semibold,font-bold. - Line height:
leading-tight,leading-normal,leading-relaxed, or a number such asleading-6. You can also attach it to the size with a slash, as intext-sm/6. - Letter spacing:
tracking-tight,tracking-wide. - Alignment and transform:
text-center,uppercase,capitalize. - Overflow:
truncate(one line with an ellipsis),line-clamp-3(three lines, then an ellipsis).
Size and line height are linked, so a later leading-* class overrides the line height that text-* set. That is intended. It lets you pick a size and then adjust only the spacing between lines.
Font family classes (font-sans, font-serif, font-mono) come from the theme. Chapter 12 shows how to add your own.
Color
Color classes combine a property prefix, a palette name, and a shade from 50 (lightest) to 950 (darkest): text-gray-900, bg-blue-600, border-red-300. The same palette works for text, backgrounds, borders, outlines, rings, and shadows.
You can set opacity with a slash modifier:
export function Overlay() {
return <div className="absolute inset-0 bg-black/50" />
}bg-black/50 is a black background at 50% opacity. It changes only the background color, unlike the older approach of fading the whole element. Any color utility accepts the modifier, such as text-white/80 or border-gray-900/10.
Pick a small set of roles and stick to them. A common pattern for a light interface:
text-gray-900for headings andtext-gray-600for secondary text.bg-whitefor surfaces andbg-gray-50for page backgrounds.border-gray-200for dividers.- One accent color, such as
blue-600, for actions.
Check contrast, too. text-gray-400 on white is often too faint to read for body text.
Borders, radius, and shadow
Border utilities are separate from color:
- Width:
border(1px),border-2,border-t,border-x-4. - Color:
border-gray-200. - Style:
border-dashed,border-none. - Radius:
rounded-md,rounded-lg,rounded-full, or one corner withrounded-t-lg. - Dividers between children:
divide-y divide-gray-200. - Shadow:
shadow-sm,shadow-md,shadow-lg.
Focus rings and outlines have their own utilities (ring-*, outline-*). They are used mostly with state variants, which the next chapters cover.
Putting it together
This component uses every group from this chapter. It is the ProfileCard from the chapter's examples:
export function ProfileCard({ name, role }: { name: string; role: string }) {
return (
<div className="max-w-sm rounded-lg border border-gray-200 bg-white p-6 shadow-sm">
<h2 className="text-lg font-semibold text-gray-900">{name}</h2>
<p className="mt-1 text-sm text-gray-600">{role}</p>
</div>
)
}Read it group by group:
max-w-smcaps the width at 24rem, so the card does not stretch across a wide screen.rounded-lg border border-gray-200gives a 1px light gray border with rounded corners.bg-white p-6 shadow-smsets a white surface, 1.5rem of inner padding, and a light shadow.text-lg font-semibold text-gray-900styles the name as a strong heading.mt-1 text-sm text-gray-600places the role close under the name in smaller, softer text.
The card has no CSS file and no class names to invent. Everything it looks like is visible in the markup.
Common errors
Typing a value that is not on the scale. p-5 exists, but p-5.5 and p-13 may not behave as you expect. In v4, many numeric values work because spacing is calculated from the base unit. Other scales, such as font sizes and shadows, only accept named steps. If a class has no effect, check that it is a real utility. You can inspect the element in your browser's dev tools and look for the rule.
Expecting text-* to set only color. text-lg sets size and line height, and text-gray-900 sets color. Both start with text-, but they control different properties. Tailwind tells them apart by the value.
Forgetting the border color (see the warning above).
Using margin for spacing inside a flex or grid container. It works, but gap-* keeps spacing in one place and avoids extra margin on the first or last child.
Putting mx-auto on an element with no width. A block element already fills its container, so auto margins have nothing to center. Pair mx-auto with max-w-* or w-*.
Relying on class order to resolve conflicts. Writing p-4 p-6 does not mean the last one wins. Remove the conflicting class instead, or use the merging helper from chapter 11.
Reading unfamiliar classes
When you meet a class you do not know, split it at the first hyphen. The part before it names the property. Then check the official documentation, which lists every utility with the CSS it generates. For example, tracking-wide is a letter-spacing utility, and line-clamp-3 limits text to three lines. This habit is faster than memorizing classes, and it is how most experienced Tailwind users work.
Exercise
A card should have 1.5rem of padding on all sides, a white background, a 1px border in light gray, rounded corners, and a title in bold 20px-ish dark text (use text-xl). Write the className for the card and the className for the title. Then explain why the card would look wrong if you wrote border without a color in Tailwind v4.
Show answer
Card: rounded-lg border border-gray-200 bg-white p-6. Title: text-xl font-bold text-gray-900. In Tailwind v4 the default border color is currentColor, so a bare border would draw the border in the element's text color, which is usually dark and heavy, instead of light gray. Adding border-gray-200 sets the color explicitly.
Exercise
What CSS do px-6 and py-2 produce, and which single class combination would give the same result as px-6 py-6?
Show answer
px-6 sets padding-left and padding-right to 1.5rem, and py-2 sets padding-top and padding-bottom to 0.5rem. px-6 py-6 is equivalent to p-6, which sets 1.5rem on all four sides.