Sub topic 7
Writing CSS classes with @apply
This chapter covers the @apply directive: what it does, how to write a component class with it, and where it behaves differently from plain utilities. It assumes you know basic HTML and CSS (classes and the cascade), common Tailwind utilities such as flex, px-4, and bg-blue-500, and the idea of components or template partials. It also assumes you have seen the duplication problem from earlier chapters: the same long class list pasted into many places.
What @apply does
@apply is a Tailwind CSS directive. It copies the declarations of existing utility classes into a CSS rule you write. You list utilities after @apply, and Tailwind replaces that line with the CSS those utilities would produce.
Here is the duplication from the earlier chapters:
<button class="rounded-md bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700">Save</button>
<button class="rounded-md bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700">Publish</button>
<button class="rounded-md bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700">Send</button>With @apply, you move the list into one CSS class:
@layer components {
.btn-primary {
@apply rounded-md bg-blue-600 px-4 py-2 font-semibold text-white;
}
.btn-primary:hover {
@apply bg-blue-700;
}
}The markup then shrinks to a single class name:
<button class="btn-primary">Save</button>
<button class="btn-primary">Publish</button>
<button class="btn-primary">Send</button>The generated CSS contains a .btn-primary rule with the plain declarations (border-radius, background-color, padding, and so on). The utilities you applied are not required to appear anywhere else in your HTML.
How it works internally
Tailwind reads your source files, collects the class names it finds, and generates CSS for them. When it processes your stylesheet and meets @apply, it looks up each named utility, generates its declarations, and inlines them into the surrounding rule. Two consequences follow:
- The utility does not need to be used in your markup.
@applygenerates it on demand. - The result is ordinary CSS. After the build, the browser sees
.btn-primary { ... }and has no knowledge of Tailwind.
A working setup
To run @apply, the stylesheet that contains it must be processed by Tailwind and must have access to the theme and utilities. In Tailwind CSS v4 the entry file starts with @import "tailwindcss";.
@import "tailwindcss";
@layer components {
.card {
@apply rounded-lg border border-gray-200 bg-white p-6 shadow-sm;
}
.card-title {
@apply mb-2 text-lg font-semibold text-gray-900;
}
}<div class="card">
<h2 class="card-title">Weekly report</h2>
<p class="text-gray-600">Revenue is up 4% on last week.</p>
</div>Notice that you can still mix both approaches. The card class supplies the shared look, and ordinary utilities such as text-gray-600 handle one-off details in the markup.
Placing rules in @layer components
The examples above wrap rules in @layer components. This matters because of the cascade. Tailwind organizes its output into layers, and utilities come after components. A rule inside @layer components therefore has lower priority than any utility class on the same element.
That ordering lets you override a component class in the markup:
<button class="btn-primary rounded-full">Save</button>The .btn-primary class sets rounded-md, and the rounded-full utility wins because utilities are later in the cascade than components. If you wrote .btn-primary outside any layer, it would sit above Tailwind's layers, and rounded-full would lose. Putting component classes in @layer components keeps utilities as the final say. The next chapter covers layers in more detail.
Hover, focus, and other variants
You can apply a utility with a variant by writing the variant prefix as usual:
@layer components {
.btn-primary {
@apply rounded-md bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700 focus-visible:outline-2 focus-visible:outline-blue-600 md:px-6;
}
}Tailwind generates the hover, focus, and responsive rules for you. This is shorter than writing .btn-primary:hover { @apply bg-blue-700; } as a separate rule, and it keeps the whole definition in one place. Both forms work. Pick one style and use it consistently in the project.
Pitfalls and limits
Custom classes are not utilities
A class you define with @apply in @layer components is a plain CSS class. In Tailwind CSS v4 you cannot write hover:btn-primary or md:btn-primary in your HTML, because btn-primary is not registered as a utility. The v4 @utility directive handles this case:
@import "tailwindcss";
@utility btn-base {
@apply rounded-md px-4 py-2 font-semibold;
}With this definition, md:btn-base and hover:btn-base both work. Use @utility when you need variants in the markup, and @layer components when you do not.
Applying a class that does not exist
@apply accepts utilities that Tailwind can generate. If you misspell one, the build fails with an error. You can also apply your own utilities defined with @utility, but not arbitrary classes from your own CSS files.
Separate stylesheets need @reference
In Tailwind CSS v4, each stylesheet is processed separately. A Vue <style> block, a Svelte <style> block, or a CSS module does not know your theme or utilities. Using @apply there fails unless you import them with @reference:
<style>
@reference "../../app.css";
h1 {
@apply text-2xl font-bold;
}
</style>@reference imports the theme and utilities for lookup only. It does not copy the Tailwind CSS output into the component stylesheet again. Use @reference "tailwindcss"; if you have no custom theme. In Tailwind CSS v3 this step is not needed, because the build resolved @apply against your configuration automatically.
Specificity and ordering surprises
@apply pastes declarations in the order you list the utilities. If two applied utilities set the same property, such as px-4 and p-2, the result depends on the order in which Tailwind emits the declarations, not on the order you wrote them. Avoid applying conflicting utilities in one rule.
Hidden coupling
A class like .btn-primary hides what it looks like. Anyone reading the HTML has to open the stylesheet to learn what it does. You also add a name to maintain, and an unused rule tends to stay in the file because nobody is sure it is dead. A component, by contrast, keeps its styles next to its markup.
Bundle size
Each @apply rule duplicates declarations in the output. Applying the same 10 utilities in 20 classes produces 20 copies of those declarations. For most projects this is small, but it is the reverse of the usual Tailwind benefit, where each utility exists once.
A comparison
| Concern | Component | `@apply` class |
|---|---|---|
| Reuses markup as well as styles | Yes | No, styles only |
| Works in plain HTML without a framework | No | Yes |
| Styles visible in the markup | Yes | No, one class name |
| Can be overridden by utilities | Yes, with class merging | Yes, in `@layer components` |
| Usable with `hover:` or `md:` in markup | Not applicable | Only with `@utility` in v4 |
| Output size | One copy of each utility | Declarations repeated per class |
When @apply is the right tool
@apply fits best in these situations:
- You have no component system, such as static HTML, server-rendered templates without partials, or a CMS where editors write the markup.
- You must style markup you do not control, such as HTML rendered from Markdown or content from a third-party widget. In that case you target elements, such as
h1ora, rather than classes. - You maintain a small set of widely shared patterns, such as a button or a form field, and want a single class name.
If you have a component system, prefer a component first. It reuses structure as well as styles, and it avoids the hidden coupling described above.
Exercises
Exercise
You define .btn-primary { @apply bg-blue-600 px-4 py-2; } outside any @layer, then write <button class="btn-primary bg-red-600">. Which background does the button get, and what change would let the bg-red-600 utility win?
Show answer
The button is blue. Unlayered CSS beats layered CSS, and Tailwind's utilities are in a layer, so the unlayered .btn-primary rule wins. Wrapping the rule in @layer components { ... } puts it below utilities in the cascade, so bg-red-600 wins.
Exercise
In Tailwind CSS v4, a Vue component's <style> block uses @apply text-2xl font-bold; and the build reports an error. What is missing, and what does it do?
Show answer
The block needs @reference "../../app.css"; (or @reference "tailwindcss";) at the top. It imports the theme and utilities so @apply can look them up, without duplicating the generated CSS in the component's stylesheet.
Exercise
You want md:btn-base to work in your HTML in Tailwind CSS v4. Do you write .btn-base in @layer components or use another directive?
Show answer
Use @utility btn-base { ... }. A class in @layer components is plain CSS and cannot take variant prefixes. @utility registers it as a real utility.