Sub topic 12
Stacking variants: dark:hover:, md:hover:
This chapter assumes you know the hover: and focus: variants from earlier chapters, and that you have Tailwind set up with dark mode available.
How stacking works
A variant adds one condition to a utility. You can chain several variants, and the utility applies only when every condition is true. Read the prefixes from left to right as a sentence:
dark:hover:bg-slate-700means "in dark mode, while hovered, use this background".md:hover:shadow-mdmeans "at themdbreakpoint and wider, while hovered, add this shadow".
Each prefix is separated by a colon, and the utility comes last.
<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>The following table shows which classes apply in each situation.
| Situation | Background | Shadow |
|---|---|---|
| Light, not hovered | `bg-white` | none |
| Light, hovered | `hover:bg-slate-100` | none below `md` |
| Dark, not hovered | `dark:bg-slate-800` | none |
| Dark, hovered | `dark:hover:bg-slate-700` | none below `md` |
| Wide screen, hovered | as above | `md:hover:shadow-md` |
Why dark:hover: is needed
hover:bg-slate-100 has no condition about dark mode. In dark mode it still fires on hover, so the button would flash a light background on a dark page. Adding dark:hover:bg-slate-700 fixes this. Tailwind generates the more specific stacked rule so that it wins over the plain hover: rule when both match.
The rule of thumb: every hover: color you set on a themed element needs a matching dark:hover: color.
Mixing stacking with other variants
You can stack any variants, including group-hover: and peer-invalid:.
<a href="#" class="group block rounded-lg border p-4 dark:border-slate-700">
<h3 class="font-semibold text-slate-900 group-hover:text-indigo-600 dark:text-white dark:group-hover:text-indigo-400">
Read the guide
</h3>
</a>Here the heading turns indigo when the card is hovered, and a lighter indigo when the same happens in dark mode. The group class stays on the parent, and only the child's classes are stacked.
Common errors
- Forgetting the base style.
md:hover:shadow-mdadds a shadow only on hover atmdand up. It does not set any shadow at rest, so you rarely need a base class, but a color change always needs a starting color. - Misreading breakpoints.
md:means "atmdand wider", not "only atmd". To limit a style to a range, add a larger breakpoint that undoes it, or usemax-lg:. - Reading prefixes in the wrong order. Always read left to right and expect the last prefix before the utility to be the innermost condition. If a stacked class does nothing, check that each prefix is spelled correctly. An unknown prefix produces no CSS and no error.
- Over-stacking. Strings such as
dark:md:group-hover:focus-visible:...are hard to read. If a class list needs more than three prefixes, extract a component instead.
Exercise
A card has the classes bg-white hover:bg-slate-100 dark:bg-slate-900. In dark mode, hovering the card turns it light gray. Which class do you add to keep it dark on hover, and what does each prefix in it mean?
Show answer
Add dark:hover:bg-slate-800. The dark: prefix requires dark mode, and the hover: prefix requires the pointer to be over the element. The background applies only when both conditions are true, and it overrides the plain hover:bg-slate-100.
Exercise
What does md:hover:shadow-md do on a phone-sized screen?
Show answer
Nothing. The md: condition is false below the md breakpoint, so the shadow never applies, even when the element is hovered.