Sub topic 12
@theme inline for variables that reference variables
This chapter assumes you know CSS custom properties and var(), can use Tailwind utility classes, and have a CSS file that starts with @import "tailwindcss";. It covers one option of the @theme directive: inline.
The problem: a theme variable that points to another variable
A normal @theme block turns each variable into a utility. For --color-surface, Tailwind generates bg-surface, text-surface and so on. It also emits the variable in the output CSS, and the generated utilities read it with var():
@theme {
--color-primary: var(--brand);
}With this block, the utility bg-primary compiles to roughly background-color: var(--color-primary). The variable --color-primary is defined on :root as var(--brand).
This works until you override --brand somewhere deeper in the page, for example on a single section. A var() reference in a custom property is resolved on the element that declares it. --color-primary is declared on :root, so it resolves --brand there. Children that inherit --color-primary get that already-resolved value. An override of --brand on .section is never seen.
What the inline option does
Adding inline changes how utilities are generated. The utility copies the variable's value instead of pointing to the variable:
@import "tailwindcss";
@theme inline {
--color-primary: var(--brand);
}Now bg-primary compiles to background-color: var(--brand). The var(--brand) is evaluated on the element that uses the class, so it sees the nearest --brand in the cascade.
| Option | Utility output for `bg-primary` | `--brand` is resolved |
|---|---|---|
| `@theme` | `var(--color-primary)` | Once, on `:root` |
| `@theme inline` | `var(--brand)` | On each element that uses the class |
Worked example: a section with its own brand color
@import "tailwindcss";
:root {
--brand: oklch(0.55 0.2 265);
}
.promo {
--brand: oklch(0.7 0.2 40);
}
@theme inline {
--color-primary: var(--brand);
}<button class="bg-primary px-4 py-2 text-white">Default</button>
<section class="promo">
<button class="bg-primary px-4 py-2 text-white">Promo</button>
</section>The first button uses the :root value. The second sits inside .promo, so var(--brand) resolves to the orange value there. Without inline, both buttons would show the :root color.
When to use it
- Use
inlinewhen the value isvar(...)pointing to a variable that changes by scope, such as a class-based dark mode or a per-section palette. - Skip it for plain values like
oklch(...)or#3f3cbb. Nothing needs to be resolved later, so the default is fine and keepsvar(--color-primary)available in your own CSS. - Variables defined with
inlineare not emitted as--color-primaryin the output unless you also need them. If you want to readvar(--color-primary)from custom CSS, use the default option or@theme static inline.
Common errors
- Override ignored: the theme variable references another variable but
inlineis missing. Addinline. var(--color-primary)is undefined in custom CSS: the variable is inline-only and no utility used it, or it was never emitted. Use the default option, or mark the blockstatic.- Utility missing: the name does not start with a known namespace, such as
--primaryinstead of--color-primary.
Exercise
A page defines :root { --accent: teal; } and .invert { --accent: orange; }. The theme has @theme { --color-accent: var(--accent); }, but bg-accent inside .invert is still teal. What is the fix, and why does it work?
Show answer
Change the block to @theme inline { --color-accent: var(--accent); }. Without inline, bg-accent reads var(--color-accent), which was resolved to teal on :root. With inline, the utility uses var(--accent) directly, so it resolves on the element and picks up orange inside .invert.