Sub topic 14
Reusing styles with components and @apply
Long class lists repeat quickly. A button that appears in twenty places should not need twenty copies of the same eight utilities. This chapter covers three ways to remove that duplication and how to choose between them. It assumes you know the earlier chapters: utility classes, variants such as hover:, and how Tailwind detects class names in your source files.
Repetition is a markup problem first
Utility-first styling does not ask you to avoid reuse. It moves reuse out of CSS and into your templates. If you copy a card five times, the fix is to define the card once, not to invent a .card class.
Tailwind's guidance, in order of preference:
- Edit in one place when the repeats are in a single file (multi-cursor editing).
- Use a loop when the repeats are siblings in a list.
- Extract a component or template partial.
- Use
@applyonly when none of the above fits.
Components
A component wraps the markup and its classes together. Changing the component changes every use. Here is a React button with two variants.
export function Button({ children, variant = "primary" }) {
const variants = {
primary: "bg-indigo-600 text-white hover:bg-indigo-700",
secondary: "bg-slate-200 text-slate-900 hover:bg-slate-300",
};
return (
<button
className={`rounded-lg px-4 py-2 focus:ring-2 focus:ring-indigo-500 ${variants[variant]}`}
>
{children}
</button>
);
}The variants object follows the rule from the previous chapter. Each full class name appears literally in the source, so Tailwind detects it. Building bg-${color}-600 here would fail.
The same idea works outside React. Vue and Svelte components, Blade or Twig partials, and Jinja macros all work, because Tailwind scans any text file.
Loops
When the repeats are items in a list, you write the classes once inside the loop.
const links = ["Docs", "Blog", "Pricing"];
export function NavLinks() {
return (
<ul className="flex gap-4">
{links.map((name) => (
<li key={name} className="text-slate-600 hover:text-slate-900">
{name}
</li>
))}
</ul>
);
}The class string exists once in the source, even though it renders three times.
@apply
@apply copies utilities into a CSS rule. Use it when you cannot share markup, for example with HTML you do not control, such as output from a CMS or a third-party widget.
@import "tailwindcss";
.btn-primary {
@apply rounded-lg bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-700;
}Variants work inside @apply, as hover:bg-indigo-700 shows. You then write class="btn-primary" in the markup.
Why @apply is a last resort
@apply brings back problems that utility-first styling avoids.
- Naming. You must invent and maintain class names such as
.btn-primary. - Jumping between files. You have to open the CSS file to see what the element looks like.
- Order and specificity. Your custom class sits in the cascade alongside other rules. A utility on the same element may or may not override it, depending on where each rule is placed in the generated CSS.
- No size savings. The utilities are copied into the rule. The CSS output does not shrink.
A component has none of these problems. Prefer it whenever your framework offers one.
Comparing the options
| Approach | Best for | Main cost |
|---|---|---|
| Loop | Identical items in one list | Only works for siblings |
| Component | Repeated UI across pages | Needs a component system |
| `@apply` | Markup you cannot edit | Naming, cascade, indirection |
Exercise
A CMS outputs blog posts as plain HTML, and you cannot add classes to its <a> tags. Which reuse approach fits, and why would a component not work?
Show answer
Use @apply in a CSS rule that targets the links, for example .post-body a { @apply text-indigo-600 underline hover:text-indigo-800; }. A component needs control over the markup, and here the CMS generates it. @apply styles the elements from the CSS side instead.
Exercise
Your Button component builds its color class with ` bg-${color}-600 `. Some colors never get styled in production. How do you fix it?
Show answer
Tailwind cannot detect the full class name in the source. Replace the template string with an object that maps each color to a literal class name, such as { red: "bg-red-600", green: "bg-green-600" }, and look the class up by key.