Sub topic 1
The utility-first approach
Utility-first CSS means you build a design by composing many small, single-purpose classes directly in your markup. You do not write a new CSS rule for each component. This chapter explains the working model, how it differs from hand-written CSS and CSS Modules, and the errors people commonly make when they start. Later chapters cover setup and each utility family in detail.
Prerequisites
This chapter assumes you already have the following:
- Working knowledge of React components, props, and JSX.
- Basic HTML and CSS: the box model, flexbox, and media queries.
- Comfort with npm and a bundler such as Vite.
- Basic TypeScript or JavaScript syntax.
You do not need to install anything yet. The setup chapter covers that.
What a utility class is
A utility class is a class name that sets one CSS declaration, or a tight group of closely related ones. Tailwind CSS ships thousands of them. Here are a few:
| Class | CSS it produces |
|---|---|
| `p-4` | `padding: 1rem` (in v4, `calc(var(--spacing) * 4)`) |
| `text-lg` | a larger font size, with a matching line height |
| `font-semibold` | `font-weight: 600` |
| `rounded-lg` | a large border radius |
| `bg-white` | `background-color: white` |
| `flex` | `display: flex` |
You combine them on an element:
export function ProfileCard({ name, role }: { name: string; role: string }) {
return (
<div className="max-w-sm rounded-lg border border-gray-200 bg-white p-6 shadow-sm">
<h2 className="text-lg font-semibold text-gray-900">{name}</h2>
<p className="mt-1 text-sm text-gray-600">{role}</p>
</div>
)
}This component has no stylesheet of its own and no class name that you invented. Each class describes one visual decision: a maximum width, a border color, a padding size. Reading the JSX tells you what the element looks like.
The semantic CSS approach, for contrast
The traditional approach names things by what they are, then writes CSS for that name:
.profile-card {
max-width: 24rem;
border-radius: 0.5rem;
border: 1px solid #e5e7eb;
background: white;
padding: 1.5rem;
box-shadow: 0 1px 2px rgb(0 0 0 / 0.05);
}
.profile-card__name {
font-size: 1.125rem;
font-weight: 600;
color: #111827;
}The markup then carries className="profile-card". This looks tidy, but it has costs that grow with the project:
- You have to invent names, and naming is slow and inconsistent across a team.
- Every new design needs new CSS, so the stylesheet grows.
- Changing a shared rule can break a screen you did not look at, because selectors are global.
- To understand one element you must jump between the component file and the stylesheet.
Where CSS Modules fit
CSS Modules solve the global-scope problem. The build tool rewrites each class name to a unique one per file, so .card in one module cannot collide with .card in another. You still write the rules and still invent the names. You also still switch between two files.
Tailwind takes a different route. There is little new CSS to scope, because you reuse a fixed vocabulary. Two components that both use p-4 produce the same single CSS rule, and that rule is shared.
| Concern | Hand-written CSS | CSS Modules | Tailwind utilities |
|---|---|---|---|
| Naming | You invent names | You invent names | Mostly none |
| Scope | Global | Per file | Shared, but rules are tiny and fixed |
| Where styles live | Separate file | Separate file | In the JSX |
| CSS size over time | Grows with features | Grows with features | Levels off as classes are reused |
| Design consistency | By convention | By convention | Enforced by a shared scale |
How the model works
Three ideas explain why the approach holds up.
A constrained scale
Utilities pull values from a design scale, called the theme. p-4 and p-6 are always the same two steps apart, in every component. You pick from a menu instead of typing arbitrary pixel values. This is the main reason utility-first designs look consistent without a designer reviewing every margin.
Styles sit next to the markup
When the style and the element are in the same place, you can change one without searching for the other. Deleting a component also deletes its styling, so no dead CSS stays behind.
Rules are reused, not duplicated
Tailwind generates CSS only for the classes it finds in your source files. Each class becomes one rule, no matter how many times you use it. The diagram shows the flow.
The scanning step has consequences for how you write class names. The chapter on class detection covers it fully. For now, remember that Tailwind looks for complete class names as plain text.
Objections and honest answers
"This is just inline styles." It is not, for four concrete reasons:
- Utilities can express states and conditions that inline styles cannot:
hover:,focus-visible:,disabled:,dark:and breakpoints such assm:. - Values come from a shared scale, not from free-form numbers.
- Inline styles have very high specificity. Utility classes sit at ordinary class specificity.
- Inline styles cannot use media queries or pseudo-classes at all.
"The class lists are long and ugly." They can be. The answer in React is that you extract components, not CSS classes. If a button appears in twenty places, you write one Button component and the long class list exists once. A later chapter shows this with typed variants.
"I will repeat myself." You repeat class lists only when you repeat markup. In a component-based framework, repeated markup is already a signal to make a component.
A worked example: one decision at a time
Suppose you need a notification banner. Build it by adding one decision per class.
export function Notice({ message }: { message: string }) {
return (
<div className="flex items-center gap-3 rounded-md border border-amber-300 bg-amber-50 px-4 py-3 text-sm text-amber-900">
<span className="font-semibold">Notice</span>
<span>{message}</span>
</div>
)
}Read it in groups:
- Layout:
flex items-center gap-3places children in a row, centers them vertically, and leaves a 0.75rem gap. - Shape:
rounded-md border border-amber-300gives rounded corners and a thin amber border. - Color:
bg-amber-50 text-amber-900sets a pale background with dark text. - Spacing and type:
px-4 py-3 text-smsets padding and a small font size.
To change the spacing, you edit px-4 to px-6. You do not search a stylesheet, and no other component is affected.
Common errors
- Inventing class names that do not exist.
padding-4andbg-primarydo nothing unless Tailwind defines them. Check the documentation or use editor autocomplete. Custom names such asbg-primaryneed a theme entry, which a later chapter covers. - Writing custom CSS for everything anyway. If you add a
.cardclass for each component, you have kept the old approach and added a tool. Reach for a utility first, and write CSS only when no utility fits. - Copying long class lists between files. This duplicates decisions. Extract a React component.
- Expecting class order to control precedence. The order of class names inside
classNamedoes not decide which wins. The order of the rules in the generated stylesheet decides. Ifp-4andp-6are both on an element, the result depends on the stylesheet, not on which you wrote last. Avoid conflicts, and use the merging approach in a later chapter when callers need to override styles. - Assuming utilities replace CSS knowledge. Every utility maps to real CSS. If you do not understand flexbox or the box model, the classes will not help. Looking at the generated CSS in browser developer tools is a good way to check what a class does.
When utility-first is a poor fit
Be clear about the trade-offs before you adopt it.
- Content you do not control. Rendered Markdown or CMS HTML has no place for your classes. You need a stylesheet that targets element selectors, or a typography plugin.
- Large legacy stylesheets. Mixing is possible, since Tailwind can sit beside existing CSS, but expect to manage conflicts in specificity and resets.
- Teams that dislike dense markup. The approach is a matter of preference as well as technique. Components reduce the density, but they do not remove it.
- Highly dynamic, computed styles. Values that come from runtime data, such as a user-chosen color, are better set with a CSS variable or an inline
stylethan with a utility.
Summary of the working model
- You compose small classes that each express one design decision.
- Values come from a shared scale, which keeps designs consistent.
- Tailwind generates one rule per unique class it finds as complete text in your source.
- You manage repetition with React components, not with new CSS classes.
- Dynamic values need care, because classes must appear in full in the source.
Exercise
A teammate says utility classes are the same as inline styles. Give two concrete things utility classes can do that inline styles cannot, and say where you would remove duplication when the same long class list appears in twenty places.
Show answer
Utility classes can express state and condition variants such as hover:, focus-visible:, disabled:, dark: and breakpoint prefixes like sm:, and they draw values from a shared theme scale. Inline styles cannot use pseudo-classes or media queries. To remove duplication, extract a React component (for example a Button) so the class list is written once, instead of creating a new CSS class.
Exercise
This component does not show the expected text color for color="red". What is wrong, and how do you fix it? <p className={text-${color}-600}>Status</p>
Show answer
Tailwind scans source files for complete class names as plain text. The string text-${color}-600 never appears in full, so no CSS is generated for text-red-600. Fix it by mapping values to full class strings, for example const colorClasses = { red: 'text-red-600', green: 'text-green-600' } as const and then className={colorClasses[color]}.