Sub topic 14
Dark mode variants
This chapter assumes you can already use Tailwind's color, border, and shadow utilities from the earlier chapters, and that you know how a variant prefix such as hover: works. Here you will learn how the dark: variant decides when it applies, how to switch that decision between the operating system setting and a manual toggle, and how to restyle colors, borders, rings, and shadows for dark surfaces.
What the dark: variant does
dark: is a conditional prefix. A class such as dark:bg-slate-800 has no effect in light mode. When dark mode is active, it overrides the unprefixed bg-* class. You always write the light style first, then add the dark replacement.
<div class="rounded-xl border border-slate-200 bg-white p-6 text-slate-900 shadow-md dark:border-slate-700 dark:bg-slate-800 dark:text-slate-100">
<h2 class="text-lg font-semibold">Project status</h2>
<p class="mt-2 text-slate-600 dark:text-slate-300">All checks passed.</p>
</div>Only the color-related properties change. The radius, padding, and shadow size stay the same in both modes. Each property you want to change needs its own dark: class. A single dark: does not flip the whole component.
Choosing how dark mode is detected
Tailwind supports two strategies. Which one you use decides whether the user can override their system setting.
| Strategy | Active when | User toggle possible |
|---|---|---|
| Media query (default) | The operating system or browser reports `prefers-color-scheme: dark` | No |
| Class or attribute selector | An ancestor such as `<html>` carries a class like `dark` | Yes |
The media strategy needs no setup. For a manual toggle, you have to configure the selector strategy. The syntax depends on your Tailwind version.
In Tailwind v3, set darkMode in the config file:
// tailwind.config.js (Tailwind CSS v3)
module.exports = {
darkMode: 'class',
content: ['./src/**/*.{html,js}'],
}Since v3.4.1, 'selector' is the preferred value and works the same way. You can also pass ['selector', '[data-theme="dark"]'] to use a data attribute.
In Tailwind v4, you redefine the dark variant in your CSS:
/* app.css (Tailwind CSS v4) */
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));The selector .dark, .dark * matches the element carrying the class and everything inside it. This is why the class can sit on <html>, or on any wrapper to make only part of a page dark.
Wiring a toggle
With the selector strategy, Tailwind only reacts to the class. Your own script has to add and remove it. The script below follows the saved choice if there is one, and the system setting otherwise.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Theme toggle</title>
<script>
const stored = localStorage.getItem("theme");
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
document.documentElement.classList.toggle("dark", stored ? stored === "dark" : prefersDark);
</script>
<link href="/app.css" rel="stylesheet" />
</head>
<body class="bg-white text-slate-900 dark:bg-slate-900 dark:text-slate-100">
<button
id="toggle"
class="m-6 rounded-md bg-slate-900 px-4 py-2 text-white hover:bg-slate-700 dark:bg-white dark:text-slate-900 dark:hover:bg-slate-200"
>
Toggle theme
</button>
<script>
document.getElementById("toggle").addEventListener("click", () => {
const isDark = document.documentElement.classList.toggle("dark");
localStorage.setItem("theme", isDark ? "dark" : "light");
});
</script>
</body>
</html>The first script sits in <head>, before the stylesheet, on purpose. If you set the class after the page renders, users with a dark preference see a flash of the light theme.
Combining dark with other variants
Variants stack from left to right, and each one adds a condition. dark:hover:bg-slate-700 means "when dark mode is active and the element is hovered".
<button class="rounded-md bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-500 dark:bg-indigo-500 dark:hover:bg-indigo-400">
Save changes
</button>Dark UIs usually need a hover step in the opposite direction from light UIs. On a light surface, hover often moves one step darker. On a dark surface, it often moves one step lighter. Pick the shades deliberately and don't mechanically reuse the light values.
The same stacking works with the opacity modifier and with focus rings:
<input
type="text"
class="rounded-md border border-slate-300 bg-white px-3 py-2 text-slate-900
focus:border-indigo-500 focus:outline-none focus:ring-2 focus:ring-indigo-500
dark:border-white/20 dark:bg-slate-900 dark:text-slate-100
dark:focus:border-indigo-400 dark:focus:ring-indigo-400"
/>Here dark:border-white/20 uses a translucent white border. It adapts to whatever background sits behind it. This is often easier to maintain than picking a specific slate shade for each surface.
Adjusting borders, dividers, and shadows
Colors are not the only things that need attention in dark mode.
- Borders and dividers. A
border-slate-200line is almost invisible on a dark background, and a bright border looks harsh. Typical dark replacements aredark:border-slate-700ordark:border-white/10. The same goes fordivide-*utilities: adddark:divide-slate-700next todivide-slate-200. - Shadows. A black shadow on a near-black background is barely visible, so elevation stops reading. Common fixes are to remove the shadow and rely on a border (
dark:shadow-none dark:border-slate-700), or to lighten the surface so it separates from the page (dark:bg-slate-800on adark:bg-slate-900page). - Rings. Pick a ring shade that has enough contrast against the dark surface.
ring-indigo-500on slate-900 is acceptable for many designs.dark:ring-indigo-400is clearer.
<ul class="divide-y divide-slate-200 rounded-lg border border-slate-200 dark:divide-slate-700 dark:border-slate-700">
<li class="p-3">Profile</li>
<li class="p-3">Billing</li>
<li class="p-3">Security</li>
</ul>Form controls and scrollbars
Browsers draw native controls such as scrollbars, checkboxes, and date pickers using the page's color-scheme. Your dark: classes do not change that. Set it with an arbitrary property so those controls match:
<html lang="en" class="dark [color-scheme:light] dark:[color-scheme:dark]">Reducing repetition with semantic colors
If every element needs two or three dark: classes, a semantic color layer can keep the markup shorter. In Tailwind v4, define theme variables and override them under .dark:
/* app.css (Tailwind CSS v4) */
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));
@theme {
--color-surface: #ffffff;
--color-surface-border: #e2e8f0;
--color-content: #0f172a;
}
.dark {
--color-surface: #1e293b;
--color-surface-border: #334155;
--color-content: #f1f5f9;
}The classes bg-surface, border-surface-border, and text-content now switch automatically. The trade-off is indirection. Anyone reading the HTML must look in the CSS to learn what bg-surface looks like. This pattern fits design systems with many components. For a few components, plain dark: classes are easier to follow.
Common errors
- Nothing changes when toggling in v3. The default is the media strategy, so the
darkclass is ignored until you setdarkMode: 'class'or'selector'. - Nothing changes in v4. The
@custom-variantline is missing, or the class is on an element that is not an ancestor of the styled element. - Classes missing from the build. Tailwind scans for complete class names. A string built like
"dark:bg-" + shadeis never generated. Write the full class name. - Light-only colors left behind. A
text-slate-600paragraph on a dark card is hard to read. Search each component for everytext-,bg-,border-,divide-, andring-class and decide whether it needs a dark counterpart. - Unchecked contrast. Contrast ratios change when you swap shades. Test body text against the 4.5:1 WCAG guideline and focus indicators against 3:1, in both modes.
Exercise
A page uses Tailwind v3 with the default configuration. You add dark:bg-slate-900 to the body and put class="dark" on <html>, but the background stays light on a laptop set to light mode. What is wrong, and how do you fix it?
Show answer
The default darkMode strategy is the media query, so dark: responds only to the system's prefers-color-scheme, and the dark class is ignored. Set darkMode: 'class' (or 'selector' in v3.4.1 and later) in tailwind.config.js. After rebuilding, the class on <html> activates the dark styles. In v4, the equivalent fix is adding @custom-variant dark (&:where(.dark, .dark *)); to your CSS.
Exercise
Write the classes for a card whose light style is bg-white, border-slate-200, and shadow-md. In dark mode it should have a slate-800 background, a slate-700 border, and no shadow. Why remove the shadow?
Show answer
bg-white border-slate-200 shadow-md dark:bg-slate-800 dark:border-slate-700 dark:shadow-none. A shadow is a darker area around the element, and on a dark page it has too little contrast to be seen. The border and the lighter slate-800 surface separate the card from the page instead.