Sub topic 12
Stacking variants: dark:hover:, md:hover:
This chapter assumes you know how a single variant prefix such as hover: or focus: works. It also assumes you have seen dark: and the breakpoint prefixes (sm:, md:, lg:) at least once, and that your project already has Tailwind CSS set up. Here you will combine those prefixes, predict when the result applies, and avoid the mistakes that stacking tends to cause.
What stacking means
A variant prefix makes a utility conditional. hover:bg-slate-100 means "apply this background only while the element is hovered." You can put more than one prefix in front of the same utility, separated by colons. The utility then applies only when every condition is true at once.
<button class="bg-white dark:bg-slate-800 dark:hover:bg-slate-700 md:hover:shadow-md">
Settings
</button>Read each class from left to right:
dark:hover:bg-slate-700applies when dark mode is active and the pointer is over the button.md:hover:shadow-mdapplies when the viewport is at least themdbreakpoint and the pointer is over the button.
Stacking is an AND. There is no OR syntax. If you want the same style under two different conditions, write two classes.
Reading a stacked class
Each prefix maps to one CSS condition. Stacking combines those conditions around the same declaration.
| Prefix | CSS condition it adds |
|---|---|
| `hover:` | The `:hover` pseudo-class on the element |
| `focus-visible:` | The `:focus-visible` pseudo-class |
| `md:` | A media query, `min-width: 48rem` (768px) by default |
| `dark:` | A media query `prefers-color-scheme: dark`, or a selector if you configured class-based dark mode |
| `disabled:` | The `:disabled` pseudo-class |
The class md:dark:hover:bg-slate-700 therefore means: inside the md media query, inside dark mode, when the element is hovered, set the background to slate-700.
Why dark:hover: is usually necessary
New users often write this and are surprised by the result:
<button class="bg-white hover:bg-slate-100 dark:bg-slate-800">
Settings
</button>In dark mode, hovering the button turns it light gray, not dark. Both rules match, so the browser picks the winner by CSS specificity. The :hover pseudo-class adds specificity, so hover:bg-slate-100 has higher specificity than dark:bg-slate-800, which only adds a media query. Media queries add no specificity. The hover rule wins whenever both match.
The fix is to say what hover should do in dark mode:
<button class="bg-white text-slate-900 hover:bg-slate-100 dark:bg-slate-800 dark:text-white dark:hover:bg-slate-700 md:hover:shadow-md">
Settings
</button>Now the dark and hover rules both have pseudo-class-level specificity. Tailwind also orders its generated CSS so that a more specific condition set comes later, so dark:hover: wins over hover: in dark mode.
Does the order of prefixes matter?
For most combinations, no. dark:hover:bg-slate-700 and hover:dark:bg-slate-700 select the same elements. md:hover: and hover:md: behave the same way. Pick one order and use it everywhere, so classes are easy to scan and search. A common convention is breakpoint, then dark, then state: md:dark:hover:.
Order does matter for variants that change which element is selected, because each prefix wraps the one after it. Tailwind v4 applies stacked variants from left to right. Tailwind v3 applied them from right to left. If you use variants such as *: (children) or group-hover: together with other variants, check your Tailwind version's documentation before assuming two orders are equivalent. If a stacked class has no effect, try reversing the order of its prefixes.
Common stacks in practice
Responsive hover
md:hover:shadow-md adds a hover shadow only on wider screens. Below md, nothing changes on hover. This is useful when a hover effect needs space, such as a card that lifts or a menu that expands.
Dark focus rings
Focus indicators also need to be visible in dark mode. A ring color that works on white can disappear on slate-900:
<a href="/docs" class="rounded px-2 py-1 text-indigo-700 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 dark:text-indigo-300 dark:focus-visible:outline-indigo-400">
Documentation
</a>The dark:focus-visible:outline-indigo-400 class replaces only the outline color in dark mode. The width and offset still come from the unprefixed focus classes.
Stacking with group and peer
Variants from earlier chapters stack the same way. dark:group-hover:text-indigo-300 applies when dark mode is on and a group ancestor is hovered. md:peer-invalid:visible shows a message only on wider screens, and only when the preceding peer input is invalid.
<a href="#" class="group block rounded-lg border border-slate-200 p-4 hover:bg-slate-50 dark:border-slate-700 dark:hover:bg-slate-800">
<h3 class="font-semibold text-slate-900 group-hover:text-indigo-600 dark:text-white dark:group-hover:text-indigo-300">
Read the guide
</h3>
</a>Disabled and hover together
A disabled button usually should not react to hover. A tempting approach is to add disabled: classes and rely on them to win over hover:, but both are pseudo-class rules with equal specificity, so the outcome depends on generated order. A more reliable option is to scope hover to enabled elements with the enabled: variant:
<button class="rounded bg-indigo-600 px-4 py-2 text-white enabled:hover:bg-indigo-700 disabled:cursor-not-allowed disabled:opacity-50">
Save
</button>Here the hover style cannot apply to a disabled button, so you don't depend on rule order.
Edge cases and failure modes
- Class-based dark mode. The default
dark:variant follows the operating system setting. If you want a manual toggle, configure the variant to use a class or data attribute. In Tailwind v4 you do this with@custom-variant dark (&:where(.dark, .dark *));in your CSS. In v3 you setdarkMode: "selector"in the config. Stacked classes such asdark:hover:keep working because they use whateverdark:is defined as. - Hover on touch screens. On touch devices, a tap can leave an element stuck in a hover state. Tailwind v4 wraps
hover:in a@media (hover: hover)query, so hover styles apply only when the primary input can hover. In v3 this is not the default. Whichever version you use, never make hover the only way to reach information. A later chapter covers touch devices in detail. - Dead classes. A stack with a condition that can never be true is valid CSS and does nothing. For example,
disabled:hover:on adivnever applies, because adivcannot be disabled. If a stacked class has no effect, test each prefix alone first. - Class length. Stacks such as
md:dark:hover:bg-slate-700make long class lists. If the same long list repeats, extract a component (a React, Vue, or template partial) instead of copying strings. Do not hide the stacks behind@applyjust to shorten the markup. - Missing base style. Variants only change a property. If you write
dark:hover:bg-slate-700but never set a base background, the element has nothing to transition from. Set the base, set each state, then addtransition-colorsto smooth the change.
A quick way to debug a stack
- Remove all prefixes except one and confirm that single condition works.
- Add prefixes back one at a time, in the order you intend to read them.
- In the browser's developer tools, open the Styles panel and check whether the rule appears under the media query you expect.
- If two rules match and the wrong one wins, add the stacked version of the losing state (for example
dark:hover:) rather than using!important.
Exercise
A card has bg-white hover:bg-slate-100 dark:bg-slate-900. In dark mode, hovering the card turns it light gray. Why does this happen, and which class would you add so that hover makes the dark card slightly lighter slate instead?
Show answer
The :hover pseudo-class gives hover:bg-slate-100 higher specificity than dark:bg-slate-900, whose dark condition is only a media query. When both match, the hover rule wins, even in dark mode. Add dark:hover:bg-slate-800. That rule applies only when dark mode is active and the card is hovered, and it takes priority over the plain hover: rule.
Exercise
What does md:dark:hover:underline do, and what happens on a 500px-wide viewport in dark mode when the element is hovered, assuming default breakpoints?
Show answer
It underlines the element only when the viewport is at least md (768px), dark mode is active, and the element is hovered. At 500px the md condition is false, so the class has no effect, even though dark mode and hover are both true.
Exercise
Write the classes for a link that is indigo-700 in light mode and indigo-300 in dark mode, turns indigo-900 on hover in light mode and indigo-100 on hover in dark mode, and shows a visible outline only for keyboard focus.
Show answer
text-indigo-700 hover:text-indigo-900 dark:text-indigo-300 dark:hover:text-indigo-100 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600. Adding dark:focus-visible:outline-indigo-400 keeps the outline visible on dark backgrounds.