Sub topic 12
Pitfalls and limits of @apply
This chapter assumes you have read the earlier chapters on extracting components and writing classes with @apply. It also assumes you know the cascade, specificity, and basic Tailwind CSS v4 syntax (@import "tailwindcss", @theme, @layer). Examples use v4. Where v3 behaved differently, the text says so.
@apply is useful, but it is not a shortcut around component extraction. It copies declarations, it interacts with the cascade in ways that surprise people, and it only understands real utilities. Each pitfall below has a cause, a symptom, and a fix.
Pitfall 1: @apply copies declarations
@apply does not make classes share anything. Tailwind looks up each utility and pastes its declarations into your rule. Two classes that apply the same utilities each carry their own copy.
@import "tailwindcss";
@layer components {
.card {
@apply rounded-lg p-4;
}
.panel {
@apply rounded-lg p-4;
}
}The compiled output contains the same declarations twice, once per class:
.card {
border-radius: var(--radius-lg);
padding: calc(var(--spacing) * 4);
}
.panel {
border-radius: var(--radius-lg);
padding: calc(var(--spacing) * 4);
}Putting rounded-lg directly in HTML is cheaper, because the .rounded-lg rule exists once however many elements use it. HTML with repeated utility classes compresses very well over gzip or brotli, so repetition in markup costs little. Repetition in CSS declarations is also small in absolute terms, but it grows with every semantic class you add.
This is not usually a reason to avoid @apply. It does remove one argument people give in its favor: that it makes the stylesheet smaller. It does not.
Pitfall 2: the cascade, layers, and overrides
You may want <button class="btn bg-red-600"> to be red even though .btn applies bg-blue-600. Whether that works depends on where .btn lives.
Tailwind v4 emits its output in native CSS cascade layers in this order: theme, base, components, utilities. Later layers win over earlier ones. Rules that are not in any layer win over every layered rule, regardless of source order or specificity.
@import "tailwindcss";
/* Unlayered: beats every utility class */
.btn {
@apply bg-blue-600 px-4 py-2 text-white;
}<button class="btn bg-red-600">Delete</button>The button stays blue. The unlayered .btn rule beats .bg-red-600 in the utilities layer. Moving the rule into @layer components fixes it:
@import "tailwindcss";
@layer components {
.btn {
@apply bg-blue-600 px-4 py-2 text-white;
}
}Now .bg-red-600 in the later utilities layer wins, and the button is red.
The layer fixes the order between layers, but it does nothing inside one layer. Two component classes in the same layer fall back to normal specificity and source order. The component layer also does not help when a rule has higher specificity than a utility inside a later layer. Layer order is decided first, and specificity only matters within a layer.
Pitfall 3: writing order is not cascade order
Inside a single @apply, the order you write utilities in does not control which one wins. Tailwind emits the declarations in its own utility sort order.
@layer components {
.badge {
@apply px-2 px-4;
}
}Listing two conflicting utilities in one @apply leaves the result to Tailwind's sorting. Do not rely on "last one wins". If you need a base value and an override, put the override on the element, or in a separate rule that comes later in the cascade:
@layer components {
.badge {
@apply px-2 py-1 text-sm;
}
.badge-large {
@apply px-4 py-2 text-base;
}
}With both classes on an element (class="badge badge-large"), source order within the layer decides, and .badge-large comes later. Tailwind's sorting still applies inside each rule.
Pitfall 4: only utilities can be applied
@apply accepts utilities. In v4, a class you wrote in plain CSS is not a utility, so you cannot apply it.
@layer components {
.btn {
@apply rounded-md px-4 py-2 font-semibold;
}
.btn-primary {
@apply btn bg-blue-600 text-white; /* error: unknown utility class */
}
}Tailwind reports that it cannot apply an unknown utility class. In v3, applying classes from @layer components worked, so this error often appears during an upgrade.
The fix is to register the shared piece with @utility, which makes it a real utility:
@import "tailwindcss";
@utility btn {
@apply rounded-md px-4 py-2 font-semibold;
}
.btn-primary {
@apply btn bg-blue-600 text-white;
}A custom utility defined with @utility also works with variants in HTML (hover:btn, md:btn). Plain classes in @layer components do not. Remember that @utility output goes in the utilities layer, so it can be overridden only by utilities that Tailwind sorts after it.
Pitfall 5: no dynamic class names
@apply runs at build time on literal names. It cannot interpolate values, read props, or choose a color at runtime. A variable such as bg-${color}-500 is not valid in CSS, and the same rule explains why string-built class names fail in templates: Tailwind scans source text for complete class names.
For per-instance values, use one of these:
- A map from variant names to complete class strings, as in the
variantsobject from the component chapter. - Separate classes per variant, such as
.btn-primaryand.btn-danger. - A CSS custom property set on the element, such as
style="--accent: #2563eb", read in your CSS withvar(--accent).
Pitfall 6: separate stylesheets need @reference
A CSS module, a Vue <style> block, or a Svelte <style> block is processed as its own file. It does not know your theme or utilities, so @apply fails with an unknown-utility error.
<style>
@reference "../../app.css";
h1 {
@apply text-2xl font-bold;
}
</style>@reference imports your theme, utilities, and custom definitions for lookup only. It emits no CSS, so nothing is duplicated.
Do not put @import "tailwindcss" in each of those files instead. Every file would then emit its own copy of Preflight and the theme variables, which bloats the output and can break the cascade. Each file that uses @apply or variants also adds build work. If you only need theme values, skip @apply and read the CSS variables directly:
h1 {
color: var(--color-blue-600);
font-size: var(--text-2xl);
}This works in any stylesheet that loads after the main Tailwind file. It needs no @reference.
Pitfall 7: tailwind-merge does not know your classes
twMerge resolves conflicts between Tailwind utility classes. It treats unknown class names as opaque strings.
import { twMerge } from "tailwind-merge";
twMerge("px-2 py-1", "p-3"); // "p-3"
twMerge("btn px-4", "px-8"); // "btn px-8" (px-4 removed, btn kept)
twMerge("btn-primary", "bg-red-600"); // "btn-primary bg-red-600"The last line shows the limit. btn-primary may set a background color, but twMerge cannot see that. It keeps both classes. The cascade then decides which wins, and Pitfall 2 applies. If a component must let callers override colors safely, keep the colors as real utilities in the class string. This is a strong reason to prefer component variants over @apply classes when overrides matter.
Pitfall 8: hidden styling and naming costs
Utility-first markup shows you what an element looks like. A class like btn-primary does not. To change one padding value you open a CSS file, find the rule, and check what else uses it.
Semantic names also invite the problems utility-first was meant to avoid:
- Names that describe use in one place, such as
.sidebar-save-button, stop fitting when the second place appears. - One-off tweaks create
.btn-primary-small-rounded. This is a sign the class should have been a component with props. - Unused classes pile up in CSS, because nothing in a template tells you a rule is dead.
Some @apply use is fine for the same reasons CSS was always fine: styling third-party markup you cannot add classes to, or content rendered from Markdown. Use it there on purpose.
Summary table
| Pitfall | Symptom | Fix |
|---|---|---|
| Copied declarations | CSS grows with each class | Extract a component and keep utilities in markup |
| Unlayered rule | Utility on the element does not override | Put the rule in `@layer components` |
| Conflicting utilities in one `@apply` | Result differs from writing order | Split into separate rules or override on the element |
| Applying a plain class | "Unknown utility class" error | Define it with `@utility` |
| Dynamic names | Style missing or build error | Use full class strings or CSS variables |
| Scoped stylesheet | `@apply` fails in Vue, Svelte, or modules | Add `@reference`, or use `var(--...)` |
| `twMerge` with custom classes | Both classes remain | Use real utilities for overridable properties |
Exercise
A stylesheet contains .alert { @apply bg-yellow-100 p-4 text-yellow-900; } outside any @layer, and your markup uses <div class="alert bg-red-100">. The alert is yellow. Explain why, and give two fixes.
Show answer
Unlayered CSS beats every rule inside a cascade layer, and Tailwind's utilities such as .bg-red-100 live in the utilities layer. So .alert wins even though the utility is on the element. Fix one: move the rule into @layer components, so the later utilities layer wins and the alert is red. Fix two: turn the alert into a component with a variant prop that chooses complete class strings, so no unlayered class is involved.
Exercise
You write .btn-primary { @apply btn bg-blue-600; } after defining .btn in plain CSS, and the build fails on Tailwind v4. What is wrong, and how do you fix it?
Show answer
.btn is not a utility, and @apply only accepts utilities. Define it with @utility btn { @apply rounded-md px-4 py-2; }. After that, @apply btn bg-blue-600 works, and hover:btn or md:btn also become valid in markup.
Exercise
In a Vue component's <style> block, @apply text-2xl font-bold fails with an unknown utility error. Name the cause and the correct fix, and say why importing tailwindcss in that block is the wrong fix.
Show answer
The block is processed as a separate stylesheet that does not know your theme or utilities. Add @reference "../../app.css"; (with the correct path to your main stylesheet) at the top of the block. @import "tailwindcss" would emit another copy of Preflight and the theme variables in every such block, which bloats the output and can disturb the cascade. @reference loads definitions for lookup only and emits no CSS.