Sub topic 8
Organizing component classes with @layer components
This chapter assumes you know basic CSS (classes, specificity, the cascade) and common Tailwind utilities. You should also have seen @apply in the previous chapter. Here you will see where component classes should live and why that placement matters.
Why the layer matters
A component class like .btn-primary competes with utility classes in your markup. Suppose you write <button class="btn-primary px-8">. You expect px-8 to beat the padding that .btn-primary sets. Whether it does depends on source order and specificity, unless you put the component class in a layer.
A cascade layer is a CSS feature that groups rules. Rules in a later layer win over rules in an earlier layer, whatever their specificity. Tailwind orders its layers like this:
Component classes belong in components, which sits below utilities. A utility in your markup can then override a component class without !important or specificity tricks.
Writing a component class
Wrap the class in @layer components and build it 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 markup like any other class:
<button class="btn-primary">Save</button>
<button class="btn-primary px-8">Publish</button>The second button gets px-8 instead of px-4. The utility layer comes after the component layer, so the utility wins.
What goes in the layer
Put reusable, named patterns here: buttons, cards, form fields, badges. Keep each class small and focused on one role. Do not put one-off page styles here. If a style appears once, a utility in the markup is simpler.
A rough naming guide:
- Name by role, such as
.cardor.btn-primary, not by appearance, such as.blue-box. - Write one base class per pattern, such as
.btn, and add modifiers separately. - Keep the class list short. A component class with 20 utilities is hard to read and hard to override.
Differences between Tailwind versions
Version 3 and version 4 treat @layer differently.
| Behavior | Tailwind v3 | Tailwind v4 |
|---|---|---|
| Setup | `@tailwind components;` marks where the layer goes | `@import "tailwindcss";` declares the layers |
| Layer type | Tailwind's own mechanism | Native CSS cascade layers |
| Unused classes | Removed from the output | Kept in the output |
| Variants on the class (`hover:btn`) | Work | Do not work; use `@utility` |
The last row causes the most trouble. In v4, md:btn-primary has no effect if .btn-primary is defined in @layer components. If you need variants on a custom class, define it with @utility instead. A later chapter section covers that.
Common errors
- Utility does not override the class. The class is probably outside
@layer components, so it wins over the utilities layer. @applyfails on an unknown class. The class name is misspelled, or it is a custom class that@applycannot reach in this version.- Variant prefix on a custom class does nothing. In v4, move the class to
@utility. - Class works in v3 but is missing in v4, or the reverse. Check which version you run. Look at how your stylesheet starts, either
@tailwinddirectives or@import "tailwindcss".
Exercise
You define .card in @layer components with @apply p-4. In markup you write <div class="card p-8">. Which padding applies, and why?
Show answer
p-8 applies. The utilities layer comes after the components layer, and a later layer wins regardless of specificity or source order. If .card were defined outside any layer, it would win instead.
Exercise
In Tailwind v4, you write <button class="md:btn-primary"> and .btn-primary is defined in @layer components. The style never applies on medium screens. What is the fix?
Show answer
Define the class with @utility btn-primary { ... } so that it works with variants such as md: and hover:.