Sub topic 5
Combining dark: with hover:, responsive and other variants
This chapter assumes you know basic HTML and CSS, the Tailwind utility classes, and how variants such as hover: and md: work. It also assumes dark mode is already configured in your project, either through the default prefers-color-scheme behavior or through a class or selector strategy. The examples work with both Tailwind v3 and v4 unless a note says otherwise.
Why variants stack
A variant is a prefix that makes a utility apply only under some condition. hover:bg-gray-200 applies only while the pointer is over the element. dark:bg-gray-800 applies only when dark mode is active.
Tailwind lets you chain prefixes. Each prefix adds one more condition, and the utility applies only when all of them are true. This is how you describe a style such as "in dark mode, on hover, on medium screens and wider" without writing any CSS yourself.
<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 base style.hover:bg-gray-200applies on hover in light mode.dark:bg-gray-800applies in dark mode.dark:hover:bg-gray-700applies on hover in dark mode.
The last rule is the one that stacks. It applies only when dark mode is active and the pointer is over the button.
Why you need dark:hover: at all
A common mistake is to write only dark:bg-gray-800 and expect the light-mode hover style to keep working. It does not do what you want. In light mode the hover color is light, so in dark mode the button would turn light gray on hover, which is a jarring flash on a dark page.
Tailwind sorts its generated CSS so that variant rules override plain rules. In this example, hover:bg-gray-200 is a rule with higher precedence than dark:bg-gray-800 in some situations, depending on the Tailwind version and the order of its variants. Do not rely on that ordering. State each combination you care about explicitly. If a utility needs a different value in dark mode while hovered, give it a dark:hover: class.
Order of prefixes
Tailwind v3 applies stacked variants from the inside out, so the order of prefixes rarely changes what a class means for simple conditions. For example, dark:hover:bg-gray-700 and hover:dark:bg-gray-700 both describe "hover and dark" in v3.
Tailwind v4 changed this. Stacked variants are applied from left to right, which matches how you read the class. For most variants like dark: and hover: the result is the same. The order matters for variants that wrap or modify selectors in a way that depends on position, such as *: or group-hover: combined with pseudo-elements.
A safe habit that works in both versions is to put dark: first, then state variants such as hover:, and put the responsive prefix at the front only if you want a single reading order:
<a class="text-blue-600 md:dark:hover:text-blue-300">Read more</a>Pick one order and use it everywhere in your project. A consistent order makes class lists easier to scan and easier to search.
Combining dark: with responsive prefixes
Responsive prefixes such as sm:, md: and lg: are variants that apply a utility at a minimum screen width. They stack with dark: the same way.
<div class="bg-white p-4 dark:bg-gray-900 md:p-8 md:dark:bg-gray-800">
<p class="text-gray-700 dark:text-gray-200">
On small screens this panel has a gray-900 dark background.
From the md breakpoint up, the dark background becomes gray-800.
</p>
</div>Here is how the browser reads the classes:
| Class | When it applies |
|---|---|
| `bg-white` | Always, unless overridden |
| `dark:bg-gray-900` | Dark mode, any screen width |
| `md:p-8` | Any theme, 768px and wider |
| `md:dark:bg-gray-800` | Dark mode and 768px and wider |
Responsive and dark rules are separate conditions. A screen that is both wide and in dark mode matches dark:bg-gray-900 and md:dark:bg-gray-800. The second one wins because Tailwind emits the responsive variants later in the stylesheet than the plain dark: variant, so the more specific condition overrides the more general one.
Mobile first applies to dark mode too
Tailwind is mobile first. An unprefixed class is the baseline, and md: adds to or overrides it for wider screens. Read dark mode the same way: dark: replaces the light value in the dark theme, and a responsive prefix narrows where that replacement applies.
If you only want a dark override on large screens, write lg:dark:bg-gray-800. The dark theme on smaller screens then keeps the light-mode background, which is rarely what you want. In practice, most responsive dark styles are overrides of an existing dark style, not the first dark style on an element.
Other variants that combine with dark:
Any variant can be stacked. These are the ones you will use most often.
| Variant | Example | Meaning |
|---|---|---|
| `focus:` | `dark:focus:ring-blue-400` | Focus ring color in dark mode |
| `focus-visible:` | `dark:focus-visible:outline-white` | Keyboard focus outline in dark mode |
| `active:` | `dark:active:bg-gray-600` | Pressed state in dark mode |
| `disabled:` | `dark:disabled:text-gray-600` | Disabled controls in dark mode |
| `group-hover:` | `dark:group-hover:text-white` | Child reacts when a `group` parent is hovered |
| `peer-checked:` | `dark:peer-checked:bg-blue-500` | Element reacts to a checked sibling input |
| `placeholder:` | `dark:placeholder:text-gray-500` | Placeholder text in dark mode |
| `first:` and `last:` | `dark:first:border-t-0` | Position-based styling in dark mode |
A complete example with groups
This card changes its title color when the card is hovered. Both the light and dark colors are stated, so neither theme is left with a wrong hover color.
<a href="/docs" class="group block rounded-lg border border-gray-200 bg-white p-4 hover:border-blue-500 dark:border-gray-700 dark:bg-gray-900 dark:hover:border-blue-400">
<h3 class="font-semibold text-gray-900 group-hover:text-blue-600 dark:text-white dark:group-hover:text-blue-300">
Documentation
</h3>
<p class="mt-1 text-sm text-gray-600 dark:text-gray-400">
Guides and reference for every feature.
</p>
</a>The group class on the anchor marks it as the parent. group-hover: classes on children respond to the hover state of that parent. The dark versions follow the same pattern as dark:hover:.
Writing less: reduce repeated pairs
Stacked variants make class lists long. Each color decision often needs a light value, a dark value, a hover value for each, and sometimes a focus value for each. A few techniques keep this manageable.
Group related utilities with a component
If a pattern repeats, put it in a single component of your framework (a React component, a template partial, a web component) and keep the class string in one place. This is usually the best approach, because the long string lives in one file.
Move theme colors into CSS variables
When many elements use the same pair of colors, define the colors once as CSS variables and refer to them with arbitrary values. This is covered in detail in a later chapter, but the idea is short:
:root {
--surface: #ffffff;
--surface-hover: #f3f4f6;
}
.dark {
--surface: #111827;
--surface-hover: #1f2937;
}<div class="bg-[var(--surface)] hover:bg-[var(--surface-hover)]">Card</div>The element has no dark: classes at all. The variables change with the theme, and the hover: utility still works. Use dark: for one-off differences and variables for shared surface and text colors.
Use a custom variant for combined conditions
In Tailwind v4 you can declare your own variant in CSS. If your team often writes "dark and hover" or wants a named condition, a custom variant gives it a short name:
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));
@custom-variant dark-hover (&:where(.dark, .dark *):hover);<button class="bg-gray-100 dark-hover:bg-gray-700">Save</button>This is optional. The plain dark:hover: form is clear and needs no extra setup, so reach for a custom variant only when it removes real repetition.
Edge cases and common errors
Both variants must be generated
Tailwind builds only the classes it finds in your source files. If you build class names by joining strings, such as "dark:hover:bg-" + color, the scanner cannot see the full class and the CSS is never generated. Write complete class names, or map a value to a full string:
const styles = {
primary: "bg-blue-600 hover:bg-blue-700 dark:bg-blue-500 dark:hover:bg-blue-400",
neutral: "bg-gray-100 hover:bg-gray-200 dark:bg-gray-800 dark:hover:bg-gray-700",
};
function buttonClass(kind) {
return "rounded px-3 py-1 " + styles[kind];
}
console.log(buttonClass("primary"));Hover on touch screens
On touch devices, hover: styles may stay applied after a tap. In Tailwind v4, hover: applies only when the device supports hover, using the @media (hover: hover) query. In v3 it applies on any device. If a tapped button keeps its dark hover color on a phone in v3, that is the reason. Using active: for the pressed state avoids depending on hover for essential feedback.
Dark mode and `prefers-` variants
Variants based on media queries stack with dark: too. For example, motion-reduce: and contrast-more: can be combined: dark:contrast-more:text-white raises text contrast for users who ask for more contrast while in dark mode. This is a good way to support accessibility settings without changing the base design.
Conflicting utilities at the same level
If two classes set the same property under the same condition, the one that appears later in Tailwind's generated CSS wins, not the one later in your class attribute. Do not write dark:bg-gray-900 dark:bg-black on the same element. Remove the one you do not want.
Variant order inside the class list
The order of classes in the class attribute does not change which one wins. Only the order of prefixes within one class has meaning, and only in the cases described above.
Checking your work
- Switch your theme with your toggle, or with your system or browser developer tools, and test each state: default, hover, focus, active and disabled.
- Resize the window across your breakpoints in both themes.
- Tab through the page with the keyboard to see focus styles in dark mode.
- Inspect an element in the browser developer tools, open the Styles panel, and read which rules are active. A stacked variant produces one selector that contains all its conditions.
Summary of the rules
- Variants stack, and the utility applies only when every condition is true.
- State each combination you care about, such as
dark:hover:, explicitly. - Dark and responsive conditions are independent, and you can combine them freely.
- Use complete class names so Tailwind can generate the CSS.
- Move repeated color pairs into components or CSS variables.
Exercise
A link has the classes text-blue-600 hover:text-blue-800 dark:text-blue-300. In dark mode, what color does the link turn on hover, and what class would you add so that the hover color is readable on a dark background?
Show answer
In dark mode the hover rule hover:text-blue-800 still applies, because nothing overrides it for the combined dark and hover condition. The link would turn dark blue on a dark background, which is hard to read. Add dark:hover:text-blue-200 (or another light blue) so that the hover color is correct in dark mode.
Exercise
Write the classes for a panel that has a white background and p-4 padding in light mode, a gray-900 background in dark mode, and p-8 padding from the md breakpoint up in both themes.
Show answer
bg-white p-4 dark:bg-gray-900 md:p-8. The padding utility does not need dark: because the padding is the same in both themes. The responsive prefix and the dark prefix are independent conditions.
Exercise
Why does class={"dark:hover:bg-" + color} fail to produce a style, and how do you fix it?
Show answer
Tailwind scans source files for complete class names. A string built by concatenation never appears whole, so the CSS for it is not generated. Fix it by writing each full class name in the source, for example in an object that maps a name to a complete string such as "dark:hover:bg-gray-700".