Sub topic 5
Combining dark: with hover:, responsive and other variants
This chapter assumes you know basic HTML and CSS, Tailwind utility classes, and variants such as hover: and md:. It also assumes a project where dark: already works, using either the media strategy or a class or selector strategy.
Variants are prefixes that stack
A variant is a prefix that makes a utility apply only under some condition. dark: means "when dark mode is active". hover: means "when the pointer is over the element". You can put several prefixes in front of one utility, separated by colons, and the utility applies only when all of the conditions are true.
<button class="rounded bg-gray-100 px-3 py-1 hover:bg-gray-200 dark:bg-gray-800 dark:hover:bg-gray-700">
Save
</button>This button has four background rules:
bg-gray-100is the default.hover:bg-gray-200replaces it on hover in light mode.dark:bg-gray-800replaces it in dark mode.dark:hover:bg-gray-700replaces it only when dark mode is active and the pointer is over the button.
The last rule is needed because hover:bg-gray-200 would otherwise also apply in dark mode, and a light gray hover color on a dark page looks wrong. Whenever a light-mode state style would look wrong in dark mode, write a dark: version of it.
How the stacked variants resolve
Tailwind generates the CSS for stacked variants in a fixed order, so the order you write the classes in the HTML does not matter. A stacked rule is more specific than a single-variant rule, so dark:hover:bg-gray-700 wins over dark:bg-gray-800 when both match.
The diagram below shows which background applies for each combination of mode and pointer state.
Adding responsive variants
Responsive prefixes such as md: and lg: stack in the same way. Each prefix adds one more condition.
<div class="bg-white p-4 md:bg-gray-50 dark:bg-gray-900 md:dark:bg-gray-800">
<p class="text-gray-700 dark:text-gray-200">Panel content</p>
</div>On screens at the md breakpoint and wider, the panel uses bg-gray-50 in light mode and bg-gray-800 in dark mode. Below md, it uses bg-white or bg-gray-900.
The prefix order has one catch. In Tailwind v3, either md:dark: or dark:md: works. In Tailwind v4, stacked variants are applied from left to right, which can matter for variants that wrap a selector. For plain dark: and md: the result is the same, so pick one order and use it everywhere. Many teams put the breakpoint first, then dark:, then the state, for example md:dark:hover:bg-gray-700.
Other variants that combine well
dark: works with most variants.
| Variant | Example | Applies when |
|---|---|---|
| `focus:` | `dark:focus:ring-blue-400` | The element has focus in dark mode |
| `disabled:` | `dark:disabled:bg-gray-700` | The control is disabled in dark mode |
| `group-hover:` | `dark:group-hover:text-white` | A parent marked `group` is hovered in dark mode |
| `placeholder:` | `dark:placeholder:text-gray-500` | The placeholder text is shown in dark mode |
| `print:` | `print:bg-white` | The page is printed |
The print: row shows a useful pattern. If a visitor prints a page in dark mode, the ink-heavy background is a poor result. Adding print:bg-white print:text-black to the page wrapper forces a light result on paper.
<label class="group block rounded p-3 hover:bg-gray-100 dark:hover:bg-gray-800">
<span class="text-gray-700 group-hover:text-gray-900 dark:text-gray-300 dark:group-hover:text-white">
Email address
</span>
<input
type="email"
placeholder="[email protected]"
class="mt-1 block w-full rounded border border-gray-300 bg-white px-2 py-1
placeholder:text-gray-400
focus:border-blue-500
dark:border-gray-600 dark:bg-gray-900 dark:placeholder:text-gray-500 dark:focus:border-blue-400"
/>
</label>Keeping class lists manageable
Stacked variants make class lists long. Two habits help.
- Group classes by condition, with the base classes first, then
dark:classes, then responsive ones. Consistent order makes a missingdark:counterpart easier to spot. - When the same light and dark pair repeats across many elements, move the colors into CSS variables (covered in a later chapter) so one class works in both themes.
Exercise
A card has the classes bg-white hover:bg-gray-100. In dark mode it should be bg-gray-900, and bg-gray-800 when hovered. Which classes do you add, and what happens if you add only dark:bg-gray-900?
Show answer
Add dark:bg-gray-900 dark:hover:bg-gray-800. If you add only dark:bg-gray-900, then in dark mode the card is dark at rest, but hovering applies hover:bg-gray-100, so it flashes a light gray background. The dark:hover: class is needed to override the light hover style.
Exercise
Write the classes for a paragraph that is text-gray-700 in light mode and text-gray-200 in dark mode, and that becomes text-black in light mode and text-white in dark mode from the lg breakpoint upward.
Show answer
text-gray-700 dark:text-gray-200 lg:text-black lg:dark:text-white. The lg:dark: pair applies only when the screen is at least lg wide and dark mode is active.