Sub topic 7
Writing CSS classes with @apply
This chapter assumes you know basic HTML and CSS, common Tailwind utilities such as px-4 and bg-blue-600, and the idea of a component or template partial. It covers the @apply directive: what it does, how to write a class with it, and where it stops being useful.
The problem it solves
Repeated utility lists are the usual reason people reach for @apply. Here the same classes appear on three buttons:
<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>If the project has a component system, extracting a component is usually the better fix. When it has none, such as plain server-rendered HTML, @apply lets you give the list a single name.
What @apply does
@apply is a Tailwind directive. It copies the CSS declarations of one or more utility classes into a rule you write. You list the utilities, and Tailwind expands them at build time.
@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 becomes short:
<button class="btn-primary">Save</button>
<button class="btn-primary">Publish</button>
<button class="btn-primary">Send</button>Changing the button now means editing one rule.
How the pieces fit
Why use @layer components
Wrapping the rule in @layer components puts it below the utility classes in the cascade. Utilities then win when both apply to the same element. This matters when you want to adjust one button without editing the shared rule:
<button class="btn-primary px-8">Wide save</button>Here px-8 overrides the px-4 that .btn-primary applied. Without the layer, the order of rules in your stylesheet would decide, and the result could be surprising.
Hover and other variants
Inside @apply you can use variants as you would in markup. This does the same job as the separate :hover rule above:
@layer components {
.btn-primary {
@apply rounded-md bg-blue-600 px-4 py-2 font-semibold text-white hover:bg-blue-700 md:px-6;
}
}Because variants work here, you don't need a second rule for each state.
Limits to know early
@applyonly reuses styles. It does not reuse markup, so a card with an icon, title and body still needs a partial or component for its structure.- Class names add a second place to look. A reader must open the stylesheet to see what
.btn-primarydoes. - Tailwind v4 changes how
@applyworks in separate stylesheets, such as a Vue<style>block. There you must add@referencefirst:
<style>
@reference "../../app.css";
h1 {
@apply text-2xl font-bold;
}
</style>- Utilities that you apply once are copied into every rule that uses them, so heavy use of
@applycan enlarge the output CSS compared with using utilities in markup.
Exercise
A .btn-primary rule uses @apply px-4, and it sits inside @layer components. You write <button class="btn-primary px-8">. Which padding does the button get, and why?
Show answer
It gets px-8. Rules in @layer components come before the utilities layer in the cascade, so the px-8 utility overrides the px-4 copied in by @apply.
Exercise
You move a rule that uses @apply bg-blue-600 into a Vue <style> block in Tailwind v4, and the build reports an unknown utility. What line fixes it?
Show answer
Add @reference "../../app.css"; (using the correct relative path to your main stylesheet) at the top of the style block, so the file can see the theme and utilities.