Sub topic 12
Pitfalls and limits of @apply
This chapter assumes you know Tailwind utility classes, the @layer components pattern, and basic component-based UI. It covers where @apply causes trouble and how to avoid it.
@apply moves the duplication, it does not remove it
@apply copies the declarations of each utility into your class. If .btn-primary and .btn-secondary both apply rounded-md px-4 py-2 font-semibold, that CSS is emitted twice. Utility classes in markup are shared by every element that uses them, so the CSS file stays small. With heavy @apply use, the file grows as you add classes.
Specificity and source order
A component class and a utility on the same element compete under normal cascade rules. Placing the class in @layer components helps in Tailwind's layer order, because utilities come after components and win. Outside a layer, source order and specificity decide.
@layer components {
.btn-primary {
@apply rounded-md bg-blue-600 px-4 py-2 text-white;
}
}<button class="btn-primary px-8">Save</button>Here px-8 overrides the padding from .btn-primary because utilities are emitted after components. If you write the same rule outside any layer, the result depends on file order, and the override can silently fail.
Hover and other states need care
A variant can be applied inside @apply, but the output must match what you expect. This works in Tailwind v3 and v4:
@layer components {
.btn-primary {
@apply rounded-md bg-blue-600 px-4 py-2 text-white hover:bg-blue-700;
}
}Problems appear when you apply a class that already exists as your own custom class. In v3, you can only @apply utilities that Tailwind knows. Applying another custom class fails unless it is defined in a layer.
Version differences
The mechanism changes between versions, and mixing them is a common source of errors.
| Situation | Tailwind v3 | Tailwind v4 |
|---|---|---|
| Custom class usable with `hover:` or `md:` | Define it in `@layer utilities` | Define it with `@utility` |
| `@apply` in a separate stylesheet (Vue, Svelte, CSS modules) | Works through the config | Needs `@reference` to the main CSS file |
| Theme access | `theme()` and config | CSS variables and `@theme` |
In v4, a stylesheet that is processed separately has no access to your theme or utilities. Without @reference, @apply reports that a class does not exist.
<style>
@reference "../../app.css";
h1 {
@apply text-2xl font-bold;
}
</style>Do not use @import "tailwindcss" in each component stylesheet to fix this. It duplicates the whole framework's CSS in every file. @reference imports only what is needed to resolve classes, and it emits no CSS.
A custom class that must work with variants such as hover: or md: should be a utility, not a component class.
@import "tailwindcss";
@utility btn-base {
@apply rounded-md px-4 py-2 font-semibold;
}<button class="btn-base md:px-8 hover:bg-gray-100">Save</button>Loss of visibility
With utilities in markup, you see what an element looks like when you read the template. With @apply, you must open the CSS file. Naming also becomes a cost. A name like .card-title-large tends to drift away from what the class does, and nobody is sure whether it is safe to delete.
Dynamic styling is another limit. @apply cannot take a prop or a runtime value. A component can choose between full class strings in JavaScript, but a CSS class cannot. For variants, you end up writing more classes, such as .btn-primary and .btn-secondary, or using data attributes.
Class names cannot be built dynamically
This applies to markup and to component code. Tailwind scans source files for complete class names as plain text.
// Broken: Tailwind never sees "bg-red-600"
const color = "red";
<button className={`bg-${color}-600`}>Delete</button>Use complete strings in a lookup object instead, as in the variants map in the earlier component chapter.
A practical rule
Use @apply for small, stable, project-wide patterns that have no framework component to carry them. This includes styles for markup you do not control, such as rendered Markdown. Prefer a component when you have one. Keep @apply classes few, and put them in a layer.
Exercise
A Vue component's <style> block uses @apply text-2xl font-bold and the build fails in Tailwind v4 with an error saying the utility does not exist. What is missing, and why is @import "tailwindcss" the wrong fix?
Show answer
The block needs @reference "../../app.css"; (pointing at the main stylesheet) so @apply can resolve the theme and utilities. @import "tailwindcss" would work but would copy all of Tailwind's generated CSS into that component's output. @reference imports for resolution only and emits no CSS.
Exercise
Why does ` bg-${color}-600 ` fail to produce a style, and what is the fix?
Show answer
Tailwind scans source files for complete class names and never sees bg-red-600 as text. The fix is to write full class names, for example in an object such as { red: "bg-red-600", blue: "bg-blue-600" }, and look the value up.