Sub topic 9
Hover, focus, and responsive variants in @apply
This chapter assumes you know the basic @apply directive and @layer components from the previous chapters, and that you have used variants such as hover: and md: in markup. Here you will see how variants behave when you move them into CSS.
Variants work inside @apply
@apply accepts the same class names you write in HTML, including variant prefixes. You can write hover:bg-blue-700 inside @apply and Tailwind generates the matching selector and media query.
@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 focus-visible:outline-blue-600;
@apply md:px-6;
}
}The compiled output contains a base rule, a :hover rule, a :focus-visible rule, and a rule inside a @media block for the md breakpoint. You do not write any of those selectors yourself.
Two ways to write a hover state
The spine's earlier example wrote the hover state as a separate rule. Both forms are valid.
@layer components {
/* Form 1: variants inside @apply */
.btn-a {
@apply bg-blue-600 text-white hover:bg-blue-700;
}
/* Form 2: a separate selector */
.btn-b {
@apply bg-blue-600 text-white;
}
.btn-b:hover {
@apply bg-blue-700;
}
}Form 1 keeps everything in one place. Form 2 is useful when the selector is more complex than a variant can express, such as .btn-b:not(:disabled):hover.
Stacking variants
Variants can be combined, in the same order you would use in markup.
@layer components {
.card {
@apply rounded-lg bg-white p-4 shadow;
@apply md:p-6 lg:hover:shadow-lg dark:bg-gray-800;
}
}Here lg:hover:shadow-lg applies the shadow only on large screens while hovering. dark:bg-gray-800 applies when the dark variant is active.
Common errors
- Variants on the component class in markup. In Tailwind v4,
hover:btn-primaryonly works ifbtn-primaryis registered with@utility. A class defined in@layer componentscannot take variants. This is the difference between the two mechanisms. - Using a class that does not exist.
@apply hover:bg-brandfails ifbg-brandis not defined in your theme. The build reports an unknown utility. - Applying one component class inside another with variants. Nesting component classes through
@applyis fragile. Compose from utilities instead. - Forgetting
@referencein separate stylesheets. A Vue or Svelte<style>block cannot resolve variants until it imports the theme with@reference.
Variants on a custom utility
If you need hover: or md: in markup, define the class with @utility.
@import "tailwindcss";
@utility btn-base {
@apply rounded-md px-4 py-2 font-semibold;
}<button class="btn-base bg-blue-600 text-white md:px-6 hover:bg-blue-700">Save</button>You can also write md:btn-base or hover:btn-base, because @utility classes take part in the variant system.
Exercise
You wrote .alert { @apply p-3 md:p-6; } inside @layer components. A teammate writes <div class="md:alert"> and nothing happens. Why, and how can you get that markup to work?
Show answer
Classes defined in @layer components are plain CSS classes and cannot be prefixed with variants in markup. Either keep using alert alone, since its md:p-6 already handles the breakpoint, or register it with @utility alert { ... } so that md:alert is generated.
Exercise
Rewrite .link { @apply text-blue-600; } .link:hover { @apply text-blue-800; } so the hover state is inside one @apply.
Show answer
.link { @apply text-blue-600 hover:text-blue-800; }