Utility-First Basics
In this lesson you'll learn how Tailwind class names are put together. Once you understand the pattern, you can guess most class names without looking them up.
Anatomy of a class name
Most utilities follow the shape property-value, optionally preceded by variants and followed by a modifier:
md:hover:bg-sky-500/75
│ │ │ │ └─ modifier: 75% opacity
│ │ │ └───────── value: the colour sky-500
│ │ └──────────── utility: background-color
│ └────────────────── variant: only on hover
└───────────────────── variant: only at the md breakpoint and wider| Part | Examples | Meaning |
|---|---|---|
| Utility | p, mt, text, bg, w |
which CSS property |
| Value | 4, lg, red-500, 1/2, full |
which value from the design scale |
| Negative | -mt-2, -rotate-6 |
prefix a - for negative values |
| Variant | hover:, md:, dark:, focus: |
when the style applies |
| Modifier | /50, /75 |
opacity (colours) or line height (font size) |
From CSS to utilities
Compare a hand-written component with its Tailwind version. Both produce the same badge:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
<style>
.badge {
display: inline-block;
padding: 0.25rem 0.75rem;
border-radius: 9999px;
background: #dcfce7;
color: #166534;
font-size: 0.875rem;
font-weight: 600;
}
</style>
</head>
<body class="space-y-4 p-6">
<p>Classic CSS: <span class="badge">Active</span></p>
<p>Tailwind: <span class="inline-block rounded-full bg-green-100 px-3 py-1 text-sm font-semibold text-green-800">Active</span></p>
</body>
</html>Read the Tailwind version left to right: inline-block, fully rounded, light green background, horizontal padding 3, vertical padding 1, small text, semibold, dark green text. Once you know the vocabulary, the markup describes the design.
The spacing scale
Many utilities (p, m, w, h, gap, inset …) share one numeric scale. In v4 each step is a multiple of the --spacing variable, which defaults to 0.25rem (4px):
| Class | Value | Pixels |
|---|---|---|
p-1 |
0.25rem |
4px |
p-2 |
0.5rem |
8px |
p-4 |
1rem |
16px |
p-8 |
2rem |
32px |
p-13 |
3.25rem |
52px |
Because the scale is calculated, any whole number works in v4 (p-13, mt-17, w-29) — in v3 only the predefined steps existed. Half steps like p-1.5 and p-2.5 work too.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="space-y-2 p-4 font-mono text-sm">
<div class="w-8 bg-sky-500 text-white">8</div>
<div class="w-16 bg-sky-500 text-white">16</div>
<div class="w-24 bg-sky-500 text-white">24</div>
<div class="w-32 bg-sky-500 text-white">32</div>
<div class="w-48 bg-sky-500 text-white">48</div>
<div class="w-64 bg-sky-500 text-white">64 (= 16rem)</div>
</body>
</html>Combining many utilities
Real interfaces stack many classes on one element. Don't worry about the length — each class is easy to read and change.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="bg-gray-100 p-6">
<div class="flex items-center gap-4 rounded-2xl bg-white p-4 shadow-md">
<div class="flex size-14 shrink-0 items-center justify-center rounded-full bg-linear-to-br from-pink-500 to-orange-400 text-2xl">🎧</div>
<div>
<p class="font-semibold text-gray-900">Now playing</p>
<p class="text-sm text-gray-500">Lo-fi beats to learn Tailwind to</p>
</div>
<button class="ml-auto rounded-full bg-gray-900 px-4 py-2 text-sm text-white hover:bg-gray-700">Pause</button>
</div>
</body>
</html>Preflight: Tailwind's base styles
When Tailwind loads, it also injects Preflight, a small reset. It removes default margins, makes headings and plain text the same size, removes list bullets and sets box-sizing: border-box on everything. That's why an <h1> looks like normal text until you add text-3xl font-bold.
Note: In v4 Preflight sets the default border colour to
currentColor(v3 usedgray-200), so always pairborderwith a colour such asborder-gray-200.
Conflicting classes
If you write two utilities for the same property, such as p-2 p-8, the order in the class attribute does not matter — the one that appears later in the generated stylesheet wins. Avoid conflicts; keep one value per property per variant.
Tip: Editor extensions such as Tailwind CSS IntelliSense autocomplete class names, show the generated CSS on hover and warn about conflicts.
Test yourself
With the default --spacing of 0.25rem, how large is p-6?
- 6px
- 0.6rem
- 1.5rem (24px)
- 6rem
In md:hover:bg-sky-500/75, what does /75 do?
- Sets the width to 75%
- Sets the colour's opacity to 75%
- Delays the hover by 75ms
- Applies at 75rem and wider
Why does a plain <h1> look like normal text in a Tailwind page?
- The browser build is broken
- Preflight resets default heading sizes and margins
- Headings are not supported
- You must add
class="h1"