Sub topic 1
The utility-first approach
Prerequisites
This chapter assumes you know basic HTML structure and common elements. It also assumes you know core CSS ideas: the box model, selectors, and specificity. You do not need to install anything yet. Setup comes in the next chapter.
What utility-first means
Tailwind CSS gives you a large set of small classes. Each class does one job. p-6 sets padding. rounded-xl rounds the corners. text-slate-900 sets the text color. You style an element by listing the classes you need in its class attribute.
This differs from the traditional approach. There you invent a name such as .card, then write a rule in a separate stylesheet. With utilities, you skip the naming step and the separate rule. The styling is visible in the markup.
A worked example
This card uses only utility classes. They cover sizing, spacing, typography, color, radius, shadow, and hover and focus states.
<div class="max-w-sm rounded-xl bg-white p-6 shadow-md">
<h2 class="text-xl font-semibold text-slate-900">Card title</h2>
<p class="mt-2 text-slate-600">Some supporting text for the card.</p>
<button class="mt-4 rounded-lg bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-700 focus:ring-2 focus:ring-indigo-500">
Read more
</button>
</div>Read the classes by group:
| Classes | Purpose |
|---|---|
| `max-w-sm`, `p-6`, `mt-2`, `mt-4`, `px-4`, `py-2` | Sizing and spacing |
| `text-xl`, `font-semibold` | Typography |
| `bg-white`, `text-slate-900`, `bg-indigo-600`, `text-white` | Color |
| `rounded-xl`, `rounded-lg`, `shadow-md` | Radius and shadow |
| `hover:bg-indigo-700`, `focus:ring-2`, `focus:ring-indigo-500` | Interactive states |
The hover: and focus: prefixes are variants. A variant applies a utility only in a given condition. Later chapters cover them in detail.
Compared with custom CSS classes
The same card in traditional CSS would need a class name for each part, plus a stylesheet. The markup would look like this:
<div class="card">
<h2 class="card-title">Card title</h2>
<p class="card-text">Some supporting text for the card.</p>
<button class="card-button">Read more</button>
</div>The trade-offs are concrete:
- Naming. Custom CSS makes you name every piece. Utilities remove that work.
- Context switching. You edit one file instead of moving between HTML and CSS.
- Safe changes. A utility class affects only the element that carries it. Editing
.cardcan change every card on the site. - Stylesheet growth. Custom CSS grows with each feature. Utilities are reused, so the generated CSS grows slowly.
- Constraints. Values come from a shared scale, such as
p-4andp-6. This keeps spacing and color consistent.
The costs are real too. Long class lists make markup harder to scan. Repeated lists must be managed. The final chapter of this tutorial covers components and @apply for that problem.
How utilities relate to the CSS you know
A utility is ordinary CSS with a short name. p-6 produces one declaration, and flex produces display: flex. Nothing is hidden. If you know CSS, you can predict what a class does. The learning task is mostly memorizing names.
Tailwind generates only the CSS for classes it finds in your files. The next chapter explains this process.
Common errors
- Treating utilities as inline styles. They are not. Utilities support variants such as
hover:, breakpoints, and dark mode. Inlinestyleattributes do not. - Copying long class lists everywhere. Repetition becomes hard to maintain. Extract a component when a pattern repeats.
- Inventing class names. Writing
p-7may do nothing if it is not on the scale. Check the names against the documentation.
Exercise
Name two advantages of utility classes over a custom .card class, and one cost.
Show answer
Advantages: you do not have to name things, and a change affects only the element that carries the class. Other valid answers: fewer file switches, consistent values from a shared scale. A cost: long class lists make markup harder to read, and repeated lists need a component or @apply.
Exercise
What does the hover: prefix in hover:bg-indigo-700 do?
Show answer
It is a variant. It applies bg-indigo-700 only while the pointer is over the element.