Sub topic 1
The utility-first approach
Prerequisites
This chapter assumes you can read React components and JSX, and that you know basic CSS: the box model, flexbox, and media queries. You should also be comfortable with npm and Vite. The examples use TypeScript, but the ideas apply to plain JavaScript too. Setup comes in the next chapter, so you only need to read the code here.
What utility-first means
Tailwind CSS ships a large set of small classes. Each class does one job, such as p-6 for padding or text-gray-900 for text color. These are called utility classes. You style an element by listing the utilities it needs in its className.
Compare three ways to style the same card.
- Custom CSS: you invent a class name like
.profile-card, then write its rules in a separate stylesheet. - CSS Modules: you write the same rules in a
.module.cssfile. The build tool renames each class so it applies only to the component that imports it. - Utility-first: you write no rules. You compose existing classes directly in the JSX.
Here is the utility-first version.
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>
)
}The component has no stylesheet and no invented names. To see what the card looks like, you read its markup.
How the working model differs
With custom CSS, markup and styles live in two places. You name a class, then keep both sides in sync. Tailwind removes the naming step, and it changes three things.
- Styles sit next to the markup. Changing an element means editing one file. A class on one element cannot affect another element by accident.
- Values come from a shared scale.
p-6always means the same spacing, andtext-gray-600always means the same color. Spacing and color stay consistent without a separate design document. - The stylesheet stops growing. Tailwind generates only the classes your source files use. New features reuse the same utilities instead of adding new rules.
Common objections and answers
Intermediate developers usually raise the same concerns.
| Objection | Response |
|---|---|
| Long class lists are hard to read. | In React you extract a component, not a CSS class. The list then appears once. Later chapters cover this. |
| It is just inline styles. | Utilities support media queries, hover and focus states, and dark mode. Inline styles support none of these. |
| It repeats the same CSS everywhere. | The generated CSS contains each rule once, however many elements use it. |
Trade-offs to know
Utility-first has costs. Class lists grow long on complex elements. You must learn the class names, though editor tooling helps. Tailwind also reads class names as plain text, which limits how you can build them dynamically. A later chapter covers that limit.
Exercise
Exercise
Name two differences between the utility-first approach and writing a custom .profile-card class in a stylesheet.
Show answer
Any two of these. First, utility-first has no naming step, because you compose existing classes in the JSX instead of inventing a name. Second, the styles live next to the markup instead of in a separate file. Third, values come from a shared scale, so spacing and color stay consistent. Fourth, the generated stylesheet does not grow with each new component, because Tailwind produces only the classes you use.
Exercise
A teammate says long class lists mean you will repeat the same styling in many places. In a React project, how do you avoid this?
Show answer
Extract the repeated markup and its classes into a React component. The class list then exists in one place, and every use of the component shares it.