Sub topic 10
Dark mode with the dark variant
This chapter assumes you know the Tailwind basics from earlier chapters: utility classes, color utilities, and variant prefixes such as hover: and md:. You should also know basic CSS, including media queries.
How the dark variant works
The dark: prefix works like any other variant. A utility with the prefix applies only when dark mode is active. Without the prefix, the utility applies all the time.
<div class="bg-white text-slate-900 dark:bg-slate-900 dark:text-white">
Adapts to the dark color scheme.
</div>In light mode this element has a white background and dark text. In dark mode, the dark: utilities override the base ones, so you get a dark background and white text.
You write the light style first, then add the dark override. This follows the same pattern as the mobile-first breakpoints in the previous chapters: the unprefixed class is the default, and the prefixed class changes it under a condition.
What "dark mode is active" means
By default, Tailwind ties dark: to the prefers-color-scheme media query. This query reflects the operating system or browser setting. If the visitor's system is set to dark, the dark: utilities apply. No JavaScript is needed.
This default is simple, but the visitor cannot choose a theme on your site that differs from their system setting. If you want a theme toggle, you need to change how the variant is triggered.
Using a manual toggle
In Tailwind v4, you can redefine the dark variant in your CSS with @custom-variant. The following example makes dark: apply when an ancestor has the dark class:
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));Now you control dark mode by adding or removing the dark class, usually on the <html> element. A small script can do this:
<button id="theme-toggle" class="rounded-lg bg-slate-200 px-4 py-2 dark:bg-slate-700 dark:text-white">
Toggle theme
</button>
<script>
const button = document.getElementById('theme-toggle');
button.addEventListener('click', () => {
const isDark = document.documentElement.classList.toggle('dark');
localStorage.setItem('theme', isDark ? 'dark' : 'light');
});
</script>The script saves the choice in localStorage so it can be restored on the next visit.
Combining dark with other variants
Variants stack. You can combine dark: with state and breakpoint prefixes:
<button class="bg-indigo-600 hover:bg-indigo-700 dark:bg-indigo-400 dark:hover:bg-indigo-300 md:px-6">
Save
</button>Here dark:hover:bg-indigo-300 applies only when the page is in dark mode and the pointer is over the button.
Common mistakes
- Forgetting the base style. If you write only
dark:bg-slate-900, the element has no background in light mode. Always define both. - Low contrast. Pure white text on pure black can look harsh. Shades such as
slate-900andslate-100are often easier to read. Check contrast in both themes. - Flash of the wrong theme. If your script runs late, the page may show light mode briefly before switching. Put a small script in the
<head>that sets the class before the page renders. - Mixing approaches. If you switch to a class-based toggle but the
darkclass never gets set, the system setting no longer has any effect. - Repeating every pair. If many elements share the same color pairs, consider defining theme colors once, as covered in the theme customization chapter.
Exercise
A page uses the default dark mode behavior. A visitor's operating system is set to dark, but they click a toggle button on your site that adds or removes the class dark on <html>. Nothing changes on the page. Why, and how do you fix it?
Show answer
By default, dark: follows the prefers-color-scheme media query, not a class, so adding the dark class has no effect. To fix it, redefine the variant in CSS with @custom-variant dark (&:where(.dark, .dark *)); (or set darkMode: 'class' in the v3 config). After that, dark: utilities apply when the dark class is present.
Exercise
Write the classes for a paragraph that has text-slate-700 in light mode and text-slate-300 in dark mode.
Show answer
class="text-slate-700 dark:text-slate-300"