Sub topic 4
Configuring dark mode in Tailwind v3 and v4
This chapter covers how Tailwind decides when dark: utilities apply, and how to change that rule in Tailwind v3 and v4. It assumes you can write utility classes and variants such as hover: and md:, and that a Tailwind project already builds. Earlier chapters introduced the dark: variant, the default system-following behavior, and the idea of a manual class strategy. Here you set that strategy up in each major version. The chapter ends with the mistakes that most often leave dark: classes without effect.
What configuration actually changes
A dark: utility such as dark:bg-gray-900 is a normal rule wrapped in a condition. The only thing you configure is that condition. Tailwind offers three kinds:
| Strategy | Condition in the generated CSS | Who decides the theme |
|---|---|---|
| Media (default) | `@media (prefers-color-scheme: dark)` | The operating system or browser |
| Class | An ancestor has `class="dark"` | Your JavaScript |
| Attribute or custom selector | An ancestor matches a selector such as `[data-theme=dark]` | Your JavaScript |
With the media strategy you write no configuration and no script. The user's system setting is the only input, and the page cannot override it. With the other two, the generated CSS tests your markup, so something must put the class or attribute on an element. That something is the toggle script, covered in a later chapter.
The version you use decides where the setting lives. Tailwind v3 reads a JavaScript config file. Tailwind v4 moves configuration into your CSS.
The default: media
If you do nothing, dark: uses prefers-color-scheme. Both versions behave the same here. This markup needs no configuration:
<div class="bg-white text-gray-900 dark:bg-gray-900 dark:text-white">
<h1 class="text-2xl font-bold">Hello</h1>
<p class="text-gray-600 dark:text-gray-300">This card follows the active theme.</p>
</div>To test it, change your operating system's appearance setting, or use the browser developer tools. Chrome and Edge have a "Emulate CSS media feature prefers-color-scheme" option in the Rendering panel. Firefox has light and dark buttons in the Inspector toolbar.
Choose this default when the system setting is enough for your product. It has no script, no storage, and no flash of the wrong theme, because the browser evaluates the media query before first paint. You give up the in-page toggle.
Tailwind v3: the darkMode option
In v3 you set the strategy with the darkMode key in tailwind.config.js:
// tailwind.config.js (Tailwind v3.4.1 and later)
module.exports = {
darkMode: 'selector', // use 'class' in earlier v3 versions
content: ['./src/**/*.{html,js}'],
};The accepted values are:
'media': the default. You can write it explicitly.'selector':dark:applies when.darkis on the element itself or on an ancestor. Added in v3.4.1.'class': the same behavior as'selector'for the.darkclass. It is the only class-based option before v3.4.1, and it still works in later v3 releases.['selector', '[data-theme="dark"]']: the same idea, but with a selector you choose. Also v3.4.1 or later.['variant', '&:is(.dark *)']: a fully custom variant selector, for cases where you need more than an ancestor match. Also v3.4.1 or later.
For example, to key dark mode to a data-theme attribute in v3.4.1 or later:
// tailwind.config.js (Tailwind v3.4.1 and later)
module.exports = {
darkMode: ['selector', '[data-theme="dark"]'],
content: ['./src/**/*.{html,js}'],
};Then the attribute goes on a parent, usually the <html> element:
<html data-theme="dark">
<body class="bg-white dark:bg-gray-900"></body>
</html>Check your version first
The 'selector' value and the array forms fail in older v3 releases. Run npx tailwindcss --help or check package.json and the lock file. If you are on a release before v3.4.1, use 'class'. If your config uses a value your version does not know, the symptom is usually that dark: silently stays on the media strategy or never matches, so the version check saves time.
Tailwind v4: redefine the variant in CSS
Tailwind v4 has no darkMode option. A darkMode key in a JavaScript config does nothing there. Instead, the dark variant is defined in CSS, and you replace that definition with @custom-variant:
/* Tailwind v4 */
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));This line means: the dark: variant applies to an element that is .dark or is inside a .dark element. The same technique supports a data attribute:
/* Tailwind v4, using a data attribute instead of a class */
@import "tailwindcss";
@custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));Some details about this syntax:
&stands for the element that carries the utility, sodark:bg-gray-900generates a rule whose selector is the utility class followed by this condition.- The
:where()wrapper gives the condition zero specificity. Without it, the variant would add the specificity of.dark, and dark rules could beat other utilities you did not mean to override. With it, ordering inside Tailwind stays predictable. - The selector lists both the element itself and its descendants. If you wrote only
.dark *, an element that has thedarkclass itself would not match its owndark:utilities. That matters when you putdarkon<html>and also style<html>. - The line must come after
@import "tailwindcss", because it overrides the definition imported there. Place it in the same CSS file that holds the import.
Moving from v3 to v4
| Task | Tailwind v3 | Tailwind v4 |
|---|---|---|
| Follow the system | Default, or `darkMode: 'media'` | Default |
| Use a `.dark` class | `darkMode: 'selector'` (v3.4.1+) or `'class'` | `@custom-variant dark (&:where(.dark, .dark *));` |
| Use a data attribute | `darkMode: ['selector', '[data-theme="dark"]']` | `@custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));` |
| Where it lives | `tailwind.config.js` | The main CSS file |
If you upgrade a project, remove the darkMode line from the old config and add the @custom-variant line to your CSS. The markup does not change. The dark: classes in your HTML stay exactly as they were.
A complete working check
The following page is a minimal check for a manual strategy in v4. It uses the class strategy and sets class="dark" directly, so you can confirm the configuration before you write any toggle script.
<!doctype html>
<html lang="en" class="dark">
<head>
<meta charset="utf-8" />
<title>Dark mode check</title>
<link rel="stylesheet" href="/styles.css" />
</head>
<body class="bg-white text-gray-900 dark:bg-gray-900 dark:text-white">
<p class="p-4">Dark if the configuration works.</p>
</body>
</html>With the @custom-variant line from earlier in styles.css (compiled by your build), the page shows a dark background. Delete class="dark" and reload, and the page turns light even if your operating system is set to dark. That second result is the proof that the class strategy has replaced the media strategy.
If you ran the same page with the default strategy and no class, the page would follow your system instead, and the dark class would have no effect.
Which strategy to choose
Use these criteria:
- Follow the system only. Pick media. It is the simplest and has the fewest failure modes.
- A visible toggle, or a stored user choice. Pick class, selector, or attribute. You need the script from the next chapter.
- A framework that already sets a theme attribute. Many UI libraries and theme packages set
data-themeor a class on<html>. Match their convention in your variant so you do not run two systems. - Embedding in a host page. If your components live inside someone else's page, avoid a bare
.darkclass that could collide with theirs. A data attribute with a specific name is safer.
When the configuration seems ignored
The most common causes, in the order worth checking:
- Wrong version syntax.
darkModein a v4 project, or@custom-variantin a v3 project, does nothing. Confirm the installed version first. - A v3 value your release does not support.
'selector'before v3.4.1 is the usual case. Use'class'there. - The config file is not loaded. In v3, the config must be found by the build, and
contentmust list the files that contain your class names. A missing path means nodark:CSS is generated for those files. - The class or attribute is on the wrong element. It must be on the element or an ancestor. A
darkclass on a sibling does not match. - The
@custom-variantline is above the import. The import then overrides your definition. - A stale build. After changing the config, restart the dev server or rebuild. Some watchers do not reload the config file.
- Class names built from strings. Tailwind finds classes by scanning your source text. A name assembled in code, such as
"dark:" + color, is never generated.
You will meet more of these cases in the troubleshooting chapter. For now, remember the order: version, syntax, scanned files, element, rebuild.
Exercise
Your project uses Tailwind v4. You want dark: utilities to apply when <html data-theme="dark"> is set. Write the CSS you add after the import, and explain why a darkMode key in tailwind.config.js would not help.
Show answer
Add @custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *)); after @import "tailwindcss";. Tailwind v4 defines the dark variant in CSS and has no darkMode option, so a config key is ignored. The :where() wrapper keeps specificity at zero, and listing both the attribute element and its descendants lets <html> style itself.
Exercise
A v3.3 project uses darkMode: 'selector', and dark: classes never match even though class="dark" is on <html>. What is the likely cause, and what is the fix?
Show answer
The 'selector' value was added in v3.4.1, so v3.3 does not recognize it. Change the value to 'class', or upgrade Tailwind to v3.4.1 or later. Then rebuild so the new setting takes effect.