Sub topic 1
How utility classes map to CSS
Prerequisites
This chapter assumes you know basic HTML elements and the class attribute, and the CSS box model (content, padding, margin, border). You should also recognize CSS units such as px, rem, and percentages. You need a project with Tailwind CSS installed.
The working model
Tailwind is a utility-first framework. Instead of writing a custom class such as .card-header and filling it with declarations, you apply small classes that each do one job. Each utility class maps to one CSS declaration, or to a small, fixed group of them.
Here is a mapping for a few classes from this tutorial:
| Class | CSS it generates |
|---|---|
| `p-4` | `padding: 1rem` |
| `mt-8` | `margin-top: 2rem` |
| `font-bold` | `font-weight: 700` |
| `w-1/2` | `width: 50%` |
| `text-lg` | `font-size: 1.125rem` plus `line-height: 1.75rem` |
| `-mb-2` | `margin-bottom: -0.5rem` |
The name follows a pattern: a property prefix, then a value. In mt-8, mt means margin-top and 8 is a step on the spacing scale. The next chapter covers that scale.
Tailwind scans your source files for class names and generates CSS only for the classes it finds. The stylesheet stays small, and it contains nothing you did not use.
Reading a class list
Take this element:
<div class="mx-auto mt-8 -mb-2 px-6 py-4">
Content
</div>Read each class on its own:
mx-autosetsmargin-leftandmargin-righttoauto. A block with a constrained width is centered horizontally.mt-8adds top margin.-mb-2is a negative bottom margin. The leading-flips the sign.px-6sets left and right padding.py-4sets top and bottom padding.
The generated CSS looks roughly like this:
.mx-auto { margin-left: auto; margin-right: auto; }
.mt-8 { margin-top: 2rem; }
.-mb-2 { margin-bottom: -0.5rem; }
.px-6 { padding-left: 1.5rem; padding-right: 1.5rem; }
.py-4 { padding-top: 1rem; padding-bottom: 1rem; }The exact output differs between Tailwind v3 and v4. Version 4 builds its values from a CSS variable, for example calc(var(--spacing) * 8). The mapping from class to property is the same.
Common errors
- Order in the
classattribute does not decide which utility wins. When two utilities set the same property, such asp-4 p-8, the one defined later in the generated stylesheet wins. Avoid writing both. Use a variant instead, as shown in the responsive chapter. - Dynamic class names are not detected. Tailwind reads your files as plain text. A string like `
mt-${size}never appears as a complete class, so no CSS is generated for it. Write whole class names, such asmt-4ormt-8`, and choose between them. - A utility does nothing when its property does not apply.
mx-autoon an inline element has no visible effect, andgap-4needs a flex or grid container.
Exercise
What CSS does -mt-4 produce, and what would be wrong with writing ` mt-${n} ` in a template and expecting Tailwind to generate classes from it?
Show answer
-mt-4 produces margin-top: -1rem. The template string never contains a complete class name, so Tailwind's scan of your source files cannot find it and generates no CSS. Write full class names such as mt-4 and mt-8 and pick between them in code.