Sub topic 7
Stacking breakpoints with hover, dark and other variants
This chapter assumes you know the default breakpoints (sm, md, lg, xl, 2xl) and the mobile-first rule: an unprefixed utility applies at every width, and a prefixed one applies from that width upward. You should also know what a CSS media query is.
Variants are conditions
A prefix such as md:, hover:, dark: or focus: is a variant. A variant wraps a utility in a condition. md: means "the viewport is at least 768px wide". hover: means "the pointer is over this element".
You can chain variants. The utility then applies only when every condition is true at the same time.
<button class="bg-blue-600 md:hover:bg-blue-700">Save</button>The button is bg-blue-600 at every width. The darker bg-blue-700 appears only when the viewport is at least md and the pointer is over the button. On a narrow screen, hovering does nothing.
Reading a stacked class
Read the class from left to right as a list of conditions, then the utility.
| Class | Applies when |
|---|---|
| `md:hover:bg-blue-700` | viewport is at least `md` and the element is hovered |
| `dark:lg:text-white` | dark mode is active and the viewport is at least `lg` |
| `lg:focus:ring-2` | viewport is at least `lg` and the element has focus |
| `md:max-lg:hidden` | viewport is from `md` up to, but not including, `lg` |
Combining with dark mode
Dark mode is another variant, so it stacks with breakpoints in the same way. This heading changes its color for dark mode and also changes its size on wider screens:
<h2 class="text-lg text-slate-900 md:text-2xl dark:text-slate-100">
Dashboard
</h2>You can also make a dark-mode rule depend on width:
<div class="bg-white dark:bg-slate-900 lg:dark:bg-slate-800">
Panel
</div>Here the panel is bg-slate-900 in dark mode on small screens. At lg and wider in dark mode, it becomes bg-slate-800. In light mode, nothing changes with width.
Other variants that stack
Any variant can join the chain. Common combinations:
md:focus:ring-2adds a focus ring only on wider screens.lg:group-hover:opacity-100reacts to a hovered parent marked withgroup, only fromlgupward.sm:disabled:opacity-50dims disabled controls fromsmupward.md:first:mt-0removes the top margin of the first child frommdupward.
<a href="/docs" class="group block p-4">
<span class="opacity-100 lg:opacity-60 lg:group-hover:opacity-100">
Read the docs
</span>
</a>On small screens the text is always fully visible. From lg upward it is dimmed until the parent link is hovered.
Ranges are stacked too
The max-* variants are breakpoint variants, so they stack with the min-width ones. Stacking md: with max-lg: gives a width range:
<div class="md:max-lg:hidden">
Hidden from md up to, but not including, lg
</div>You can add a state to a range as well, for example md:max-lg:hover:underline.
Pitfalls
- Repeating the base style.
md:hover:bg-blue-700does not set a background belowmd. Put the base color (bg-blue-600) on the unprefixed class. If you forget it, the element has no background at narrow widths. - Hover on touch screens. Touch devices have no real hover. Do not hide essential content behind
hover:. Tailwind v4 applieshover:only on devices that support hover. Version 3 does this only if you enable thehoverOnlyWhenSupportedoption. - Order of variants. For a breakpoint plus a state, write the breakpoint first (
md:hover:). It is the convention and it reads in the order the conditions are checked. Some stacks, such as combining with*:orgroup-*, can behave differently between v3 and v4. If a stacked class does nothing, check the generated CSS in your browser's developer tools. - Too much stacking. A class like
sm:dark:hover:focus:md:...is hard to read. If an element needs many conditions, extract it into a component.
Exercise
A card must have a white background, turn light gray on hover only on viewports at least lg wide, and use bg-slate-800 in dark mode at every width. Write the class list.
Show answer
bg-white dark:bg-slate-800 lg:hover:bg-gray-100. The white background is the base. dark:bg-slate-800 handles dark mode at all widths. lg:hover:bg-gray-100 stacks the breakpoint and the hover state, so the hover color appears only from lg upward. In dark mode, if you also want a hover color, add lg:dark:hover:bg-slate-700.
I need to fix one thing: I left an empty mermaid fence before the real diagram. Corrected chapter follows.
Stacking breakpoints with hover, dark and other variants
This chapter assumes you know the default breakpoints (sm, md, lg, xl, 2xl) and the mobile-first rule: an unprefixed utility applies at every width, and a prefixed one applies from that width upward. You should also know what a CSS media query is.
Variants are conditions
A prefix such as md:, hover:, dark: or focus: is a variant. A variant wraps a utility in a condition. md: means "the viewport is at least 768px wide". hover: means "the pointer is over this element".
You can chain variants. The utility then applies only when every condition is true at the same time.
<button class="bg-blue-600 md:hover:bg-blue-700">Save</button>The button is bg-blue-600 at every width. The darker bg-blue-700 appears only when the viewport is at least md and the pointer is over the button. On a narrow screen, hovering does nothing.
Reading a stacked class
Read the class from left to right as a list of conditions, then the utility.
| Class | Applies when |
|---|---|
| `md:hover:bg-blue-700` | viewport is at least `md` and the element is hovered |
| `dark:lg:text-white` | dark mode is active and the viewport is at least `lg` |
| `lg:focus:ring-2` | viewport is at least `lg` and the element has focus |
| `md:max-lg:hidden` | viewport is from `md` up to, but not including, `lg` |
Diagram: Flowchart showing that md:hover:bg-blue-700 applies only when the viewport is at least md and the element is hovered. Otherwise the base bg-blue-600 stays.
Combining with dark mode
Dark mode is another variant, so it stacks with breakpoints in the same way. This heading changes its color for dark mode and its size on wider screens:
<h2 class="text-lg text-slate-900 md:text-2xl dark:text-slate-100">
Dashboard
</h2>You can also make a dark-mode rule depend on width:
<div class="bg-white dark:bg-slate-900 lg:dark:bg-slate-800">
Panel
</div>The panel is bg-slate-900 in dark mode on small screens. At lg and wider in dark mode, it becomes bg-slate-800. In light mode, nothing changes with width.
Other variants that stack
Any variant can join the chain. Common combinations:
md:focus:ring-2adds a focus ring only on wider screens.lg:group-hover:opacity-100reacts to a hovered parent marked withgroup, only fromlgupward.sm:disabled:opacity-50dims disabled controls fromsmupward.md:first:mt-0removes the top margin of the first child frommdupward.
<a href="/docs" class="group block p-4">
<span class="opacity-100 lg:opacity-60 lg:group-hover:opacity-100">
Read the docs
</span>
</a>On small screens the text is always fully visible. From lg upward it is dimmed until the parent link is hovered.
Ranges are stacked too
The max-* variants are breakpoint variants, so they stack with the min-width ones. Stacking md: with max-lg: gives a width range:
<div class="md:max-lg:hidden">
Hidden from md up to, but not including, lg
</div>You can add a state to a range as well, for example md:max-lg:hover:underline.
Pitfalls
- Missing base style.
md:hover:bg-blue-700sets nothing belowmd. Put the base color (bg-blue-600) on the unprefixed class, or the element has no background at narrow widths. - Hover on touch screens. Touch devices have no real hover, so do not hide essential content behind
hover:. - Variant order. For a breakpoint plus a state, write the breakpoint first (
md:hover:). Some combinations, such as those with*:orgroup-*, can be ordered differently between v3 and v4. If a stacked class does nothing, check the generated CSS in your browser's developer tools. - Too much stacking. A class like
sm:dark:hover:focus:...is hard to read. If an element needs many conditions, extract it into a component.
Exercise
A card must have a white background, turn light gray on hover only on viewports at least lg wide, and use bg-slate-800 in dark mode at every width. Write the class list.
Show answer
bg-white dark:bg-slate-800 lg:hover:bg-gray-100. The white background is the base. dark:bg-slate-800 handles dark mode at all widths. lg:hover:bg-gray-100 stacks the breakpoint and the hover state, so the hover color appears only from lg upward. If dark mode also needs a hover color, add lg:dark:hover:bg-slate-700.