Skip to content

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.

Updated 23 November 2026Tested with
  • 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:

Terminal
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

  1. 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 the dark class and everything inside it. :where() keeps the specificity at zero, so a dark: class doesn't win fights it shouldn't.

  2. 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>
  3. 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 *));.

Was this page helpful?

Still stuck? We answer every message ourselves. Write to us

This page is built from UI Pro parts: the docs article block (docs-quernly) with the code, terminal, install command, steps, contents and feedback components. They ship in the next UI Pro release; the install command and the contents rail are free.