Sub topic 7
Stacking breakpoints with hover, dark and other variants
This chapter assumes you know the sm, md, lg, xl and 2xl prefixes and why unprefixed utilities apply at every width. It also assumes you have used state variants such as hover: and focus: on their own. Here you will combine them. You will see how Tailwind reads a stacked class, which conditions it requires, and how to avoid the mistakes that appear once variants pile up.
A stacked class is a chain of conditions
A variant is a prefix that limits when a utility applies. md: limits it to viewports at least 48rem wide. hover: limits it to elements the pointer is over. You can put several prefixes on one utility, separated by colons. The utility then applies only when every condition is true.
<button class="bg-blue-600 md:hover:bg-blue-700">Save</button>This button is always bg-blue-600. The darker bg-blue-700 appears only when the viewport is at least md wide and the pointer is over the button. Below md, hovering changes nothing.
In CSS terms, the breakpoint becomes a media query and the state becomes a pseudo-class selector. Tailwind produces output like this for the class above:
@media (min-width: 48rem) {
.md\:hover\:bg-blue-700:hover {
background-color: var(--color-blue-700);
}
}The exact variable name differs between versions, but the structure is the same. The media query wraps the rule, and the pseudo-class sits on the selector.
Which variants you can stack
Almost any variant can be combined with any other. The table lists the ones you will use most often.
| Variant | Condition it adds | Example |
|---|---|---|
| `md:` and other breakpoints | Viewport width is at least the breakpoint | `lg:p-8` |
| `max-md:` | Viewport width is below the breakpoint | `max-md:hidden` |
| `hover:` | Pointer is over the element | `hover:underline` |
| `focus:` and `focus-visible:` | Element has focus | `focus:ring-2` |
| `disabled:` | Form control is disabled | `disabled:opacity-50` |
| `first:` and `last:` | Element is the first or last child | `last:border-0` |
| `dark:` | Dark color scheme is active | `dark:bg-gray-900` |
| `motion-reduce:` | User asked for less motion | `motion-reduce:transition-none` |
| `print:` | Page is being printed | `print:hidden` |
| `group-hover:` | A marked ancestor is hovered | `group-hover:opacity-100` |
| `peer-checked:` | A marked earlier sibling is checked | `peer-checked:bg-blue-600` |
You can chain as many as you need, for example dark:md:hover:bg-gray-700. Long chains are legal, but each prefix makes the class harder to read, so stop when the intent is clear.
Breakpoints with state variants
The most common stack is a breakpoint plus an interaction state. A typical use is to enable a hover effect only on larger screens, where a mouse is likely:
<a
href="/docs"
class="rounded px-4 py-2 text-gray-700 md:hover:bg-gray-100 md:focus-visible:ring-2"
>
Docs
</a>Another use is to change a state style at a breakpoint while keeping the unprefixed state style for small screens:
<button class="bg-blue-600 hover:bg-blue-700 lg:hover:bg-blue-800">Save</button>Below lg, hovering gives blue-700. From lg upward it gives blue-800. Both rules have the same specificity, and Tailwind emits breakpoint rules after the unprefixed ones. The lg: rule therefore wins when both match.
Order inside the stack
For the combinations in this chapter, md:hover:bg-blue-700 and hover:md:bg-blue-700 select the same elements, so either works. Pick one order and use it consistently. A common convention is to put the breakpoint first, then the color scheme, then the state: md:dark:hover:....
Order starts to matter for variants that change the selector more deeply, such as *: (which targets children). Tailwind v4 reads stacked variants from left to right. Tailwind v3 read them from right to left. If you upgrade a project and a child-targeting stack changes behavior, this is the first place to look.
Breakpoints with dark mode
By default, dark: uses the prefers-color-scheme: dark media query. It follows the operating system setting. It stacks with breakpoints the same way as any other variant:
<article class="rounded border border-gray-200 bg-white p-4 text-gray-900 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-100 md:p-8 dark:md:hover:bg-gray-700">
<h2 class="text-lg font-semibold md:text-2xl">Release notes</h2>
<p class="mt-2 text-sm md:text-base">Everything that changed this week.</p>
</article>This card has three layers. Padding and text size respond to the width. Colors respond to the color scheme. The hover background applies only when the scheme is dark and the viewport is at least md.
Many sites let users toggle a theme with a button. That needs class-based dark mode, where dark: applies when an ancestor (usually <html>) has a dark class. Configure it once.
In Tailwind v4, declare a custom variant in your CSS:
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));In Tailwind v3, set darkMode in the config file:
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ['./src/**/*.{html,js}'],
darkMode: 'class',
};After that, the same stacked classes work. The only change is what triggers dark:. Breakpoints still depend on viewport width, and they are independent of how dark mode is switched.
Breakpoints with group and peer variants
group-hover: lets a child react when a marked ancestor is hovered. Add group to the ancestor and group-hover: to the child. You can put a breakpoint in front:
<a href="/pricing" class="group block rounded border p-4">
<h3 class="font-semibold">Pricing</h3>
<p class="opacity-100 md:opacity-0 md:group-hover:opacity-100">
Plans start at $5 per month.
</p>
</a>On narrow screens the description is always visible, because hover is unreliable on touch devices and hiding text behind it would hide content from many users. From md upward the text is hidden until the card is hovered. This is the safer shape for hover-reveal patterns. Show the content by default, and hide it only at widths where hover is likely to exist.
peer-* variants work the same way for siblings. The peer must come before the element that reacts to it in the HTML, because CSS sibling selectors only look forward.
Ordering and specificity
Two ideas explain most surprises with stacked variants.
- A media query adds no specificity.
md:bg-blue-600is one class. It wins overbg-red-500only because the generated rule comes later in the stylesheet, and Tailwind sorts breakpoints after unprefixed utilities. - A pseudo-class adds specificity.
hover:bg-red-500is a class plus:hover, so it beats a plain class no matter where either rule appears.
Combine them and you get a trap:
<button class="bg-gray-200 hover:bg-red-500 md:bg-blue-600">Delete</button>At md and wider, the button is blue. When you hover it, it turns red, even though md:bg-blue-600 should seem to "override" everything at that width. The hover rule is more specific, so it wins whenever its condition is true. If you want the hover color to change at md, write the stack: md:hover:bg-blue-700.
Hover on touch screens
Touch browsers often apply :hover after a tap and leave it stuck until you tap elsewhere. Tailwind v4 wraps hover: in @media (hover: hover), so it only applies on devices whose primary input can really hover. Tailwind v3 does not do this by default. It has an opt-in setting, future: { hoverOnlyWhenSupported: true }, in the config file.
Do not use md:hover: as a substitute for this check. Width is a poor proxy for input type. A large tablet is wider than md and has no hover, and a small laptop window can be narrower than md and still have a mouse. Treat the breakpoint as a layout decision, and treat hover support as a separate capability.
Common mistakes
- Building class names from strings. Tailwind finds classes by scanning your source for complete strings. A template such as `
md:hover:bg-${color}-700` never appears whole, so the CSS is not generated. Write the full class names, or map each value to a complete string:
const hoverColor = {
blue: 'md:hover:bg-blue-700',
red: 'md:hover:bg-red-700',
};
function buttonClass(color) {
return `bg-gray-200 ${hoverColor[color]}`;
}- Expecting a prefix to carry over.
md:hover:bg-blue-700 underlinedoes not makeunderlineresponsive. A prefix belongs to one utility. If three utilities must change atmd, each needsmd:. - Stacking unreachable conditions.
md:max-md:flexrequires a width that is both at least 48rem and below 48rem. Nothing matches, and Tailwind produces no visible error. Check range stacks against the actual numbers. - Forgetting the dark trigger. If
dark:classes do nothing after you add a toggle button, the project is still using the default media query. Add the class-based configuration shown earlier. - Hiding essential content behind hover. Hover states, even at a breakpoint, are an enhancement. Keyboard and touch users should still reach the same content, so pair
hover:withfocus-visible:where an action is involved.
Exercises
Exercise
A link has the classes text-gray-700 lg:hover:text-blue-600. A visitor on a 700px-wide window moves the mouse over the link. What color is the text, and why?
Show answer
The text stays gray (text-gray-700). The lg:hover: stack requires both a viewport of at least 1024px and a hovered element. At 700px wide the lg condition is false, so the hover color never applies.
Exercise
What does this button look like at 1200px wide when hovered, and what single change makes the hover color blue at that width? The class list is bg-gray-200 hover:bg-red-500 md:bg-blue-600.
Show answer
It is red when hovered, because hover:bg-red-500 has higher specificity than md:bg-blue-600. Change the last class to md:hover:bg-blue-600, which applies the blue background only for hover at md and wider.
Exercise
Write a class list for a div that has a white background and a dark gray text by default, switches to gray-900 background and light text in dark mode, and shows a gray-800 background on hover only when dark mode is active and the viewport is at least lg.
Show answer
bg-white text-gray-900 dark:bg-gray-900 dark:text-gray-100 lg:dark:hover:bg-gray-800. The final class carries three conditions: the lg breakpoint, the dark scheme, and hover. Writing dark:lg:hover:bg-gray-800 is equivalent.