Sub topic 3
Defining design tokens with @theme
This chapter assumes you know basic CSS, including custom properties and var(). It also assumes you have used Tailwind utility classes in HTML and know how to import Tailwind into a CSS file with @import "tailwindcss";.
What a design token is
A design token is a named design decision, such as a brand color, a heading font, or the width at which a layout changes. Tokens keep those decisions in one place, so a change to one value updates every element that uses it.
Tailwind CSS v4 defines tokens in your CSS file with the @theme directive. Each token is a CSS custom property inside a @theme block. Tailwind reads those properties and does two things with them:
- It generates utility classes and variants that use the values.
- It emits the values as CSS variables, so your own CSS can read them.
Tailwind v3 kept this information in a JavaScript file, tailwind.config.js. In v4 the configuration lives in CSS, next to the rest of your styles.
A first @theme block
@import "tailwindcss";
@theme {
--font-display: "Satoshi", "sans-serif";
--breakpoint-3xl: 120rem;
--color-avocado-100: oklch(0.99 0 0);
--color-avocado-500: oklch(0.84 0.18 117.33);
--ease-fluid: cubic-bezier(0.3, 0, 0, 1);
}This block adds five tokens to the default theme that @import "tailwindcss" already provides. Each token is a normal custom property declaration, so the syntax is the one you already know.
You can use the new tokens in markup straight away:
<div class="bg-avocado-500 font-display 3xl:grid-cols-6 grid grid-cols-2">
<p class="text-avocado-100">Themed content</p>
</div>None of these classes existed before you wrote the @theme block. bg-avocado-500, font-display, text-avocado-100 and the 3xl: variant all come from the five variables above. This is the main difference between @theme and a plain :root block. A @theme variable changes what Tailwind can generate. A :root variable only exists as a CSS value.
How a name becomes a class
The name of a theme variable has two parts: a namespace prefix and a key.
--color-avocado-500has the namespace--color-and the keyavocado-500.--font-displayhas the namespace--font-and the keydisplay.--breakpoint-3xlhas the namespace--breakpoint-and the key3xl.
The namespace decides which utilities or variants Tailwind builds. The key becomes the part of the class name that you write. The next chapter covers every namespace in detail. This table shows the ones used above.
| Variable | Namespace | What Tailwind generates |
|---|---|---|
| `--color-avocado-500` | `--color-` | `bg-avocado-500`, `text-avocado-500`, `border-avocado-500`, and other color utilities |
| `--font-display` | `--font-` | `font-display` |
| `--breakpoint-3xl` | `--breakpoint-` | the `3xl:` variant |
| `--ease-fluid` | `--ease-` | `ease-fluid` |
A variable whose name does not start with a known namespace still becomes a CSS variable. It generates no utility. That fits values you only want to read with var(), but a misspelled prefix is a common cause of a missing class. If bg-avocado-500 does nothing, check that the variable starts with --color- and not --colour- or --colors-.
The diagram below shows the whole path from declaration to output.
Rules for writing @theme
Follow these rules, because Tailwind enforces them when it builds your CSS.
- Top level only.
@thememust sit at the top level of the stylesheet. You cannot put it inside a selector or a media query. Tailwind needs to know the tokens before it generates any class, so a conditional definition would make no sense. - Plain values. Theme variables hold values, not rules. Choose a value that a CSS custom property can hold: a color, a length, a font stack, a timing function.
- Several blocks are fine. You can write more than one
@themeblock. Tailwind merges them in order, and a later declaration of the same name replaces an earlier one. This lets you split tokens across files. - Same-name variables replace defaults. If you declare
--color-red-500, your value replaces the defaultred-500. The other default colors stay.
The last point is how you tune the default theme without rebuilding it. To change a single value, redeclare that variable. The next chapters show how to add to a namespace and how to remove one entirely.
@import "tailwindcss";
@theme {
--color-red-500: oklch(0.6 0.22 25);
}After this, bg-red-500 uses your red. bg-blue-500 and the rest of the default palette are unchanged.
Using the variables in your own CSS
Theme variables are ordinary CSS variables in the generated stylesheet, so custom CSS can read them with var() and stay consistent with the utilities:
.card {
padding: calc(var(--spacing) * 4);
background: var(--color-avocado-100);
border-radius: var(--radius-lg);
}--spacing and --radius-lg come from Tailwind's default theme, and --color-avocado-100 comes from your own block. The rule .card therefore changes whenever the theme changes. Later chapters cover the spacing variable and how Tailwind decides which variables to emit.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `bg-brand` has no effect | The variable is named `--brand` or `--colors-brand` | Rename it to `--color-brand` |
| `3xl:grid-cols-6` has no effect | The variable is `--screen-3xl` or sits outside the `--breakpoint-` namespace | Use `--breakpoint-3xl` |
| Build error about `@theme` | The block is inside a selector or `@media` | Move it to the top level of the stylesheet |
| A token works in `var()` but not as a class | The name has no recognized namespace | Add the right namespace prefix |
| A variable is missing from the browser's `:root` | Nothing in the project uses it | Use it somewhere, or see the `static` option |
Check the first row when a class does not work. Most mistakes in new theme code are naming mistakes, not syntax mistakes.
Choosing token names
Choose names that describe the role or the design language, not the current value. --color-brand survives a redesign. --color-purple does not, if the brand changes to green. For scales, follow the numeric pattern used by the default palette (100 to 900 or 950) so your team can guess class names.
Keep related tokens together in the block and add CSS comments to group them. Comments are allowed inside @theme like anywhere else in CSS.
@theme {
/* Typography */
--font-display: "Satoshi", "sans-serif";
/* Brand colors */
--color-avocado-100: oklch(0.99 0 0);
--color-avocado-500: oklch(0.84 0.18 117.33);
/* Motion */
--ease-fluid: cubic-bezier(0.3, 0, 0, 1);
}Exercise
You add --colour-sky-400: #38bdf8; inside @theme, but bg-sky-400 does not use it. What is wrong, and how do you fix it?
Show answer
The variable name uses the namespace --colour-, which Tailwind does not recognize. Color utilities are generated only from variables in the --color- namespace. Rename it to --color-sky-400, and bg-sky-400, text-sky-400 and the other color utilities will use the value.
Exercise
Why does this fail, and what should you do instead? @media (min-width: 64rem) { @theme { --color-brand: #3f3cbb; } }
Show answer
@theme must be at the top level of the stylesheet, not inside a media query or a selector. Tailwind has to know every token before it generates classes. Declare the token at the top level. If the value must change under a condition, keep a regular CSS variable for the changing part and reference it from the theme.