Sub topic 13
Choosing between components, loops, and @apply
This chapter assumes you know Tailwind utility classes, basic CSS, and either a component framework such as React or Vue or template partials. It also assumes you have seen the three reuse techniques from earlier chapters. Here you will pick between them.
The options side by side
Each technique removes duplication in a different place.
| Technique | Where the reuse lives | Best for | Main cost |
|---|---|---|---|
| Loop or partial | One template, rendered many times | Repeated items from data, such as cards or table rows | Only helps inside one template |
| Component | Markup and classes together | Repeated UI with structure, such as buttons, inputs, and cards | Needs a framework or partials |
| @apply class | A named CSS class | Markup you cannot wrap, such as CMS output or third-party HTML | Adds a CSS layer to maintain |
| @utility | A custom utility name | One small pattern that needs `hover:` or `md:` | Only suits small patterns |
A decision flow
Start from the duplication you see and work through the questions in order. Stop at the first match.
Applying the flow
Loop first. If one template renders five identical cards, the classes appear once in the source. There is nothing else to reuse. Edit the single copy.
Component next. If buttons appear on many pages, extract a component. It keeps markup, classes, and behavior in one file. Expose variants as props, and map each variant name to a complete class string so Tailwind can detect every class. Never build names such as ` bg-${color}-600 `, because the scanner cannot see them.
@apply when no component fits. Use it for HTML you do not control, such as rendered Markdown or a third-party widget. Put the class in @layer components so utilities can still override it:
@import "tailwindcss";
@layer components {
.prose-link {
@apply font-semibold text-blue-600 underline;
}
.prose-link:hover {
@apply text-blue-800;
}
}@utility for small patterns. In Tailwind v4, a custom utility works with variants, so md:btn-base is valid:
@import "tailwindcss";
@utility btn-base {
@apply rounded-md px-4 py-2 font-semibold;
}Checks before you commit to @apply
- Scoped styles. In Tailwind v4, a Vue or Svelte style block cannot see your theme. Add
@reference "../../app.css";at the top of the block before using@apply. Tailwind v3 does not need this. - Override order. Conflicts between two classes are settled by their order in the generated CSS, not by their order in the
classattribute. When a component accepts aclassNameoverride, merge withtwMergeso the caller'sp-3replaces yourpx-2 py-1. - Hidden coupling. A class such as
.btn-primaryhides what it does. Readers must open the stylesheet. A component keeps the classes visible next to the markup. - Growth. If your
@applyclasses begin to mirror every component, you are rebuilding a traditional CSS architecture. Prefer a component.
A worked decision
Suppose your app has a Button component with primary and secondary variants. A blog section renders Markdown from a CMS, and the generated links have no classes.
- Buttons are repeated, structured, and wrappable, so use a component with a variants map and
twMerge. - The Markdown links cannot take component props, so write a
.prose-linkclass with@applyin@layer components. - A shared "focus ring" appears on many elements and must work as
focus-visible:, so define it with@utility.
Exercise
Your team renders a list of 20 product cards from an array in one React file, and the same card style is also needed in an email template that is plain HTML with no framework. Which technique do you use for each place, and why?
Show answer
In the React file, use a loop over the array. The card markup is written once, so no extra abstraction is needed. In the email template you cannot use components, so write a class with @apply in @layer components (or paste the utilities if there is only one use). The deciding factors are whether the repetition is data-driven in one template and whether a component boundary is available.