Sub topic 10
Using theme variables in your own CSS
This chapter assumes you can define tokens with @theme and know how namespaces map to utilities. Here you will read those tokens from your own CSS rules.
Theme variables are real CSS variables
Tailwind turns each @theme entry into a custom property on :root in the generated stylesheet. Your own CSS can read it with var(), the same way it reads any other custom property. Utilities and custom rules then share one source of truth.
Reading tokens in a custom rule
Define the tokens, then use them in a class that Tailwind does not provide.
@import "tailwindcss";
@theme {
--color-avocado-100: oklch(0.99 0 0);
--radius-lg: 0.5rem;
}
.card {
padding: calc(var(--spacing) * 4);
background: var(--color-avocado-100);
border-radius: var(--radius-lg);
}The padding line works because --spacing is the base unit of the spacing scale. Multiplying it by 4 gives the same value as the p-4 utility. If you later change --spacing, both the utilities and .card change together.
Which variables you can read
Any variable from a namespace can be read. Common ones are:
| Variable | Holds |
|---|---|
| `--color-*` | Colors such as `--color-avocado-500` |
| `--font-*` | Font families such as `--font-display` |
| `--radius-*` | Border radii such as `--radius-lg` |
| `--spacing` | The base unit for the spacing scale |
| `--ease-*` | Easing curves such as `--ease-fluid` |
Only used variables reach the output
By default Tailwind emits only the theme variables that are used. A variable counts as used when a utility needs it or when your CSS references it with var(). If you reference --color-avocado-100 in .card, Tailwind keeps it in the output.
Variables you read only from JavaScript, or from CSS that Tailwind does not process, may be left out. In that case, declare them with @theme static so they are always emitted. Chapter material on static covers this in full.
Using variables in markup
You can also reference a theme variable from an arbitrary value in a class. Tailwind v4 accepts the shorthand with parentheses.
<div class="bg-(--color-avocado-500) rounded-(--radius-lg) p-4">
Uses theme variables directly
</div>Prefer the generated utility (bg-avocado-500) when one exists. Use the var() form for properties that have no utility, or in rules inside @layer components.
Common errors
- Wrong name. Writing
var(--colour-brand)or omitting the namespace prefix gives an empty value, and the browser ignores the declaration. Check the exact variable name in your@theme. - Reading a reset variable. After
--color-*: initial;, default variables such as--color-red-500no longer exist. Anyvar()that reads them fails. - Defining in the wrong place. A variable declared in
:rootinstead of@themecan be read withvar(), but it creates no utilities. Use@themewhen you want both.
Exercise
A rule uses padding: calc(var(--spacing) * 6);. Which utility gives the same padding, and what happens to this rule if you set --spacing: 0.5rem in @theme?
Show answer
The utility is p-6. With --spacing: 0.5rem, the rule computes 3rem of padding, and p-6 computes the same 3rem, because both are derived from the same base value.