Sub topic 1
How utility classes map to CSS
A Tailwind utility class is a name for one CSS declaration, or a small fixed group of them. This chapter explains how that mapping works, what Tailwind generates from your markup, and which mistakes come from misreading it. The rest of the tutorial applies the same idea to spacing, sizing, and type.
Prerequisites
This chapter assumes you have the following:
- You can write basic HTML elements and use the
classattribute. - You know the CSS box model: content, padding, border, and margin.
- You recognize CSS units such as
px,rem, and percentages. - You have a project with Tailwind CSS installed, either v3 or v4. Differences between the two are marked where they matter.
The utility-first idea
Traditional CSS names a component and describes it in a stylesheet:
.card-body {
padding: 1rem 1.5rem;
margin-top: 2rem;
}Tailwind inverts this. It provides many small classes, each with one job, and you combine them in the markup:
<div class="mt-8 px-6 py-4">
Content
</div>You do not invent a name like card-body. You describe the result directly: top margin, horizontal padding, vertical padding. The trade-off is longer class attributes in exchange for three things:
- You never switch files to see what an element looks like.
- You do not create CSS that nothing else reuses.
- Changing one element cannot break another, because classes do not carry hidden context.
Reading a class name
Most utility names follow the pattern property-value, with optional modifiers in front:
md:hover:-mt-4
│ │ │└─ value (4 on the spacing scale)
│ │ └── property abbreviation (mt = margin-top), "-" makes it negative
│ └──────── state variant
└─────────── breakpoint variantEach part has a fixed role:
| Part | Example | Meaning |
|---|---|---|
| Variant | `md:`, `hover:` | The condition under which the rule applies |
| Negative sign | `-` in `-mb-2` | Negates the value |
| Property | `mt`, `px`, `w`, `text` | Which CSS property or properties are set |
| Value | `4`, `1/2`, `full`, `lg` | A key from the scale, a keyword, or a fraction |
The value is a key in a lookup table, not a raw number. p-4 does not mean 4 pixels. It means "the spacing-scale entry named 4", which Tailwind defines as 1rem. The next chapter covers that scale.
What each class generates
Take the first example from the introduction and look at the CSS that Tailwind v3 produces for it:
<div class="mx-auto mt-8 -mb-2 px-6 py-4">
Content
</div>.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;
}Three points follow from this output:
- Some classes set two properties.
px-6is shorthand for left and right padding, andmx-autodoes the same for margins. - Negative values are part of the class name.
-mb-2yieldsmargin-bottom: -0.5rem. - The class selector is the only selector. Every rule has the same specificity, one class. This is why utilities are easy to override predictably.
In v4, the values reference a CSS variable instead of fixed rem numbers, and horizontal and vertical shorthands use logical properties. For example, px-6 becomes padding-inline: calc(var(--spacing) * 6). The visible result is the same in a left-to-right layout. The difference matters when you inspect generated CSS in the browser's developer tools.
Variants wrap the same rule
A variant prefix does not create a new kind of class. It wraps the same declaration in a condition. Given md:p-8, Tailwind emits roughly this:
@media (min-width: 768px) {
.md\:p-8 {
padding: 2rem;
}
}The colon in the class name is escaped with a backslash in the stylesheet, as is the slash in w-1/2, which becomes .w-1\/2. You never write the backslashes yourself. They explain why those characters appear in the HTML but look odd in the generated CSS.
State variants work the same way. hover: wraps the selector in :hover, and md: wraps the rule in a media query. Chapter 12 covers breakpoints in detail.
Only the classes you use are generated
Tailwind does not ship every possible class. It scans your source files for strings that look like class names and generates CSS only for the ones it finds. This keeps the stylesheet small. It also causes the most common error with utility classes.
// Broken: Tailwind never sees the complete class name
const gap = 4;
const html = `<ul class="flex gap-${gap}">...</ul>`;The scanner reads source as plain text and does not run your code. It sees gap-${gap}, which matches no class, so gap-4 is never generated and the element has no gap. The fix is to write complete class names so the scanner can find them:
// Works: each complete class name appears literally in the source
const gapClasses = {
small: "gap-2",
medium: "gap-4",
large: "gap-8",
};
const size = "medium";
const html = `<ul class="flex ${gapClasses[size]}">...</ul>`;Order in HTML does not decide conflicts
When two utilities set the same property, the winner is whichever appears later in the generated stylesheet. Tailwind sorts its output, and the order of names in your class attribute plays no part.
<!-- Both set padding. The result does not depend on which is written first. -->
<div class="p-4 p-8">Ambiguous</div>
<div class="p-8 p-4">Same result</div>Two same-property classes on one element is a bug. Write one of them, or move one behind a variant such as md:p-8. Tailwind sorts variant rules after plain ones, which is how the responsive pattern works:
<section class="p-4 md:p-8 lg:p-12">
<p class="text-lg md:text-xl">Padding and text grow at larger breakpoints.</p>
</section>Below 768px, p-4 applies. From 768px up, md:p-8 overrides it. From 1024px up, lg:p-12 overrides both. The same-property rule holds because each variant sits in its own later position in the output.
How the pieces fit together
The theme lookup in step three is where the spacing and type scales live. Later chapters return to it when you extend the defaults with theme.extend in v3 or @theme in v4.
Arbitrary values at a glance
When a value is not in the theme, square brackets pass a literal CSS value through:
<div class="w-[320px] mt-[3px] text-[18px]">One-off values</div>The mapping rule is unchanged: the class name still corresponds to a single declaration, and the bracketed text becomes the value. Chapter 13 covers when to use this and when to extend the theme instead.
Common errors
- Dynamic class names. As shown above, fragments joined at runtime are never generated.
- Expecting the number to be pixels.
mt-8is2rem, not 8px. It scales if the user changes the root font size. - Conflicting same-property classes.
p-4 p-8is not "last one wins" in the attribute. Remove one. - Using v3.3 or v3.4 features on an older install. Classes such as
size-10(v3.4) andtext-base/7(v3.3) silently produce nothing on older versions. Check your installed version when a class has no effect. - Stale scanning configuration. In v3, if a file is outside the
contentpaths intailwind.config.js, its classes are not generated. The symptom is a class that works in one file and not another.
Summary of the mapping
- A class name is a key:
property-value, optionally prefixed by variants. - Each class becomes one rule with a single class selector.
- Values come from a theme lookup, a fraction, a keyword, or square brackets.
- Variants wrap the rule in a media query or a pseudo-class selector.
- Only complete class names found in your source are generated.
Exercise
A teammate writes <div class={mt-${level}}> in a template, where level is a variable that is 4 at runtime. The margin never appears. Explain why, and show a fix that keeps the choice at runtime for the values 2, 4, and 8.
Show answer
Tailwind scans source files as plain text and does not run the code. It sees mt-${level}, which is not a complete class name, so mt-4 is never generated. The fix is to write each full class name literally and select among them, for example const marginClasses = { 2: "mt-2", 4: "mt-4", 8: "mt-8" }; and then use marginClasses[level] in the template. Each of mt-2, mt-4, and mt-8 now appears as a complete string, so all three are generated.
Exercise
Write the CSS that Tailwind v3 generates for -mb-2 and for md:px-6, assuming the md breakpoint is 768px and the spacing scale defines 2 as 0.5rem and 6 as 1.5rem.
Show answer
For -mb-2: .-mb-2 { margin-bottom: -0.5rem; }. For md:px-6: @media (min-width: 768px) { .md\:px-6 { padding-left: 1.5rem; padding-right: 1.5rem; } }. The negative sign negates the scale value, and the md: prefix wraps the same two declarations in a media query with an escaped colon in the selector.