Sub topic 1
Utility-first styling and why long class lists appear
Prerequisites
This chapter assumes you have:
- Basic HTML and CSS, including classes and the cascade.
- Some familiarity with Tailwind utilities such as
flex,px-4, andbg-blue-500. - Basic knowledge of a component-based UI framework such as React or Vue, or of template partials.
The working model
A utility class is a CSS class that sets one property or a small group of related properties. px-4 sets horizontal padding. font-semibold sets font weight. You style an element by listing utilities in its class attribute instead of writing a named rule in a stylesheet.
Tailwind does not ship every possible class. At build time it scans your source files for class names and generates CSS only for the ones it finds. This scanning has one consequence you will rely on later: Tailwind finds class names as plain text, so a class must appear as a complete string somewhere in your source.
Here is a button styled only with utilities:
<button class="rounded-md bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700">
Save
</button>Each class makes one visible decision: corner shape, background, padding, weight, text color, and hover color.
Why class lists grow
A traditional stylesheet hides these decisions behind a name such as .btn. Utility-first styling puts them in the markup. Every visual choice becomes one more class, and states and screen sizes add more through variants, which are prefixes such as hover: and md:.
A realistic button also needs focus styles, a disabled state, and a larger size on wide screens. That quickly reaches ten to fifteen classes on one element.
The long list is the cost of a deliberate design. It buys the following:
| Benefit | What it means in practice |
|---|---|
| No naming | You do not invent names like `.card-header-inner`. |
| Local changes | Editing one element's classes cannot break another element. |
| Small CSS | Only the utilities you use end up in the output. |
| Constrained values | Spacing and colors come from a shared scale. |
The cost is that the same list can appear in many places.
Where the trade-off hurts
Suppose a page has three buttons that look the same:
<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>To change the button color, you must edit three places, and you must find every other copy in the project too. This is the duplication problem.
It is a template problem, not a CSS problem. The CSS output contains bg-blue-600 once, however many elements use it. What repeats is the markup you wrote. So the fix is usually to reuse the markup, with a component, a partial, or a loop. Writing a new CSS class is the other option, and it fits fewer cases.
What the rest of this tutorial covers
The remaining chapters look at the options in order. You will see when a loop or multi-cursor editing is enough. You will then extract a component and expose variants through props. After that you will write CSS classes with @apply, place them in @layer components, and learn the limits of that approach. The final chapter gives a rule of thumb for choosing between them.
Exercise
The three buttons above share one class list, and the generated CSS contains bg-blue-600 only once. Where does the duplication live, and what does that suggest about the fix?
Show answer
The duplication lives in your markup, where the class list is copied onto each element, not in the generated CSS. The fix therefore usually belongs in the template layer, by reusing markup through a component, partial, or loop, rather than by adding more CSS.
Exercise
Tailwind finds class names by scanning your source files as text. Why does this matter when you write classes?
Show answer
Tailwind only generates CSS for class names it can find as complete strings in the source. A class name built by joining pieces at runtime may never be detected, so its CSS is not generated.