Sub topic 9
When to use @theme and when to use :root
This chapter assumes you know CSS custom properties and var(), you have used Tailwind utility classes, and you have Tailwind v4 imported with @import "tailwindcss";. By the end, you should be able to pick @theme or :root for any variable.
The working model
Both @theme and :root define CSS variables. They differ in what Tailwind does with them.
- A variable in
@themeis a theme variable. Tailwind reads its name, matches the namespace, and generates utility classes and variants. It also emits the variable in the output CSS. - A variable in
:rootis a regular CSS variable. Tailwind ignores it when it decides which utilities to create.
@import "tailwindcss";
@theme {
--color-brand: #3f3cbb;
}
:root {
--header-height: 4rem;
}With this file, bg-brand, text-brand and border-brand exist. No h-header-height utility exists, because --header-height is not in a theme namespace and sits outside @theme.
<div class="bg-brand text-white">Works</div>
<div class="h-header-height">Does nothing</div>A decision rule
Ask one question: should this value become a utility class or a variant?
| Situation | Use |
|---|---|
| You want `bg-*`, `font-*`, `3xl:*` or `ease-*` classes from the value | `@theme` |
| The value is a design token shared by the whole design system | `@theme` |
| The value is only read by your own CSS through `var()` | `:root` |
| The value changes at runtime, for example per user or per class | `:root` plus an override selector |
Layout constants such as a header height, a z-index you use in one place, or a value set by JavaScript belong in :root. Adding them to @theme would create utilities nobody needs.
Why @theme cannot be nested
@theme must sit at the top level of your CSS. It cannot go inside a selector or a media query. Tailwind collects theme variables at build time, so it needs to know them statically.
/* Invalid: @theme cannot be nested under a selector */
.dark {
@theme {
--color-brand: #8b89f0;
}
}Changing a token under a condition is a runtime job, so use a regular variable for it. Build the override with :root and .dark, then expose the result through @theme inline. A later chapter covers this pattern.
Common errors
- Utility not generated. The variable is in
:root, or its name does not start with a known namespace such as--color-. Move it to@themeand check the prefix. - Variable missing from the output. Tailwind emits theme variables only when something uses them, so an unused token may not appear in the CSS. Section "@theme static" in a later chapter shows how to force emission.
- Dark mode does not change. A theme variable holds a fixed value. Overriding it under
.darkwill not update utilities as you expect. Define the value in:root, override it in.dark, and reference it from@theme inline.
Exercise
You need a --sidebar-width: 16rem value used in two of your own stylesheets, and you also want a bg-sunrise class for the color #ff8a3d. Where do you declare each variable, and why?
Show answer
Declare --sidebar-width in :root, because it is only read through var() and needs no utility. Declare --color-sunrise: #ff8a3d; in @theme, because the --color- namespace makes Tailwind generate bg-sunrise and the other color utilities.