Sub topic 12
When dark: classes do nothing
This chapter is a debugging guide. It assumes you have a Tailwind project that builds, you know the dark: variant, and you have read the earlier chapters on the media and class strategies, the v3 and v4 configuration, and the theme toggle script. You will learn to find out why a dark: utility has no visible effect, and how to fix the usual causes.
The working model
A dark: class does nothing for one of two reasons:
- The rule was never generated, or it was generated with a condition that never becomes true. The cause is in your configuration or your file scanning.
- The rule exists and its condition is true, but another rule wins, or the value is the same as the light value. The cause is in the cascade or your color choices.
The condition is set by your strategy:
- With the default strategy, Tailwind wraps the rule in
@media (prefers-color-scheme: dark). The condition is true only when the operating system or browser reports a dark preference. - With a class or selector strategy, Tailwind scopes the rule to a selector such as
.darkor[data-theme=dark]. The condition is true only when a matching element is on the page.
Simplified output for dark:bg-gray-900 under the default strategy looks like this:
@media (prefers-color-scheme: dark) {
.dark\:bg-gray-900 {
background-color: var(--color-gray-900);
}
}Under the v4 class strategy from earlier chapters, the same utility is scoped to the .dark selector instead of the media query:
.dark\:bg-gray-900:where(.dark, .dark *) {
background-color: var(--color-gray-900);
}Knowing which of these two shapes you should have is the first step of every diagnosis.
A fixed order of checks
Work through the checks in order. Each one rules out a whole group of causes.
Check 1: Is the rule in the built CSS?
Open your compiled stylesheet, or look in the browser's Sources panel, and search for dark\:bg-gray-900 (the backslash is part of the escaped class name). If it is missing, Tailwind never saw the class. Common reasons:
- The file is not scanned. In v3, the
contentarray must cover every file with class names. A template in./templates/is invisible ifcontentlists only./src/**/*.{html,js}. In v4, detection is automatic, but files ignored by.gitignoreand some binary or generated paths are skipped. Add a source with@source "../path"in your CSS if needed. - The class name is built dynamically. Tailwind reads your files as plain text and looks for complete class strings. It does not run your code.
// Broken: "dark:bg-gray-900" never appears as a whole string
const bg = (shade) => `dark:bg-gray-${shade}`;
// Works: every class is written out in full
const backgrounds = {
normal: "bg-white dark:bg-gray-900",
muted: "bg-gray-50 dark:bg-gray-800",
};- The build is stale. After you change
tailwind.config.js, restart the dev server. A config change is not always picked up by a running watcher. - You edited the wrong CSS entry. If the page loads
/styles.cssbut your build writes/dist/styles.css, nothing you change will appear.
Check 2: Is the dark condition true on the page?
If the rule exists, the condition must be true. Run this in the browser console:
const root = document.documentElement;
console.log("dark class on <html>:", root.classList.contains("dark"));
console.log("OS prefers dark:", window.matchMedia("(prefers-color-scheme: dark)").matches);
console.log("saved theme:", localStorage.getItem("theme"));
console.log("color-scheme:", getComputedStyle(root).colorScheme);Compare the output with your strategy:
- With the default strategy, only the second line matters. If it prints
false, thedark:classes are correctly inactive. You can test without changing your OS setting: in Chrome or Edge DevTools, open the Rendering panel and set "Emulate CSS media feature prefers-color-scheme" todark. - With a class strategy, only the first line matters. If it prints
false, your toggle script is not setting the class, or it sets it somewhere else.
Strategy mismatch
This is the most common cause. There are two directions.
You added .dark but the project still uses the default strategy. The toggle changes the class, the page does not change, and dark: styles appear only when the OS is dark. The fix is to declare the manual strategy.
For Tailwind v3:
// tailwind.config.js (Tailwind v3.4.1 and later)
module.exports = {
darkMode: "selector", // use "class" in earlier v3 versions
content: ["./src/**/*.{html,js}"],
};For Tailwind v4:
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));You configured a class strategy but expect the OS to control the page. Now dark: styles never apply for users with a dark OS unless your script copies the system preference into a class. The script from the persistence chapter does this on load, but it only runs if it is actually on the page.
Version-specific traps
| Symptom | Likely cause | Fix |
|---|---|---|
| `darkMode: 'selector'` seems ignored in v3 | Tailwind is older than 3.4.1 | Use `'class'`, or upgrade |
| `darkMode: 'class'` in the config has no effect in v4 | v4 has no `darkMode` option | Add `@custom-variant dark` in CSS |
| `@custom-variant` has no effect | It appears before `@import "tailwindcss"`, or sits in a CSS file that is not the entry file | Place it after the import in the file your build processes |
| The v3 `tailwind.config.js` is ignored after upgrading to v4 | v4 does not load JS configs automatically | Move settings to CSS, or load the file with `@config` |
| Both a config option and a custom variant exist | You are mixing v3 and v4 instructions | Keep only the one for your installed version |
Check your installed version with npm ls tailwindcss before you follow any guide. Many online examples mix v3 and v4 syntax.
The class is on the wrong element
Selectors such as .dark * and :where(.dark, .dark *) need the class on an ancestor of the styled element. Put it on <html>, as the toggle script in earlier chapters does.
Typical mistakes:
- The script toggles
document.bodywhile a different part of the page, or a CSS rule, expects the class on<html>. - A component renders in a portal, such as a modal or tooltip, outside the element that holds
.dark. If the class is on a wrapper<div>instead of<html>, the portal content sits outside it and loses the dark styles. - A preview tool such as Storybook or an embedded
<iframe>has its own document. The class on your app's<html>does not reach it. Add the class (or a decorator that sets it) inside the preview document.
The saved value blocks the system choice
The toggle script treats a missing theme key as "follow the system". Any value other than "dark" is treated as light when the key exists. This causes confusing bugs:
- Old code once saved
"system"or"Dark"undertheme. The checklocalStorage.theme === "dark"fails for both, and because the key exists, the system preference is skipped. The page stays light. - A different app on the same origin, such as
localhost:3000in development, wrote its ownthemevalue.
Inspect the Application panel in DevTools, delete the theme key, and reload. If that fixes it, your script needs to handle only the three states it defines ("light", "dark", and no value), and you may want a more specific key such as "app-theme".
The script runs too late or not at all
If the page is dark only after a visible delay, or never, check where the script sits.
- A script at the end of
<body>runs after the first paint, which causes a flash of the wrong theme. - A script with
type="module",defer, orasyncalso runs after parsing, and it will not prevent a flash. The inline script in<head>, before the stylesheet link, avoids this. - A syntax error stops the script silently from the user's point of view. Check the console.
- In a framework with server rendering, the server HTML has no class, and hydration can reset the
classattribute of<html>and removedark. Set the class in a way that your framework does not overwrite, for example in the document template that renders<html>.
Another rule wins
If the rule exists and its condition is true, open the element in DevTools and look at the Styles panel. A crossed-out dark: declaration means another rule is stronger.
- Same property, same element, two utilities.
bg-white dark:bg-gray-900works because variant rules are emitted after plain utilities. But if you also addbg-blue-500without a variant, the order in the stylesheet decides, not the order in yourclassattribute. Give each state its own variant:bg-white dark:bg-gray-900, not three competing background utilities. - Custom CSS with higher specificity. A rule like
.card .title { color: black; }has more specificity than a single utility class and beatsdark:text-white. - Unlayered CSS in v4. Tailwind v4 places its utilities in a cascade layer. Any CSS you write outside a layer beats layered rules regardless of specificity. Put your own styles in
@layer base,@layer components, or@layer utilities, or remove the conflicting rule. - Inline styles and
!important. Both override utility classes. Remove them or use the important modifier on the utility (dark:bg-gray-900!in v4).
The values are the same, or a variable is not redefined
Sometimes the rule applies and nothing visibly changes.
text-gray-900 dark:text-gray-900is a typo you can read past. Compare the two values.- With CSS variables,
dark:bg-[var(--surface)]is redundant if--surfaceis already redefined under.dark, and it does nothing if--surfaceis defined only in:root. Define both values:
:root {
--surface: #ffffff;
--text: #111827;
color-scheme: light;
}
.dark {
--surface: #111827;
--text: #f9fafb;
color-scheme: dark;
}- Native controls and scrollbars stay light when
color-schemeis missing. That is not adark:failure, but it looks like one. CheckgetComputedStyle(root).colorSchemefrom the diagnostic script. - A default color from a component library may set an explicit color on a child element, so the inherited dark text color never reaches it.
A short checklist
- Run
npm ls tailwindcssand confirm the version. - Confirm the strategy declaration matches the version (config for v3,
@custom-variantfor v4). - Restart the dev server.
- Search the built CSS for the escaped class name.
- Run the console diagnostic and compare it with your strategy.
- Clear
localStorage.theme. - Look at the Styles panel for a crossed-out
dark:rule.
Exercise
A project uses Tailwind v4. The config file contains darkMode: 'class', a toggle button adds dark to the <html> element, and dark:bg-gray-900 works only when the operating system is set to dark. What is wrong, and what is the fix?
Show answer
Tailwind v4 has no darkMode option, so the setting is ignored and the variant still uses prefers-color-scheme. Add @custom-variant dark (&:where(.dark, .dark *)); to the CSS file after @import "tailwindcss";, and remove the unused darkMode setting.
Exercise
The page is light after a reload even though the OS is dark and you use the localStorage.theme script. The console shows localStorage.getItem("theme") returns "system". Why, and how do you recover?
Show answer
The script adds the dark class only if the value is exactly "dark", or if the theme key is absent and the OS prefers dark. The key exists with the value "system", so neither condition holds. Remove the key with localStorage.removeItem("theme") and reload. Make sure the code writes only "light" or "dark", and removes the key for system.
Exercise
A component builds its class with dark:bg-${color}-900. The light styles work, and the dark styles never appear. Why, and what is a fix that keeps the code short?
Show answer
Tailwind scans files as text and finds only complete class names, so dark:bg-red-900 never appears in the source and its CSS is never generated. Write the full class names in a lookup object, for example { red: "dark:bg-red-900", blue: "dark:bg-blue-900" }, and select from the object.