Sub topic 12
When dark: classes do nothing
This chapter assumes you have a Tailwind project that builds, that you know hover: and md: variants, and that you have read the earlier chapters on the media and class strategies. Here you will work through the usual reasons a dark: class has no effect, in the order you should check them.
Start with the strategy
Most failures come from a mismatch between how dark mode is activated and how the variant is configured. Tailwind decides which condition dark: waits for. Your page must meet that same condition.
Use the flowchart below as a checklist.
Cause 1: The strategy does not match your toggle
By default, dark: follows prefers-color-scheme. If you add a dark class to <html> and change nothing else, nothing happens, because Tailwind is still waiting for the operating system setting.
The reverse also occurs. If you configured a class strategy but expect the page to follow the system, dark: stays inactive until the class is present.
In Tailwind v3, set the strategy in the config file:
// tailwind.config.js (Tailwind v3.4.1 and later)
module.exports = {
darkMode: 'selector', // use 'class' in earlier v3 versions
content: ['./src/**/*.{html,js}'],
};In Tailwind v4, the darkMode option no longer exists. Redefine the variant in your CSS:
/* Tailwind v4 */
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));If you use a data-theme attribute instead of a class, the selector in the variant must match it:
/* Tailwind v4, using a data attribute instead of a class */
@import "tailwindcss";
@custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));Cause 2: The class is on the wrong element
The class strategy uses a selector that matches the dark class on an element or any of its ancestors. Put the class on <html> so it reaches the whole page. Common mistakes:
- Adding
darkto a sibling or child instead of an ancestor of the styled element. - Toggling the class on
<body>while some content, such as a modal mounted outside the body's subtree, sits elsewhere. - Misspelling the class, for example
Darkordark-mode.
Open the browser's element inspector and confirm that <html class="dark"> is present after you toggle.
Cause 3: The utility was never generated
Tailwind only emits CSS for class names it finds in the files listed in content (v3) or detected automatically (v4). If you build class names dynamically, such as 'dark:bg-' + color, the scanner cannot see the full name, so no CSS exists.
Write complete class names in your source. Then search the built stylesheet for dark\:bg-gray-900. If it is missing, fix the content globs in v3 or check that the file is not ignored in v4.
Cause 4: A different rule wins
A dark: utility is a normal rule with a media query or a selector. It can lose to another rule. Check these points:
- A light utility and a
dark:utility on the same element should both be present, such asbg-white dark:bg-gray-900. Without the light one, the element has no base style to switch from. - An inline
styleattribute beats any class. - Custom CSS with higher specificity or later order can override a utility.
- Stacked variants must be written in the right form.
dark:hover:bg-gray-700applies only in dark mode while hovering.
<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>Cause 5: The script runs too late or saves the wrong value
If dark mode works after a click but disappears on reload, the saved choice is not applied early enough. The script from the persistence chapter must run in the <head>, before the stylesheet and body render:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script>
document.documentElement.classList.toggle(
"dark",
localStorage.theme === "dark" ||
(!("theme" in localStorage) &&
window.matchMedia("(prefers-color-scheme: dark)").matches),
);
</script>
<link rel="stylesheet" href="/styles.css" />
</head>
<body class="bg-white dark:bg-gray-900"></body>
</html>Also check the stored value. localStorage.theme = "Dark" does not equal "dark", so the comparison fails. Open the Application tab in the developer tools and read the theme key.
Cause 6: Stale builds
A watcher that stopped, a cached bundle, or a service worker serving an old stylesheet can hide a correct fix. Restart the dev server, hard reload, and confirm the stylesheet you loaded is the one you just built.
Quick reference
| Symptom | Likely cause | Fix |
|---|---|---|
| Works only when the OS is dark | Default `media` strategy | Set `selector` (v3) or `@custom-variant` (v4) |
| `dark` class present, no change | Config ignored or wrong version | Match the config to your Tailwind version |
| Some elements ignore it | Class not on an ancestor | Put `dark` on `<html>` |
| Class missing from the CSS | Dynamic names or bad content paths | Use full class names, fix globs |
| Works after click, lost on reload | Script placement or bad stored value | Run the script inline in `<head>` |
Exercise
A v4 project has darkMode: 'class' in an old config file, and the page has <html class="dark">. Elements with dark:bg-gray-900 still show a white background when the OS is in light mode. What is wrong, and what is the fix?
Show answer
Tailwind v4 has no darkMode option, so the setting is ignored and dark: still uses prefers-color-scheme. Add @custom-variant dark (&:where(.dark, .dark *)); to the CSS after @import "tailwindcss";.
Exercise
You wrote class={"dark:bg-" + shade} in a template, and no dark background appears. Why, and how do you fix it?
Show answer
The scanner never sees a complete class name, so Tailwind does not generate the CSS. Write full names, for example by mapping each value to a literal string such as dark:bg-gray-900.