Sub topic 14
Runtime theming and dark mode
This chapter assumes you know CSS custom properties and var(), can use Tailwind utility classes, and have read the earlier chapters on @theme and @theme inline. Here you will build a theme that switches at runtime, such as light and dark mode.
Why `@theme` alone is not enough
A variable declared in @theme is a design token. Tailwind generates utilities from it, and the value is fixed when the CSS is built. You cannot change --color-avocado-500 for one subtree with a class like .dark, because @theme must be top-level and cannot be nested under a selector or media query.
Runtime theming needs two things:
- Regular CSS variables whose values change under a selector.
- Theme variables that point to those regular variables.
The pattern
Define the changing values on :root and override them under .dark. Then map them into the theme with @theme inline.
@import "tailwindcss";
:root {
--surface: oklch(1 0 0);
--content: oklch(0.2 0 0);
}
.dark {
--surface: oklch(0.2 0 0);
--content: oklch(0.98 0 0);
}
@theme inline {
--color-surface: var(--surface);
--color-content: var(--content);
}Now bg-surface and text-content exist as utilities.
Why `inline` matters here
Without inline, Tailwind would generate a utility like background-color: var(--color-surface). The variable --color-surface is defined on :root as var(--surface). CSS resolves a var() where the variable is declared, so .dark elements that inherit it can still see the :root resolution, not the .dark value. The override then fails on the elements that use the class.
With inline, Tailwind writes the referenced value straight into the utility:
.bg-surface {
background-color: var(--surface);
}The utility now reads --surface on the element itself, so the .dark override applies.
Using it in markup
<html class="dark">
<body class="bg-surface text-content">
<h1>Hello</h1>
</body>
</html>Toggling the dark class on <html> switches both colors. No dark: variants are needed on each element, because the tokens carry the change.
Common errors
- Using plain
@themewithvar()references. The utility works, but the runtime override does not reach the elements. Use@theme inline. - Putting the overrides inside
@theme. Theme variables cannot sit under a selector. Keep overrides in normal CSS. - Forgetting a default. If
:rootlacks a value, elements outside.darkget no color. - Missing utilities. Names must follow the namespace, so
--surfacealone creates nothing. Use--color-surface.
Other themes
The same pattern supports more than two themes. Add a selector such as [data-theme="forest"] that sets --surface and --content. No change to the @theme inline block is needed.
Exercise
You wrote @theme { --color-surface: var(--surface); } and set --surface differently under .dark, but bg-surface elements inside .dark do not change. What is the fix, and why does it work?
Show answer
Change it to @theme inline. With inline, the utility uses var(--surface) directly, so the variable is resolved on the element that uses the class and the .dark override applies. Without inline, the utility reads --color-surface, which was already resolved from :root.
Exercise
Why can't you write .dark { @theme { --color-surface: black; } }?
Show answer
@theme must be at the top level of the stylesheet. It cannot be nested under a selector. Put the changing values in regular variables under .dark and reference them from @theme inline.