Sub topic 9
Hover, focus, and responsive variants in @apply
This chapter assumes you can write a basic @apply rule and know where @layer components fits. If you can't yet, read the previous two chapters first. Examples target Tailwind CSS v4. Where v3 differs, the text says so.
A component class is only useful if it behaves like the markup it replaces. A button needs a hover color, a visible focus ring, and sizes that change at breakpoints. This chapter covers the two ways to express those states in a class built with @apply, and what happens when they interact with utilities in your markup.
Two ways to write a state
Tailwind variants are prefixes such as hover:, focus-visible:, md:, and dark:. You can use them inside @apply exactly as you would in a class attribute:
@import "tailwindcss";
@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-offset-2 focus-visible:outline-blue-600
disabled:opacity-50
md:px-6;
}
}The other way is to write the selector yourself and put a plain @apply inside it:
@layer components {
.btn-primary {
@apply rounded-md bg-blue-600 px-4 py-2 font-semibold text-white;
&:hover {
@apply bg-blue-700;
}
&:focus-visible {
@apply outline-2 outline-offset-2 outline-blue-600;
}
}
}Both produce a working button. The earlier chapter's .btn-primary:hover { @apply bg-blue-700; } is the same idea without nesting.
| Approach | Strength | Weakness |
|---|---|---|
| Variant prefix inside `@apply` | One declaration holds everything. Prefixes match what you already write in markup. | A long `@apply` line is hard to scan. |
| Hand-written selector with a plain `@apply` | Grouped by state, so it is easy to read. Lets you add custom CSS beside the utilities. | You must know the selector. It misses variants that have no simple selector, such as `md:` or `dark:`. |
The variant prefix form is the better default. It also uses Tailwind's own definition of each state, which matters more than it first appears.
What the compiler produces
A variant prefix is not just a pseudo-class. In Tailwind v4, hover: wraps the declaration in a media query so hover styles apply only on devices that can actually hover. The compiled output for hover:bg-blue-700 looks roughly like this:
.btn-primary {
@media (hover: hover) {
&:hover {
background-color: var(--color-blue-700);
}
}
}If you hand-write &:hover, you skip that guard. On touch screens the hover style can then stick after a tap. Using the prefix inside @apply gives you Tailwind's behavior, so the class matches what hover:bg-blue-700 does on an element.
Responsive prefixes compile to media queries based on your theme breakpoints. md:px-6 becomes a rule inside @media (width >= 48rem). A hand-written selector has no equivalent, so use the prefix form for responsive rules.
Mermaid note: the diagram below shows the full path.
Stacking variants
Variants combine in @apply the same way they do in markup. Each prefix wraps the rule once more:
@layer components {
.nav-link {
@apply px-3 py-2 text-gray-700
hover:text-gray-900
md:px-4 md:hover:bg-gray-100
dark:text-gray-300 dark:hover:text-white;
}
}Here md:hover:bg-gray-100 applies a background only at the md breakpoint and above, and only while hovering. dark:hover:text-white applies only when the dark variant is active. The dark: variant follows your configuration: by default it uses the prefers-color-scheme media query.
Parent-driven variants: group and peer
Variants that depend on another element also work, as long as the markup supplies the other element. A card class can react to its own hover by marking the card as a group:
@layer components {
.card {
@apply group rounded-lg border border-gray-200 p-4;
}
.card-title {
@apply text-lg font-semibold text-gray-900 group-hover:text-blue-700;
}
}<article class="card">
<h2 class="card-title">Quarterly report</h2>
</article>Hovering anywhere on the article turns the title blue. Note that group itself is a utility class applied through @apply on .card. If you forget it, group-hover: never matches, and nothing warns you.
Variants and custom utilities
The @layer components rule has a limit in v4. Classes you write there are plain CSS. They cannot take variants in markup, so md:btn-primary does nothing. They also cannot be passed to @apply from another rule.
If you need either, register the class with @utility:
@import "tailwindcss";
@utility btn-base {
@apply rounded-md px-4 py-2 font-semibold;
}Now hover:btn-base and md:btn-base work in markup, and @apply btn-base works in other rules. In v3 the equivalent was @layer utilities or @layer components with @apply. If you are upgrading, move any class that you apply elsewhere or prefix with a variant to @utility.
<button class="btn-base bg-blue-600 text-white md:px-8">Save</button>Because a @utility class is a utility, it takes part in the normal utility ordering. Be careful with this: a px-4 inside btn-base and a md:px-8 in markup do not conflict in this example, because the variant is more specific in the generated order.
Interaction with utilities in markup
Tailwind v4 emits its CSS in native cascade layers, ordered theme, base, components, utilities. A rule in a later layer beats a rule in an earlier layer regardless of selector specificity. This has two consequences.
First, a utility in markup overrides your component class, which is usually what you want:
<button class="btn-primary bg-emerald-600">Approve</button>The base background becomes emerald. But the hover rule inside .btn-primary is also in the components layer, and the bg-emerald-600 utility in the utilities layer beats it in every state. The button stays emerald on hover, because your hover:bg-blue-700 never wins.
To override both states, repeat the variant in markup:
<button class="btn-primary bg-emerald-600 hover:bg-emerald-700">Approve</button>Second, the override is only dependable when the utility is in a later layer. Conflicts inside the same layer fall back to specificity and source order, which is harder to reason about. If you build components in React, twMerge from the earlier chapter is the better tool for resolving these conflicts. For @apply classes, you accept the layer ordering and write overrides deliberately.
Specific failure modes
These are the mistakes that most often show up when variants meet @apply.
- Unknown utility error.
@apply hover:foo-barfails the build iffoo-baris not a real utility. The error names the utility, not the variant. - Wrong state for the job.
focus:fires on mouse clicks as well as keyboard focus. For a visible ring on buttons,focus-visible:is usually correct. - Missing parent class.
group-hover:andpeer-checked:needgrouporpeerin the markup. They compile without error and then never match. - Breakpoint on the wrong element.
md:in a component class responds to the viewport. If the component should respond to its container, use a container query variant like@md:and mark the parent with@container. - Hand-written
:hoveron touch devices. As shown earlier, it skips thehover: hoverguard. - Important modifier. To force a declaration, put
!at the end of a utility in v4:@apply bg-blue-700!. Use this sparingly, because it fights the layer ordering instead of working with it.
A worked component
This final example combines the pieces above into one button with a link-style secondary form.
@import "tailwindcss";
@utility btn-base {
@apply inline-flex items-center justify-center rounded-md px-4 py-2 font-semibold
focus-visible:outline-2 focus-visible:outline-offset-2
disabled:cursor-not-allowed disabled:opacity-50
md:px-6;
}
@layer components {
.btn-primary {
@apply btn-base bg-blue-600 text-white
hover:bg-blue-700
focus-visible:outline-blue-600
dark:bg-blue-500 dark:hover:bg-blue-400;
}
.btn-secondary {
@apply btn-base bg-gray-200 text-gray-900
hover:bg-gray-300
focus-visible:outline-gray-500;
}
}btn-base holds the shared shape and states. Each variant adds only colors. The shared classes are written once, and the focus ring and disabled style cannot drift between the two buttons.
Exercise
A teammate writes .btn-primary { @apply bg-blue-600 hover:bg-blue-700; } in @layer components. In markup they use <button class="btn-primary bg-red-600">. The button turns red, but it stays red on hover instead of darkening. Why does this happen, and how do you fix it?
Show answer
The bg-red-600 utility sits in the utilities layer, which beats the components layer regardless of specificity. It therefore overrides both the base and the hover rule from .btn-primary. Add a matching variant in markup so the override covers the hover state too: <button class="btn-primary bg-red-600 hover:bg-red-700">.
Exercise
You want md:btn-base to work in markup. Can you define btn-base as a plain class in @layer components? If not, what do you use in Tailwind v4?
Show answer
No. Plain classes in @layer components are ordinary CSS and cannot take variant prefixes in markup. Define it with @utility btn-base { ... }, which registers a real utility that accepts variants like md: and hover: and can also be used with @apply.