Sub topic 8
Organizing component classes with @layer components
This chapter shows where to put CSS classes that you build with @apply, and why that placement decides which styles win when two rules conflict. It assumes you can read basic CSS, know the cascade and specificity, and have used Tailwind utilities such as px-4 and bg-blue-600. The examples use Tailwind CSS v4. Differences from v3 are called out where they matter.
Why placement matters
Suppose you write a .btn-primary class with @apply. You will later want to write <button class="btn-primary px-8"> and get wider padding on that one button. Whether px-8 wins depends on where .btn-primary sits in the stylesheet and in the cascade.
Plain CSS decides conflicts by specificity first and source order second. That is fragile. If your component class lands after the utilities in the file, it can silently override px-8. Tailwind avoids this by sorting its output into layers. @layer components is the place for the classes you write yourself.
Cascade layers in one minute
A cascade layer is a named group of rules. The browser compares layers before it compares specificity. A rule in a later layer beats a rule in an earlier layer, even if the earlier rule has a more specific selector.
Tailwind v4 uses native CSS cascade layers. When you write @import "tailwindcss";, it declares four layers in this order:
@layer theme, base, components, utilities;| Layer | What goes in it |
|---|---|
| `theme` | Design tokens as CSS variables, such as colors and spacing |
| `base` | Resets and element defaults, such as `h1` and `a` |
| `components` | Class-based building blocks, such as `.btn-primary` and `.card` |
| `utilities` | Every utility class, such as `px-8` and `hover:bg-blue-700` |
Because utilities comes last, any utility on an element beats any rule in components. That is the behavior you want. A component class sets sensible defaults, and a utility in the markup adjusts them.
Writing a component class
Put your classes inside @layer components and build them from utilities with @apply:
@import "tailwindcss";
@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;
}
}Use it in HTML like this:
<button class="btn-primary">Save</button>
<button class="btn-primary px-8">Publish</button>Both buttons share the same defaults. The second button gets px-8 from the utilities layer, which beats the px-4 that .btn-primary applied from the components layer. You did not need !important or a more specific selector.
Why the same rule fails outside a layer
Rules outside any layer are called unlayered. By CSS rules, unlayered styles beat all layered styles, no matter the order. Compare:
@import "tailwindcss";
.btn-primary {
@apply rounded-md bg-blue-600 px-4 py-2 font-semibold text-white;
}Here .btn-primary is unlayered. In <button class="btn-primary px-8">, the unlayered px-4 now beats the utility px-8, so the override silently does nothing. Wrapping the class in @layer components fixes this.
What goes in each layer
Pick the layer by the job the rule does:
- Use
@layer basefor element selectors and defaults that apply everywhere, such asa { @apply underline; }or heading sizes. Utilities and component classes can override them. - Use
@layer componentsfor named, reusable class-based pieces that you intend to tweak with utilities, such as.btn-primary,.card, and.badge. - Do not put your own rules in
@layer utilities. In v4, define a custom utility with@utilityinstead (see below).
A stylesheet that uses both of the first two layers:
@import "tailwindcss";
@layer base {
h1 {
@apply text-2xl font-bold;
}
}
@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;
}
}A utility such as p-8 on a .card element overrides p-6. An h1 with class text-4xl overrides the base text-2xl.
Variants inside component classes
You can use Tailwind variants directly inside @apply. This is often cleaner than a separate :hover rule:
@import "tailwindcss";
@layer components {
.btn-primary {
@apply rounded-md bg-blue-600 px-4 py-2 font-semibold text-white;
@apply hover:bg-blue-700 focus-visible:outline-2 md:px-6;
}
}The variants hover:, focus-visible:, and md: work inside @apply because Tailwind expands them into the same media queries and pseudo-classes it would generate for the utility in markup.
Component classes are not utilities in v4
This is the biggest difference from Tailwind v3, and the most common source of confusion for people upgrading or reading older articles.
| Behavior | Tailwind v3 | Tailwind v4 |
|---|---|---|
| `@layer components` rules sorted before utilities | Yes | Yes, through native cascade layers |
| Unused component classes removed from the output | Yes | No, they are emitted as written |
| `hover:btn-primary` works in markup | Yes | No |
| `@apply btn-primary` inside another rule | Yes | No |
In v4, a class inside @layer components is ordinary CSS. Tailwind does not register it as a utility. Two consequences follow:
- You cannot write
hover:btn-primaryormd:cardin markup. Thehover:prefix only works for registered utilities. - You cannot pull one component class into another with
@apply. Writing@apply btn-primaryinside.btn-dangerraises an error.
If you need a class that works with variants or that other rules can apply, register it with @utility:
@import "tailwindcss";
@utility btn-base {
@apply rounded-md px-4 py-2 font-semibold;
}Now btn-base, hover:btn-base, and md:btn-base all work in markup. You can also write @apply btn-base inside other rules. Since @utility rules live in the utilities layer, other utilities can override them, but they also compete with utilities on equal footing. Use @utility for small single-purpose building blocks and @layer components for larger, named pieces such as a card.
A combined setup that avoids repeating the shared part:
@import "tailwindcss";
@utility btn-base {
@apply rounded-md px-4 py-2 font-semibold;
}
@layer components {
.btn-primary {
@apply btn-base bg-blue-600 text-white hover:bg-blue-700;
}
.btn-secondary {
@apply btn-base bg-gray-200 text-gray-900 hover:bg-gray-300;
}
}In v3 this setup worked by putting everything in @layer components. In v4, move the shared part into @utility.
Keeping the layer tidy
A few habits keep a component layer maintainable:
- Name classes by role, not by appearance.
.btn-primaryages better than.blue-button. - Keep each class short. If a class needs ten lines of
@apply, a framework component is probably a better fit (the next chapters compare them). - Use one file for component classes, such as
components.css, imported from your main stylesheet with@import. Rules in that file still need their own@layer componentswrapper. - Prefer theme variables over hard-coded values when a rule needs a raw CSS property. For example,
background-color: var(--color-blue-600);stays in sync with your theme.
Common errors
- A utility in markup has no effect. A matching rule is probably unlayered. Wrap it in
@layer components. hover:my-classdoes nothing in v4. The class is a plain component class. Convert it to@utility.@applyreports an unknown utility. In v4,@applyworks only with real utilities and variants, not with your own component classes.- A component class overrides a utility in an older v3 project. Check that you wrote
@layer componentsand that your CSS imports Tailwind's@tailwind components;directive in the correct place. - Your override works in one place but not another. Two classes in the same layer fall back to specificity and source order. A utility beats a component class, but two component classes compete normally.
Exercise
A project uses Tailwind v4. This CSS is in the main stylesheet: .card { @apply rounded-lg bg-white p-6; } The markup is <div class="card p-2">. The card still has 1.5rem of padding instead of 0.5rem. Explain why, and show the fix.
Show answer
The .card rule is not inside any layer, so it is unlayered. Unlayered rules beat every layered rule, and Tailwind's utilities, including p-2, are in the utilities layer. The p-6 from .card therefore wins. The fix is to wrap the rule in @layer components { ... }. Then p-2 from the later utilities layer overrides p-6, and the card gets 0.5rem of padding.
Exercise
In Tailwind v4, you want md:btn-base to work in markup, where btn-base is rounded-md px-4 py-2 font-semibold. Should you define it with @layer components or @utility, and why?
Show answer
Use @utility btn-base { @apply rounded-md px-4 py-2 font-semibold; }. In v4, classes inside @layer components are plain CSS and are not registered as utilities, so variants such as md: cannot be applied to them. @utility registers the class, which makes variants and @apply btn-base work.