Sub topic 13
@theme static and which variables reach the output
This chapter assumes you can write a @theme block, know how namespaces such as --color-* create utilities, and have used var() in plain CSS. It covers which theme variables Tailwind v4 writes into your compiled CSS, and how @theme static changes that.
Theme variables are not always emitted
A @theme block does two jobs. It tells Tailwind which utilities and variants exist, and it declares CSS variables. You might expect every declared variable to appear in the compiled stylesheet. By default it doesn't.
Tailwind v4 emits a theme variable only when something in your project uses it. The default theme alone defines hundreds of variables: every color shade, every font size, every radius. Writing all of them into every stylesheet would add a lot of unused CSS, so Tailwind leaves out the ones nobody references.
Here is a small theme:
@import "tailwindcss";
@theme {
--color-brand: #3f3cbb;
--color-accent: #f59e0b;
--font-display: "Satoshi", "sans-serif";
}Suppose your templates contain only this markup:
<h1 class="bg-brand font-display">Hello</h1>The compiled CSS contains the variables behind bg-brand and font-display. It does not contain --color-accent, because no class or var() refers to it. Simplified, the output looks like this:
@layer theme {
:root,
:host {
--font-display: "Satoshi", "sans-serif";
--color-brand: #3f3cbb;
}
}
@layer utilities {
.bg-brand {
background-color: var(--color-brand);
}
.font-display {
font-family: var(--font-display);
}
}If you add class="text-accent" later, --color-accent appears in the output on the next build. You never manage this by hand.
What counts as "used"
A variable is emitted when at least one of these is true:
- A utility class that reads it appears in your sources.
bg-branduses--color-brand. - Your own CSS reads it with
var(). The.cardexample from earlier chapters, withvar(--color-avocado-100), marks that variable as used. - Another variable that is already emitted references it. If
--color-primary: var(--color-red-500)is emitted,--color-red-500is emitted too, so the reference resolves.
Anything else is dropped. This applies to your own variables and to the default theme.
Where default pruning causes trouble
Pruning is invisible as long as the only consumers of your variables are Tailwind utilities and your stylesheet. It becomes a problem when something Tailwind cannot see reads the variable at runtime:
- JavaScript calls
getComputedStyle(document.documentElement).getPropertyValue("--color-brand"). A chart library might do this to match your palette. - A string built at runtime, such as
style={{ color: "var(--color-" + name + ")" }}, produces a variable name Tailwind never sees in your source. - Markup from a CMS or a third-party widget uses
var(--color-brand)in inline styles that are not in your scanned files. - A design-token documentation page lists every token by reading the variables from the page.
In each case the variable exists in your @theme block, but the browser reports it as undefined. The symptom is usually an empty string from getPropertyValue, or a style that silently falls back to its default.
Always emitting variables with static
Add the static option to the block to write every variable in it to the output, whether or not anything uses it:
@import "tailwindcss";
@theme static {
--color-brand: #3f3cbb;
--color-accent: #f59e0b;
--font-display: "Satoshi", "sans-serif";
}With this block, the compiled CSS contains all three variables, even for the <h1 class="bg-brand font-display"> markup above:
@layer theme {
:root,
:host {
--color-brand: #3f3cbb;
--color-accent: #f59e0b;
--font-display: "Satoshi", "sans-serif";
}
}Utilities still follow the normal rule. static changes which variables are emitted. It does not change which utilities are generated, so .text-accent is still only produced if some file uses text-accent.
Now a runtime script can read the token:
<script>
const root = getComputedStyle(document.documentElement);
const brand = root.getPropertyValue("--color-brand").trim();
console.log(brand); // "#3f3cbb"
</script>Without static, and with no class or CSS using --color-brand, the same script would log an empty string.
Mixing static and default blocks
You can have several @theme blocks in one stylesheet, and each block's options apply only to that block. This lets you be selective:
@import "tailwindcss";
/* Always emitted: read by JavaScript and by embedded widgets */
@theme static {
--color-brand: #3f3cbb;
--color-brand-dark: #2b2990;
}
/* Emitted only when used */
@theme {
--color-promo: #e11d48;
--ease-fluid: cubic-bezier(0.3, 0, 0, 1);
}@theme static does not turn on emit-everything for the default theme. The default palette, which @import "tailwindcss" brings in, still follows the used-only rule. If you want an unused default variable such as --color-red-500 to appear, redeclare it in a static block:
@theme static {
--color-red-500: oklch(0.637 0.237 25.331);
}Copy the exact default value from Tailwind's theme.css first, or you will override the default with a value you typed from memory.
Static versus inline
Both options change how a @theme block behaves, but they answer different questions.
| Option | Question it answers | Effect |
|---|---|---|
| `static` | Does this variable reach the output when nothing uses it? | Always emitted. |
| `inline` | Do utilities read the variable or its value? | Utilities contain the value, such as `var(--surface)`, not `var(--color-surface)`. |
An inline block that is not static still follows the used-only rule. Choose each option for its own reason. Use inline when a theme variable references another variable that changes at runtime, as in the dark mode setup from the previous chapter. Use static when something outside Tailwind needs to read the variable.
Deciding what goes where
A practical rule: keep tokens in a normal @theme block by default, so the stylesheet stays small. Move a token to @theme static only when you can name the consumer that Tailwind cannot see.
Common mistakes
- Expecting
staticto emit the default theme's variables. It only affects the block it is attached to. - Writing
@theme staticand then expecting a utility such astext-accentto exist without using it. Utilities are still generated on demand. - Building variable names from strings at runtime and not marking the variables
static. Tailwind cannot detect names assembled in JavaScript. - Debugging a missing variable in a built file when it was never used in the source Tailwind scanned. Check which files Tailwind scans if a used variable is still missing.
Exercise
Your @theme block defines --color-chart-1 through --color-chart-5. Only a charting library reads them, through getComputedStyle, and no class or CSS uses them. In production the library gets empty strings. What change fixes this, and why does the problem not appear in a layout that happens to use bg-chart-1?
Show answer
Move the five variables into @theme static { ... }. By default Tailwind emits a theme variable only when a utility, a var() in your CSS, or another emitted variable uses it, and the library's runtime read does not count. A layout that uses bg-chart-1 makes --color-chart-1 count as used, so that one variable is emitted. The other four are still left out. static makes all five always appear in the output.
Exercise
A stylesheet has @theme static { --color-brand: #3f3cbb; } and nothing in the project uses the class text-brand. Is --color-brand in the output? Is the .text-brand rule in the output?
Show answer
--color-brand is in the output, because static forces every variable in the block to be emitted. The .text-brand rule is not, because utilities are generated only when a class name appears in your sources. static does not change utility generation.