Sub topic 14
Runtime theming and dark mode
This chapter assumes you know how @theme turns namespaced variables into utilities, how :root variables differ from theme variables, and what @theme inline does at a basic level. Here you combine them to build a theme that changes while the page is open, with dark mode as the main case.
Why a static theme is not enough
A plain @theme block describes tokens that are fixed at build time. --color-brand: #3f3cbb always means the same color. Dark mode needs the same utility, such as bg-surface, to paint a different color depending on state.
There are two ways to get that:
- Write
dark:variants on every element, as inbg-white dark:bg-zinc-900. - Define semantic tokens once and change their values at runtime.
The second approach keeps markup short and puts the palette in one place. It needs three parts: ordinary CSS variables that hold the changing values, theme variables that point at them, and a selector that switches the values.
Step 1: Variables that change
Theme variables are meant to be stable, so put the values that change in regular CSS variables.
:root {
--surface: oklch(1 0 0);
--content: oklch(0.2 0 0);
--accent: oklch(0.55 0.2 270);
}
.dark {
--surface: oklch(0.2 0 0);
--content: oklch(0.98 0 0);
--accent: oklch(0.75 0.15 270);
}The names --surface, --content and --accent have no namespace prefix, so Tailwind ignores them. They are plain CSS. The .dark rule redefines them for any element with that class and its descendants.
Step 2: Expose them as theme variables
Now map them into the color namespace so that utilities exist.
@theme inline {
--color-surface: var(--surface);
--color-content: var(--content);
--color-accent: var(--accent);
}This generates bg-surface, text-content, border-accent and the rest of the color utilities.
Why `inline` matters
Without inline, Tailwind writes the theme variable into the output and the utility reads that variable:
:root {
--color-surface: var(--surface);
}
.bg-surface {
background-color: var(--color-surface);
}A custom property that contains var() is resolved where it is declared. --color-surface is declared on :root, so it takes the --surface value from :root. Children inherit that already-resolved color. If you later set --surface on .dark or on a nested element, --color-surface does not change. The override never reaches the utility.
With inline, the utility uses the referenced variable directly:
.bg-surface {
background-color: var(--surface);
}var(--surface) is now resolved on the element that uses the class. It sees the nearest override, so .dark and nested scopes work.
> The rule of thumb: if a theme variable's value is a var() reference and that referenced variable can change in a subtree, use inline.
Step 3: Switch the selector
Use the variables in markup. No dark: prefix is needed.
<body class="bg-surface text-content">
<main class="p-8">
<h1 class="text-2xl font-bold">Dashboard</h1>
<a href="#" class="text-accent underline">Settings</a>
</main>
</body>Toggling dark mode now means adding or removing a class on <html>. Put the class on <html> and not <body>, so that the root background and scrollbars also follow it.
const root = document.documentElement;
function setTheme(theme) {
root.classList.toggle("dark", theme === "dark");
localStorage.setItem("theme", theme);
}
const saved = localStorage.getItem("theme");
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
setTheme(saved ?? (prefersDark ? "dark" : "light"));Run this script in the <head> before the body renders. If you run it at the end of the page, users see a flash of the light theme first.
Following the operating system without JavaScript
If you only want to follow the system setting, skip the class and use a media query:
:root {
--surface: oklch(1 0 0);
--content: oklch(0.2 0 0);
}
@media (prefers-color-scheme: dark) {
:root {
--surface: oklch(0.2 0 0);
--content: oklch(0.98 0 0);
}
}Adding color-scheme: light dark; to :root also makes form controls and scrollbars match.
Mixing tokens with the `dark:` variant
You can still use dark: for one-off differences. By default, Tailwind v4's dark: variant follows prefers-color-scheme. If you toggle with a class, the two will disagree unless you redefine the variant:
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));After this, dark: matches elements inside .dark, which is the same condition your variables use. Pick one trigger and use it everywhere.
More than two themes
The pattern is not limited to light and dark. Any selector can supply values, for example a data attribute:
[data-theme="ocean"] {
--surface: oklch(0.97 0.02 220);
--content: oklch(0.25 0.05 240);
--accent: oklch(0.6 0.15 200);
}Set data-theme="ocean" on <html> and the same classes repaint. Because the variables inherit, you can also scope a theme to one section of a page: a <section data-theme="ocean"> inside a light page works only because the theme variables use inline.
Which values to make dynamic
Keep the dynamic layer small. A typical set is surface, content, border, muted text and one or two accents. Leave the rest of the palette static in @theme:
@theme {
--color-brand-500: oklch(0.55 0.2 270);
}
@theme inline {
--color-surface: var(--surface);
--color-content: var(--content);
}You can have multiple @theme blocks in one file. They are merged.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `.dark` has no effect on `bg-surface` | Theme variable declared with plain `@theme` and a `var()` value | Use `@theme inline` |
| `dark:` classes ignore the toggle | Default `dark:` variant uses the media query | Add `@custom-variant dark` |
| Flash of light theme on load | Theme script runs after first paint | Run it inline in `<head>` |
| Utility such as `bg-surface` is missing | Theme variable name lacks the `--color-` prefix | Rename to `--color-surface` |
| Overridden value shows in some places only | Override placed on an element that is not an ancestor | Set variables on `<html>` or the right wrapper |
A complete page
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="/styles.css" />
<script>
const saved = localStorage.getItem("theme");
const dark = window.matchMedia("(prefers-color-scheme: dark)").matches;
document.documentElement.classList.toggle("dark", (saved ?? (dark ? "dark" : "light")) === "dark");
</script>
</head>
<body class="bg-surface text-content">
<button
class="m-4 rounded-lg border border-accent px-3 py-1 text-accent"
onclick="const d = document.documentElement.classList.toggle('dark'); localStorage.setItem('theme', d ? 'dark' : 'light')"
>
Toggle theme
</button>
</body>
</html>Pair it with the :root, .dark and @theme inline blocks from the earlier steps in styles.css, and add a --color-border mapping if you want border-border.
Exercise
Your app defines --color-surface: var(--surface) inside a plain @theme block, with --surface changed under .dark. Pages stay light when .dark is added to <html>. Explain the cause and give the fix.
Show answer
Inside a plain @theme, --color-surface is emitted on :root and its var(--surface) is resolved there, so descendants inherit the light color. Overriding --surface under .dark happens after that resolution and never reaches bg-surface, which reads --color-surface. Change the block to @theme inline { --color-surface: var(--surface); }. The utility then emits background-color: var(--surface), which resolves on each element and picks up the .dark override.
Exercise
You toggle dark mode by adding a dark class, but a component using dark:bg-zinc-900 stays light while bg-surface switches correctly. What is missing?
Show answer
The dark: variant still uses the default prefers-color-scheme media query. Add @custom-variant dark (&:where(.dark, .dark *)); after the Tailwind import so dark: matches the same .dark class that drives the variables.