Tailwind v4 dark mode: class toggle, system setting and fixes
dark: follows prefers-color-scheme by default. One @custom-variant line switches it to a class. A no-flash toggle, and what to check when it fails.
- Tailwind CSS 4.3.5
Out of the box, the dark: variant in Tailwind CSS v4 follows the visitor's system setting through prefers-color-scheme. To switch it with a class or a toggle, you override the variant with one line of CSS: @custom-variant dark (&:where(.dark, .dark *));. There's no darkMode option any more, because there's no config file by default.
What dark: does by default
With nothing but @import "tailwindcss";, a class like dark:bg-black compiles to a media query:
npx @tailwindcss/cli -i src/styles/global.css | grep -B1 -A3 "dark\\:bg-black" @media (prefers-color-scheme: dark) { .dark\:bg-black { background-color: var(--color-black); } }That's the right choice for a site without a theme switch. It follows the operating system and needs no JavaScript. The moment you add a toggle, you need the class version.
Switch dark mode with a class
Step 1: Override the variant
Add this under the import:
src/styles/global.cssCSS @import "tailwindcss"; @custom-variant dark (&:where(.dark, .dark *));dark:now applies to the element with thedarkclass and everything inside it.:where()keeps the specificity at zero, so adark:class doesn't win fights it shouldn't.Step 2: Set the class before the page paints
Put a small inline script in the
<head>, above your stylesheet if you can. It reads the saved choice, falls back to the system setting and sets the class before anything is drawn, so there's no white flash:index.htmlHTML <script> const saved = localStorage.getItem("theme"); const dark = saved ? saved === "dark" : matchMedia("(prefers-color-scheme: dark)").matches; document.documentElement.classList.toggle("dark", dark);</script>Step 3: Add the toggle
src/theme-toggle.jsJavaScript document.querySelector("#theme-toggle").addEventListener("click", () => { const dark = document.documentElement.classList.toggle("dark"); localStorage.setItem("theme", dark ? "dark" : "light");});
Prefer a data attribute? Use it in the variant instead: @custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *)); and set data-theme="dark" on <html>. We use the class in our own projects because shadcn/ui and next-themes expect it.
Dark mode not working
When dark: classes do nothing, check these in order.
The variant still follows the system
You added a toggle and a dark class, but the page only goes dark when your OS is dark. The @custom-variant line is missing, or it's in a file Tailwind doesn't process. It has to be in the same CSS file as @import "tailwindcss", or a file imported from it.
The class is on the wrong element, or gets removed
Open the inspector and look at <html>. If dark isn't there, your script didn't run or something replaced the attribute. Two common culprits: a framework that re-renders <html> without your class, and client-side navigation that swaps the document. In Astro with view transitions the class really does disappear on every navigation; our shadcn dark mode fixes show the one-line repair.
Your colours have no dark value
If you use variables for colours, every variable needs a value under .dark too. A colour defined only in :root stays light. And if you map those variables in @theme without inline, dark mode inside a nested .dark element keeps the light value; we explain why in custom colours.
Questions
Does Tailwind v4 support darkMode: 'class'?
Only through a JavaScript config loaded with @config. The CSS way is the @custom-variant line above, and it's what the Tailwind docs show.
How do I support light, dark and system?
Store "light", "dark" or nothing. With nothing stored, the head script falls back to prefers-color-scheme. To follow live changes of the system setting, listen to matchMedia("(prefers-color-scheme: dark)") with addEventListener("change", …) while no choice is stored.
How do I disable dark mode in Tailwind?
Don't write dark: classes, and nothing changes. If a library ships dark: classes you don't want, point the variant at a class you never set: @custom-variant dark (&:where(.never-dark, .never-dark *));.
Related articles
- shadcn/uishadcn/ui dark mode not working? Check these seven thingsIs the dark class on <html>? If not: no provider, a flash on load, or Astro view transitions. If it is: colours without a dark value or @theme without inline.
- Tailwind CSS v4Tailwind v4 custom colors: how to add them, and why they don’t workAdd a --color-* variable in @theme and every colour utility follows. The four reasons a custom colour silently does nothing, each reproduced.
- UI designHow to choose a colour palette for a websiteOne brand scale, one neutral scale and a few status colours, with steps assigned by measured contrast. Tailwind’s own scales measured in light and dark.
Still stuck? We answer every message ourselves. Write to us