Sub topic 10
Using theme variables in your own CSS
This chapter assumes you can write basic CSS with var(), you have used Tailwind utility classes, and you know how to import Tailwind with @import "tailwindcss";. It also assumes you have defined tokens with @theme, as in the earlier chapters.
Theme variables are real CSS variables
Tailwind turns every @theme entry into a custom property on :root. Utility classes read these properties, and your own CSS can read them too. Because both use the same source, a card styled by hand and a card styled with utilities stay visually consistent. Changing the token changes both.
@import "tailwindcss";
@theme {
--color-avocado-100: oklch(0.99 0 0);
--color-avocado-500: oklch(0.84 0.18 117.33);
--radius-lg: 0.5rem;
}
.card {
padding: calc(var(--spacing) * 4);
background: var(--color-avocado-100);
border: 1px solid var(--color-avocado-500);
border-radius: var(--radius-lg);
}Tailwind's default theme already defines --radius-lg and --spacing. The redefinition above overrides the default radius. --color-avocado-100 and --color-avocado-500 are new tokens.
Choosing between a utility and var()
Prefer utilities in markup. Reach for var() when utilities are awkward:
- Styles for elements you cannot add classes to, such as rendered Markdown or third-party widgets.
- Component classes shared by many elements.
- Complex selectors, pseudo-elements, or multi-property rules that would need a long class list.
- Values computed with
calc(),color-mix(), or gradients.
If a few classes in a template would do the job, utilities are usually the simpler choice.
Reading variables by namespace
The variable name follows its namespace. These are the namespaces you will read most often from custom CSS.
| Namespace | Example | Typical use in custom CSS |
|---|---|---|
| `--color-*` | `var(--color-avocado-500)` | `color`, `background`, `border-color` |
| `--font-*` | `var(--font-display)` | `font-family` |
| `--text-*` | `var(--text-lg)` | `font-size` |
| `--radius-*` | `var(--radius-lg)` | `border-radius` |
| `--shadow-*` | `var(--shadow-md)` | `box-shadow` |
| `--ease-*` | `var(--ease-fluid)` | `transition-timing-function` |
| `--breakpoint-*` | `theme(--breakpoint-3xl)` | `@media` queries (see below) |
Some namespaces have companion variables. For example, --text-lg is a font size, and Tailwind also defines --text-lg--line-height. Read both if you want the same pairing the text-lg utility produces:
.lead {
font-size: var(--text-lg);
line-height: var(--text-lg--line-height);
}Doing math with --spacing
Tailwind defines one base value, --spacing, and the spacing utilities multiply it. The class p-4 is calc(var(--spacing) * 4). Use the same calculation in custom CSS and your values land on the same scale:
.panel {
margin-block: calc(var(--spacing) * 6);
padding-inline: calc(var(--spacing) * 4);
gap: calc(var(--spacing) * 2);
}If you later change --spacing in @theme, every utility and every .panel rule scales together. Recent v4 releases also provide a --spacing() function that compiles to this same calc(). The explicit calc() works in every v4 release, so this chapter uses it.
Put component CSS in a layer
Tailwind sorts its output into cascade layers. Custom CSS written outside any layer beats every utility. A utility such as p-8 then cannot override your .card padding. Put component styles in @layer components and utilities can override them:
@layer components {
.card {
padding: calc(var(--spacing) * 4);
background: var(--color-avocado-100);
border-radius: var(--radius-lg);
}
}<div class="card p-8">Padding comes from p-8, not from .card.</div>Use an unlayered rule only when you want it to win over utilities.
Fallbacks and unused variables
Tailwind includes a theme variable in the output only when something uses it. A utility class in your templates counts as a use. A var() reference in your CSS counts as well, so the card example above keeps --color-avocado-100 in the output.
That rule creates one failure mode. If you build the variable name at runtime, for example in JavaScript with ` var(--color-${name}) `, Tailwind cannot see the reference. The variable may be missing from the output. There are two fixes:
- Use
@theme staticso every variable is always emitted. A later chapter covers it. - Write the full
var(--color-avocado-500)somewhere in your CSS or source files so Tailwind can detect it.
A var() call also accepts a fallback as its second argument. The fallback applies if the variable is missing at runtime:
.badge {
background: var(--color-brand, #3f3cbb);
}A fallback is a safety net. It does not replace defining the token.
Where var() cannot go: media queries
CSS does not allow var() inside a media query condition. @media (min-width: var(--breakpoint-3xl)) is invalid. Tailwind provides the theme() function for this case. It is resolved at build time, and the value is written directly into the output:
@theme {
--breakpoint-3xl: 120rem;
}
.sidebar {
display: none;
}
@media (width >= theme(--breakpoint-3xl)) {
.sidebar {
display: block;
}
}Use theme() only where var() is not allowed. Everywhere else, var() is better because it follows runtime changes to the variable.
Variables inside arbitrary values
You can also read theme variables from markup without writing any CSS. In v4, a variable in parentheses is shorthand for var() inside an arbitrary value:
<div class="bg-(--color-avocado-500) rounded-(--radius-lg) p-4">
Uses theme variables directly.
</div>This is useful for a one-off. If you repeat it, a named utility such as bg-avocado-500 is cleaner.
Adjusting a color with color-mix and --alpha
Theme colors are plain CSS values, so you can combine them with CSS color functions. color-mix() works in browsers that support it:
@layer components {
.highlight {
background: color-mix(in oklab, var(--color-avocado-500) 20%, transparent);
}
}Tailwind also offers --alpha(), which compiles to a similar color-mix() call:
@layer components {
.highlight {
background: --alpha(var(--color-avocado-500) / 20%);
}
}Both approaches keep the color tied to the token, so a palette change updates the highlight automatically.
Common errors
- Typing the wrong name.
var(--color-avocado500)is a valid reference to a variable that does not exist. The property silently falls back to its inherited or initial value. Check the browser's dev tools for a crossed-out declaration. - Reading a variable you reset. If you set
--color-*: initial;in@theme, any custom CSS that reads a default color such asvar(--color-red-500)now refers to a missing variable. Search your CSS before resetting a namespace. - Defining the variable in
:rootand expecting a utility. A plain:rootvariable never creates utilities. Define it in@themeif you want a class such asbg-brand. - Unlayered custom CSS overriding utilities. If
p-8seems to be ignored, check whether your rule is outside@layer components.
Exercise
You define --color-brand: #3f3cbb; in @theme. Write a .button rule in @layer components that uses it as the background, and add a hover state that mixes 80% of the brand color with transparent.
Show answer
@layer components { .button { background: var(--color-brand); } .button:hover { background: color-mix(in oklab, var(--color-brand) 80%, transparent); } }
Exercise
Why does @media (min-width: var(--breakpoint-3xl)) not work, and what do you write instead?
Show answer
CSS does not allow var() in media query conditions. Use Tailwind's build-time function instead: @media (width >= theme(--breakpoint-3xl)).
Exercise
A .panel rule must have the same padding as p-6. What declaration do you write?
Show answer
padding: calc(var(--spacing) * 6);