Sub topic 14
Dark mode variants
This chapter assumes you can apply Tailwind color utilities, use hover and focus variants, and edit a Tailwind project's CSS or config. You should also know the default palette and shade numbers.
How the dark: variant works
The dark: prefix applies a utility only when dark mode is active. Without it, the utility always applies. You write the light style first and add the dark style as an override.
<div class="bg-white text-slate-900 dark:bg-slate-900 dark:text-slate-100">
Adapts to the color scheme
</div>You can stack dark: with other variants. The order is dark:hover:bg-slate-700, meaning "in dark mode, on hover".
Choosing a strategy
Tailwind decides what "dark mode is active" means. There are two common strategies.
| Strategy | Trigger | Use it when |
|---|---|---|
| Media query (default) | The operating system or browser prefers a dark color scheme | You want to follow the user's system setting with no JavaScript |
| Class or selector | A `dark` class (or another selector) on an ancestor such as `<html>` | You want a manual toggle, or a saved user choice |
The default media strategy needs no setup in either Tailwind v3 or v4. For a manual toggle, the configuration differs by version.
// tailwind.config.js (Tailwind CSS v3)
module.exports = {
darkMode: 'class',
content: ['./src/**/*.{html,js}'],
}/* app.css (Tailwind CSS v4) */
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));With either setup, adding class="dark" to the <html> element turns dark mode on for the whole page.
Dark mode for colors, borders, and shadows
Each property you style in light mode may need a dark counterpart. The card below changes its border, background, and text colors.
<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>Notes on the choices:
- The light card uses a pale border (
slate-200) and the dark card uses a darker one (slate-700), so the edge stays subtle in both. - Body text moves from
slate-600toslate-300. Dark mode usually needs lighter text shades, not pure white, to reduce glare. - Shadows are hard to see on dark backgrounds. If a shadow disappears, rely on the border and a slightly lighter background to separate the card from the page.
Common errors
- Toggle does nothing. You set a class on
<html>but never configured the class strategy, so the media query still controlsdark:. - Missing dark override. A white background with default dark text works in light mode. In dark mode, if you override only the background, the text can become unreadable.
- Focus rings vanish. An indigo ring that is clear on white can be weak on
slate-900. Testdark:focus:ring-indigo-400or a lighter shade.
Exercise
Your page has dark:bg-slate-900 on the body. The operating system is set to dark mode, but you want a button on the page to switch themes manually. What must you change, and what do you add to the page when the button is clicked?
Show answer
Switch from the default media strategy to the class (or selector) strategy: set darkMode: 'class' in the v3 config, or add @custom-variant dark (&:where(.dark, .dark *)); in v4. When the button is clicked, add the dark class to the <html> element (and remove it to return to light mode).