Sub topic 12
@theme inline for variables that reference variables
This chapter assumes you know how @theme defines design tokens, how namespaces such as --color-* generate utilities, and how plain CSS custom properties and var() work. It covers one option of the directive, inline. The option matters when a theme variable points at another variable instead of holding a literal value.
The problem: a theme variable that references a variable
Consider this setup:
@import "tailwindcss";
:root {
--surface: oklch(1 0 0);
}
.dark {
--surface: oklch(0.2 0 0);
}
@theme {
--color-surface: var(--surface);
}The intent is that bg-surface follows --surface, so it changes when .dark is applied. Whether it does depends on where each variable is defined.
A plain @theme block makes Tailwind emit the theme variables on :root. Utilities then refer to the theme variable by name. The output looks roughly like this:
:root {
--color-surface: var(--surface);
}
.bg-surface {
background-color: var(--color-surface);
}Custom properties that contain var() are resolved on the element that declares them. --color-surface is declared on :root, so it takes the value of --surface on :root. Every descendant inherits that already-resolved value. If .dark is set on a descendant such as <body> or a wrapper <div>, that element gets a new --surface. It does not get a new --color-surface, because the inherited value is a finished color, not a var() reference. The utility keeps reading the light color.
The same thing happens when the referenced variable is defined lower in the tree than :root. A common case is a font loader that puts --font-inter on <body> through a class:
@theme {
--font-sans: var(--font-inter);
}On :root, --font-inter does not exist yet, so --font-sans becomes invalid. The font-sans utility then falls back to the inherited font.
What inline changes
Adding the inline option tells Tailwind to put the value of the theme variable straight into the generated utility, instead of pointing the utility at the theme variable:
@theme inline {
--color-surface: var(--surface);
}The utility is now generated like this:
.bg-surface {
background-color: var(--surface);
}The var(--surface) reference is resolved on the element that has the class. If that element, or any ancestor between it and :root, sets a different --surface, the utility uses it.
The next diagram shows where the reference is resolved in each case.
A complete example: switchable colors
This stylesheet defines two semantic colors and switches them with a dark class on any element:
@import "tailwindcss";
:root {
--surface: oklch(1 0 0);
--content: oklch(0.2 0 0);
}
.dark {
--surface: oklch(0.2 0 0);
--content: oklch(0.98 0 0);
}
@theme inline {
--color-surface: var(--surface);
--color-content: var(--content);
}It is used like this:
<div class="bg-surface text-content p-6">
Light card
<div class="dark">
<p class="bg-surface text-content p-4">
This panel uses the dark values, even though .dark is not on the html element.
</p>
</div>
</div>The inner <p> is inside a .dark wrapper. Its bg-surface becomes background-color: var(--surface), and --surface resolves to the dark value there. With a plain @theme, the inner paragraph would stay light.
Tailwind's own dark: variant is a different tool. It switches whole utilities, as in bg-white dark:bg-black. The pattern above defines the colors once and lets CSS variables carry the switch. Chapter material on runtime theming covers the full pattern. Here the point is only that inline is what makes it work on nested elements.
Referencing variables from other sources
The same fix applies whenever the referenced variable is defined somewhere other than :root, or by something outside Tailwind:
@import "tailwindcss";
@theme inline {
--font-sans: var(--font-inter), ui-sans-serif, system-ui, sans-serif;
}Assume --font-inter comes from a framework's font loader and is set on a wrapper element. Without inline, --font-sans is computed on :root, where --font-inter is not available. With inline, font-sans becomes font-family: var(--font-inter), ui-sans-serif, ... on each element, and the lookup happens where the variable exists.
The list after var(--font-inter) is a fallback font stack in the font-family value. It is ordinary CSS and is not part of the inline feature.
Referencing other theme variables
inline is also useful when one theme variable references another theme variable:
@theme {
--color-brand: oklch(0.55 0.2 270);
}
@theme inline {
--color-link: var(--color-brand);
}Here text-link is generated as color: var(--color-brand). This works without inline too, because both variables live on :root. The difference shows up only if someone overrides --color-brand below the root. With inline, text-link follows that override. Without it, --color-link is fixed at the root.
Use inline when you need the lookup to happen at the point of use. For tokens that never change per element, a plain @theme is simpler and keeps the variable available as a named value.
Trade-offs and gotchas
- The theme variable may not exist at runtime. Because utilities no longer reference
--color-surface, Tailwind has no reason to emit it. Do not depend onvar(--color-surface)in custom CSS or in inline styles when you useinline. Reference the source variable (var(--surface)) instead. If you need the theme variable to always be emitted, that is what@theme staticis for, covered in the next chapter. - The source variable must be defined.
inlineonly changes wherevar(--surface)is looked up. If--surfaceis not defined on the element or an ancestor, the property is invalid at computed-value time, and the utility has no effect. Define defaults on:root. - Literal values gain nothing.
--color-brand: #3f3cbbinside@theme inlinebehaves the same as in a plain@theme. The option matters only for values that containvar(). - You can mix blocks. A file can contain a plain
@themefor static tokens such as breakpoints and font stacks, and a separate@theme inlinefor variable-backed tokens. Both feed the same theme. - Namespaces still decide the utilities.
--color-surfacecreatesbg-surface,text-surfaceand the other color utilities because of the--color-*namespace.inlinedoes not change which utilities exist.
| Situation | Plain `@theme` | `@theme inline` |
|---|---|---|
| Literal value (`#3f3cbb`) | Works | Same result |
| References a variable defined on `:root`, never overridden | Works | Works |
| References a variable overridden on a descendant | Override ignored | Override applies |
| References a variable defined below `:root` | Value invalid on root | Resolved on each element |
| `var(--color-x)` available in custom CSS | Yes | Not guaranteed |
Exercise
A stylesheet has :root { --accent: red; }, .promo { --accent: green; } and @theme { --color-accent: var(--accent); }. An element with class="text-accent" sits inside a <section class="promo">. What color is the text, and what single change makes it use the other color?
Show answer
The text is red. With a plain @theme, --color-accent is resolved on :root and the already-resolved red is inherited by descendants, so the .promo override of --accent is not seen. Changing @theme to @theme inline makes text-accent compile to color: var(--accent), which is resolved on the element and gives green.
Exercise
You use @theme inline { --color-surface: var(--surface); }. A teammate writes .card { background: var(--color-surface); } in custom CSS and the background is missing. Why, and what should the rule use instead?
Show answer
With inline, utilities contain var(--surface) directly, so Tailwind has no need to emit --color-surface in the output, and the custom CSS refers to a variable that may not exist. The rule should use var(--surface), the variable that is always defined, or the theme should be declared with @theme static if --color-surface must always be emitted.