Sub topic 13
@theme static and which variables reach the output
This chapter assumes you can write a @theme block and know that theme variables generate utilities. It covers one question: which of your theme variables end up in the compiled CSS, and how to change that.
Default behavior: only used variables are emitted
Tailwind v4 does not copy every theme variable into the output. A variable is written to the :root rule only if something uses it. That includes a utility class found in your source files, or a var() reference to it in your CSS.
@import "tailwindcss";
@theme {
--color-brand: #3f3cbb;
--color-accent: #e11d48;
}<button class="bg-brand">Save</button>The page uses bg-brand and never uses bg-accent. The compiled CSS therefore contains --color-brand but not --color-accent. This is why a build that imports the whole default theme does not ship hundreds of unused color variables.
Forcing emission with static
Add the static option to keep every variable in the block, used or not.
@import "tailwindcss";
@theme static {
--color-brand: #3f3cbb;
--color-accent: #e11d48;
}Now both --color-brand and --color-accent appear in the output. Utilities still work as before. Only the emission rule changes.
When you need static
Use static when something outside your scanned class names needs the variable at runtime:
- JavaScript that reads a token with
getComputedStyle(document.documentElement).getPropertyValue("--color-brand"). - Class names built at runtime, which the scanner cannot see, such as
"bg-" + name. - CSS in another file or a third-party stylesheet that calls
var(--color-accent). - Debugging, when you want to inspect the full token set in browser dev tools.
const brand = getComputedStyle(document.documentElement)
.getPropertyValue("--color-brand")
.trim();
console.log(brand); // "#3f3cbb" when the variable is emittedWithout static, this code returns an empty string whenever no class or var() reference uses --color-brand.
static versus inline
The two options are independent, and you can combine them as @theme static inline. They answer different questions.
| Option | Question it answers | Effect |
|---|---|---|
| `static` | Is the variable written to the output? | Always emitted, even when unused |
| `inline` | What value does a utility contain? | The utility uses the value directly and does not reference the variable |
Use inline when a theme variable points to another variable, as in --color-surface: var(--surface). Use static when you need the variable itself to exist.
Cost
static adds bytes to the CSS, so use it only for tokens that need it. A small, deliberate @theme static block for runtime tokens is better than making the whole theme static.
Exercise
Your @theme block defines --color-accent, but no class or CSS uses it. A script calls getPropertyValue("--color-accent") and gets an empty string. What is the smallest change that fixes this, and what stays the same?
Show answer
Change @theme to @theme static, or move only the runtime tokens into a separate @theme static block. The variable is then written to :root even though nothing uses it. Utilities such as bg-accent still need the class name to appear in your source files, because static affects only variable output.