Sub topic 1
Utility-first styling and why long class lists appear
Prerequisites
This chapter assumes you know the following:
- Basic HTML and CSS, including classes, specificity, and the cascade.
- Common Tailwind utility classes such as
flex,px-4, andbg-blue-500. - The idea of a component-based UI framework such as React or Vue, or of template partials in a server-side templating language.
You do not need to know @apply, @layer, or any class-merging library yet. Later chapters cover them. This chapter explains why the problem they solve exists.
What utility-first means
In traditional CSS you name a thing after its purpose, then write rules for that name:
<button class="save-button">Save</button>.save-button {
border-radius: 0.375rem;
background-color: #2563eb;
padding: 0.5rem 1rem;
font-weight: 600;
color: white;
}The class name describes what the element is. The styling lives in a separate file.
A utility-first framework inverts this. Each class does one small job, and you build the design by combining many of them in the markup:
<button class="rounded-md bg-blue-600 px-4 py-2 font-semibold text-white">
Save
</button>A utility class is a class that maps to a single declaration or a very small group of them. Here are some examples:
| Utility | CSS it produces |
|---|---|
| `rounded-md` | `border-radius` set to the medium theme value |
| `bg-blue-600` | `background-color` set to the blue 600 theme color |
| `px-4` | `padding-left` and `padding-right` set to the 4 spacing step |
| `py-2` | `padding-top` and `padding-bottom` set to the 2 spacing step |
| `font-semibold` | `font-weight: 600` |
| `text-white` | `color` set to white |
Tailwind does not ship a prebuilt stylesheet that contains every possible class. It scans your source files for class names and generates CSS only for the ones it finds. This matters later, because it means Tailwind must be able to see each class name as a complete string in your code.
Why this approach is popular
Utility-first styling removes several chronic problems of hand-written CSS.
- No naming. You do not have to invent names like
card-header-inner-wrapper. Naming is hard, and wrong names mislead the next reader. - Local changes. Editing the classes on one element changes only that element. In a large stylesheet, editing a shared rule can break pages you did not mean to touch.
- A CSS file that stops growing. Utilities are reused everywhere, so the generated CSS grows with the number of distinct utilities you use, not with the number of components. Most projects reach a plateau.
- Constrained values. Spacing, colors, and font sizes come from a theme. Using
px-4instead ofpadding: 15pxkeeps the design consistent. - Less jumping between files. You see an element's markup and its styling in the same place.
- Variants in the same place. State and screen-size changes are written as prefixes, such as
hover:bg-blue-700ormd:flex. You do not need a separate media query block.
The last point is worth a short example, because it explains why class lists grow quickly:
<button
class="rounded-md bg-blue-600 px-4 py-2 font-semibold text-white
hover:bg-blue-700
focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600
disabled:opacity-50
md:px-6"
>
Save
</button>This one button has a base style, a hover state, a keyboard focus ring, a disabled state, and a wider layout on medium screens. That is about 15 classes, and the button is simple. In plain CSS the same rules would take several selectors and a media query. Utility-first moves that length from the stylesheet into the markup.
Why long class lists appear
Long class lists are not a sign that you are using Tailwind badly. They follow from how the approach works.
- One class per declaration. A traditional rule can set ten properties under one name. Utilities need roughly one class per property.
- Every state is spelled out. Hover, focus, disabled, dark mode, and each breakpoint add prefixed copies of the classes they change.
- Real interfaces have many parts. A card has a container, a header, a title, a body, and a footer. Each part needs its own list.
- Nothing is inherited by name. In CSS, a name such as
.btngives you the whole style through one token. In Tailwind, if two elements share a look, both must list all the classes that create it.
The fourth cause is the important one for this tutorial. Length alone is not a problem. A single 15-class button is readable and easy to change. The problem begins when the same group of classes appears in many places.
Seeing the real problem: repeated groups
Consider three buttons on one page:
<button class="rounded-md bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700">Save</button>
<button class="rounded-md bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700">Publish</button>
<button class="rounded-md bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700">Send</button>Imagine the design team decides primary buttons should use rounded-lg instead of rounded-md. You now have to find and change every copy. If you miss one, the interface becomes inconsistent. If one copy was edited by hand a month ago, it no longer matches the others, and you cannot tell whether that was intentional.
This is the same kind of problem as copy-pasted code in any language. It is a duplication problem in your templates. Notice what the problem is not: the CSS is fine. Tailwind generated each utility once, and the stylesheet contains no duplicated rules. What is duplicated is the decision about which utilities make up a primary button.
That distinction shapes the rest of the tutorial. Because the duplication lives in the template, the first fix to reach for is usually a template tool: a component, a partial, or a loop. Only some cases call for moving the grouping into CSS with @apply.
Long versus duplicated
Keep these two ideas apart when you review code.
| Situation | Is it a problem? | Typical response |
|---|---|---|
| One element has 20 classes and appears once | Rarely. It is verbose but local. | Leave it. Format it so it is readable. |
| A list is repeated across a few unrelated pages | Maybe. Check whether the copies should always change together. | Consider a shared component. |
| A list is repeated many times and must stay identical | Yes. Edits will drift. | Extract it into one place. |
| Many elements look similar but differ in small ways | Usually yes, but plan for variants. | Extract a component with props. |
| Items in a list are generated from data | No. The template already loops. | Keep the class list in the loop body. |
A useful test is to ask what would happen if the design changed. If you would want to change all the copies together, they are one concept and belong in one place. If each copy would change on its own schedule, they only look alike by accident, and extracting them would couple things that should stay separate.
What you give up
Utility-first styling has real costs, and knowing them helps you decide when to reuse.
- Noisy markup. Reading a template means reading through styling. Formatting long lists over several lines, grouped by purpose, helps. A common grouping is layout, then spacing, then typography, then color, then state.
- No name for the concept.
rounded-md bg-blue-600 px-4 py-2 …does not say "primary button". A component namedButtondoes. - Drift between copies. Without a single source, similar elements diverge over time.
- Harder overrides. If a reusable element accepts extra classes, a conflict such as
px-2andp-3is not resolved by their order in your markup. It is resolved by their order in the generated stylesheet. A later chapter covers this and how to handle it. - Class detection rules. Tailwind finds classes by scanning source text. A name built at runtime, such as `
bg-${color}-600`, does not appear as a whole string, so Tailwind never generates it. Reuse techniques must keep complete class names in the source.
// Tailwind cannot see "bg-red-600" in this source, so it will not generate it.
const bad = (color) => `bg-${color}-600`;
// Complete strings are visible to the scanner.
const good = {
red: "bg-red-600",
blue: "bg-blue-600",
};A note on Tailwind versions
This tutorial covers both the long-standing behavior and Tailwind CSS v4, which changed how you set up and extend CSS. In v4 you load Tailwind with a single import:
@import "tailwindcss";Where a feature differs between versions, such as how separate stylesheets reach the theme, the chapter that covers it says so. For this chapter, the utility-first model is the same in every version.
The options ahead
You now have the vocabulary to compare the ways to reuse styles. Later chapters cover each one.
- Loops and editing tools. If the repetition comes from rendering a list, you already write the class list once. If it is a handful of copies in one file, multi-cursor editing can be enough.
- Components. Wrap markup and classes in a React component, a Vue component, or a template partial. This is usually the strongest option because it reuses structure as well as styling.
- Variants through props. A component can map a name such as
primaryto a complete class string. @applyand@layer components. Write a named CSS class built from utilities. This suits cases where a template-level tool is not available, such as styling markup you do not control.- Custom utilities. In v4,
@utilityregisters your own class so that it works with prefixes likehover:andmd:.
Summary
Utility-first styling builds each design from many single-purpose classes written directly in the markup. This removes naming work and stops one edit from breaking unrelated pages. The cost is that every element lists all of its classes, and every state adds more. Long lists are normal. The real problem is the same group of classes copied to many places, because the duplication lives in your templates and the generated CSS stays small. Judge each case by whether the copies should change together.
Exercise
A page has one hero banner with 22 Tailwind classes that appears nowhere else. Another page has a "Delete" button, and the same 9-class group is copied onto 14 buttons across the app. Which one is a reuse problem, and why? Also, why does the generated CSS file not grow when you copy the 9-class group onto more buttons?
Show answer
The 14 copied buttons are the reuse problem. The group of classes represents one concept ("delete button") that should change together, so one edit should update all of them. The hero banner is long but appears once, so it needs formatting, not extraction. The CSS does not grow because Tailwind generates each utility once, however many elements use it. The duplication is in the templates, not in the stylesheet.
Exercise
A teammate writes className={bg-${tone}-600 px-4} so that tone can be "red" or "blue". Why might the red and blue backgrounds be missing in the built CSS, and what is the fix?
Show answer
Tailwind scans source files for complete class names. The strings bg-red-600 and bg-blue-600 never appear in the source, so Tailwind does not generate them. The fix is to write the complete names, for example in a lookup object such as { red: "bg-red-600", blue: "bg-blue-600" }, and pick from it by key.