Sub topic 9
When to use @theme and when to use :root
This chapter assumes you know CSS custom properties and var(). It also assumes you can write Tailwind utility classes and that your CSS file starts with @import "tailwindcss";. By the end you should be able to decide, for any variable, whether it belongs in @theme or in :root.
Two places to put a variable
Tailwind v4 treats @theme and :root as different tools, even though both produce CSS variables.
- A variable in
@themeis a design token. Tailwind reads its name, matches the namespace, and generates utilities and variants from it. - A variable in
:rootis a plain CSS variable. It exists in the browser and nothing else happens.
The rule of thumb: if you want a class such as bg-brand or a variant such as 3xl:, use @theme. If you only want a value to read with var(), use :root.
@import "tailwindcss";
@theme {
--color-brand: oklch(0.55 0.2 270);
}
:root {
--header-height: 4rem;
}With this file, bg-brand, text-brand and border-brand all exist. No h-header-height utility exists, because --header-height is not in a theme namespace and was not declared in @theme.
What @theme does that :root cannot
@theme is a Tailwind directive, not standard CSS. Tailwind processes it at build time, so it has three effects.
- It generates utilities. The namespace in the variable name (
--color-*,--font-*,--breakpoint-*,--ease-*) selects which utilities are created. - It can extend or remove defaults. Setting
--color-*: initial;only has meaning inside@theme. - It controls which variables reach the output. By default, Tailwind emits a theme variable into the final CSS only if some class in your project uses it.
A plain :root rule has none of these properties. The browser sees it, and Tailwind's class generation ignores it.
| Question | `@theme` | `:root` |
|---|---|---|
| Creates utility classes? | Yes, by namespace | No |
| Creates variants (such as `3xl:`)? | Yes, for `--breakpoint-*` | No |
| Can reset defaults with `initial`? | Yes | No |
| Always present in the output CSS? | Only if used (unless `static`) | Yes |
| Can be nested inside a selector or media query? | No, top level only | Yes |
| Good for values that change at runtime? | Only with `inline` and a source variable | Yes |
The placement restriction
@theme must be at the top level of your CSS. You cannot put it inside a selector or a media query. This means the value of a theme variable is fixed when the CSS is built.
/* This does not work */
.dark {
@theme {
--color-brand: white;
}
}For values that must change per context, such as a dark mode surface color, use :root and a class override, then expose the result to Tailwind with @theme inline. The runtime theming chapter covers this in full. Here the point is the division of labor. @theme says which tokens exist. :root and its overrides say what the values are at runtime.
Unused theme variables are dropped
Because Tailwind emits only theme variables that are used, a token that no class references will not appear in your output CSS. This keeps the file small, but it surprises people who read the variable from their own CSS or from JavaScript.
@import "tailwindcss";
@theme {
--color-avocado-500: oklch(0.84 0.18 117.33);
--color-unused: oklch(0.7 0.1 30);
}If no HTML contains bg-avocado-500 (or another class that reads this variable), --color-avocado-500 may be missing from the built CSS. A custom rule that calls var(--color-avocado-500) counts as a use, so this works:
.badge {
background: var(--color-avocado-500);
}Tailwind sees the var() reference and keeps the variable. If you need a variable always present, for example because JavaScript reads it with getComputedStyle, declare it with @theme static:
@theme static {
--color-avocado-500: oklch(0.84 0.18 117.33);
}A variable in :root has no such problem. It is always emitted, because Tailwind does not touch it.
Decision guide
Ask these questions in order.
- Do I want a utility class or variant from this value? Use
@themewith the right namespace. - Does the value change at runtime (dark mode, user preference, per-section theme)? Define the changing variable in
:rootand its overrides, then map it into the theme with@theme inline. - Is it a one-off value for my own CSS (a header height, a z-index layer, a third-party widget setting)? Use
:root. - Must it always be in the output, regardless of use? Use
@theme static, or:rootif it needs no utilities.
Worked example
Suppose a site has a brand color, a fixed header height, and a page background that flips in dark mode.
@import "tailwindcss";
@theme {
--color-brand: oklch(0.55 0.2 270);
}
:root {
--header-height: 4rem;
--page-bg: oklch(1 0 0);
}
.dark {
--page-bg: oklch(0.2 0 0);
}
@theme inline {
--color-page: var(--page-bg);
}--color-brandis a fixed token, so it sits in plain@theme. It givesbg-brand,text-brandand the rest.--header-heightis only read by custom CSS, so it stays in:root.--page-bgchanges at runtime, so it lives in:rootand.dark.--color-pagein@theme inlinecreatesbg-page, and theinlineoption makes the utility usevar(--page-bg)directly.
<body class="bg-page">
<header class="bg-brand" style="height: var(--header-height)">Site</header>
</body>Adding the dark class to html or body changes --page-bg, and bg-page follows it.
Why inline matters here
Without inline, Tailwind writes the utility as background-color: var(--color-page). The --color-page variable is defined where the theme variables are emitted, normally on :root, with the value var(--page-bg) resolved at that level. A .dark override of --page-bg on a deeper element then may not reach elements that read --color-page, because the reference was already resolved on :root.
With inline, the utility becomes background-color: var(--page-bg). The variable is read at the element itself, so any override on an ancestor applies.
/* Without inline: utility reads var(--color-page) */
/* With inline: utility reads var(--page-bg) */Use inline whenever a theme variable's value is another var() that may be overridden below :root.
Common mistakes
- Putting a token in
:rootand expecting a class.--color-brandin:rootcreates nobg-brand. Move it to@theme. - Putting a runtime value in plain
@theme. A.darkoverride is not seen by elements using the utility. Use the:rootplus@theme inlinepattern. - Nesting
@theme. It must be at the top level. - Reading an unused theme variable from JavaScript. It may not exist in the output. Use
@theme static. - Wrong namespace.
--brand-colorin@themegenerates no color utilities. The name must start with--color-.
@theme {
--brand-color: oklch(0.55 0.2 270); /* no bg-brand-color utility */
--color-brand: oklch(0.55 0.2 270); /* bg-brand works */
}How the pieces relate
Exercise
A teammate writes :root { --color-accent: oklch(0.7 0.2 40); } and then uses class="bg-accent", but the background is not applied. Why, and what is the fix? Then explain how you would also make the accent color switch under a .high-contrast class.
Show answer
Tailwind generates utilities only from variables in @theme, so a :root variable creates no bg-accent class. Move the token into @theme: @theme { --color-accent: oklch(0.7 0.2 40); }. To switch it under .high-contrast, keep the changing value in :root as a separate variable (for example --accent: oklch(0.7 0.2 40);), override it in .high-contrast { --accent: oklch(0.4 0.25 40); }, and map it with @theme inline { --color-accent: var(--accent); }. The inline option makes bg-accent read var(--accent) at the element, so the override applies.
Exercise
Your JavaScript reads getComputedStyle(document.documentElement).getPropertyValue("--color-brand"), and it returns an empty string in production, though the variable is defined in @theme. What is likely happening, and how do you fix it?
Show answer
Tailwind emits theme variables only when something in the project uses them. If no class or var(--color-brand) reference uses the token, it is left out of the build. Declare it in @theme static { --color-brand: ...; } so it is always emitted, or define it in :root if no utilities are needed.