Sub topic 9
Customize design tokens with @theme
This chapter assumes you have a working Vite project with tailwindcss and @tailwindcss/vite installed. It also assumes the plugin is registered in vite.config.ts and your stylesheet starts with @import "tailwindcss";. You should know basic CSS custom properties (--name: value and var(--name)).
What a design token is in Tailwind v4
A design token is a named design value, such as a brand color, a font family, or a breakpoint width. In Tailwind v3 you declared tokens in tailwind.config.js under theme.extend. In v4 you declare them in CSS with the @theme directive.
Each declaration inside @theme is a theme variable. A theme variable is a CSS custom property whose name tells Tailwind which utilities to generate. The part of the name before your own key is the namespace.
@import "tailwindcss";
@theme {
--color-brand: #3b82f6;
--font-display: "Georgia", serif;
}This block does two things:
- It makes the utilities
bg-brand,text-brand,border-brand,fill-brandand so on available, because--color-*is the color namespace. - It makes
font-displayavailable, because--font-*is the font family namespace.
Tailwind also exposes the values as real CSS variables, so you can write color: var(--color-brand) in your own CSS.
Try it in your project
Open your main CSS file, for example src/style.css, and add the @theme block shown above. Then use the new utilities in your entry file.
import './style.css'
document.querySelector('#app').innerHTML = `
<h1 class="font-display text-3xl font-bold text-brand">Hello Tailwind</h1>
<button class="mt-4 rounded bg-brand px-4 py-2 text-white">Save</button>
`With the dev server running (npm run dev), the heading should render in Georgia and the button should have the brand blue background. Edit --color-brand to #e11d48 and save. Vite's hot module replacement updates the page without a reload.
Naming rules: the namespace decides the utility
The variable name is not decoration. Tailwind matches the prefix to a namespace, and the rest of the name becomes the utility's suffix.
| Namespace | Example variable | Utilities it creates |
|---|---|---|
| `--color-*` | `--color-brand` | `bg-brand`, `text-brand`, `border-brand` |
| `--font-*` | `--font-display` | `font-display` |
| `--text-*` | `--text-huge` | `text-huge` (font size) |
| `--font-weight-*` | `--font-weight-heavy` | `font-heavy` |
| `--breakpoint-*` | `--breakpoint-3xl` | `3xl:` variant |
| `--radius-*` | `--radius-pill` | `rounded-pill` |
| `--shadow-*` | `--shadow-card` | `shadow-card` |
| `--spacing` | `--spacing` | base unit for `p-4`, `m-2`, `w-8` and similar |
A variable that matches no namespace still becomes a CSS variable, but no utility is generated for it. If bg-brand does nothing, check first that the name starts with --color-. A name such as --brand-color will not work.
Adding a full scale of tokens
You can define as many values as you need. A common pattern is a color scale with numeric suffixes.
@import "tailwindcss";
@theme {
--color-brand-50: #eff6ff;
--color-brand-500: #3b82f6;
--color-brand-900: #1e3a8a;
--font-display: "Georgia", serif;
--breakpoint-3xl: 120rem;
--radius-pill: 9999px;
--shadow-card: 0 1px 3px rgb(0 0 0 / 0.15);
}This yields bg-brand-50, text-brand-900, 3xl:grid-cols-4, rounded-pill and shadow-card. Opacity modifiers also work on custom colors, so bg-brand-500/50 gives a half-transparent version.
Extending versus replacing the defaults
By default, @theme adds to Tailwind's built-in theme. Your --color-brand sits alongside bg-red-500 and the rest. If you define a variable that already exists, yours wins. For example, setting --color-blue-500 changes what bg-blue-500 produces.
To remove defaults, set a namespace to initial:
@import "tailwindcss";
@theme {
--color-*: initial;
--color-white: #ffffff;
--color-black: #000000;
--color-brand: #3b82f6;
}After this, only white, black and brand exist as colors. Classes like bg-red-500 generate nothing. To clear every default token at once, use --*: initial; and define everything yourself.
Referencing other variables with @theme inline
Sometimes a theme variable should point at another CSS variable, for instance one set by a font loader or a theme switcher.
@import "tailwindcss";
:root {
--app-accent: #0ea5e9;
}
@theme inline {
--color-accent: var(--app-accent);
}The inline option tells Tailwind to put the referenced value directly into the generated utility, so bg-accent becomes background-color: var(--app-accent). Without inline, the utility refers to var(--color-accent), and that variable is resolved where it is defined, on the root. That can give the wrong result if --app-accent is overridden on a nested element. Use inline when the value depends on another variable that may change below the root.
Which variables appear in the CSS output
Tailwind v4 emits a theme variable into the final CSS only if some utility or your own CSS uses it. This keeps the stylesheet small. If you want a variable always present, for example because JavaScript reads it with getComputedStyle, use static:
@import "tailwindcss";
@theme static {
--color-brand: #3b82f6;
}With @theme static, --color-brand is written to the output even when no class uses it. static and inline can be combined as @theme static inline.
Rules and limits of @theme
@thememust be at the top level of the stylesheet. You cannot nest it inside a selector or inside@media.- Tokens are global. For values that change by context, such as a dark mode palette, define ordinary CSS variables under a selector and reference them with
@theme inline. - Tokens used in plain CSS do not need
@apply. You can writeborder: 1px solid var(--color-brand);anywhere after the import. - If you split styles across files, each file that you want to use theme variables in must be reachable from the main stylesheet through
@import, or Tailwind will not know about the tokens.
Common problems with tokens
- The utility does nothing. The name does not match a namespace. Check the prefix, such as
--color-or--font-. - The change does not show up. Confirm that you edited the stylesheet that the app actually imports. Then check the terminal for a CSS error, such as an unclosed brace.
- A default color disappeared. A
--color-*: initial;reset removed it. Add the token back or remove the reset. - A variable is missing from DevTools. It is unused, so Tailwind did not emit it. Use
@theme staticif you need it. - A v3
theme.extendconfig is ignored. v4 does not readtailwind.config.jsautomatically. Move the values into@theme. You can load a legacy file with@config, but new projects should use CSS.
Check your understanding
Exercise
You add --brand-color: #3b82f6; inside @theme, but the class bg-brand produces no background. What is wrong, and what should the declaration be so that bg-brand works?
Show answer
The name does not start with a namespace prefix Tailwind recognizes. Background colors come from the --color-* namespace, so the variable must be --color-brand: #3b82f6;. With that name, Tailwind generates bg-brand, text-brand, and the other color utilities.
Exercise
A script reads getComputedStyle(document.documentElement).getPropertyValue('--color-brand') but gets an empty string, even though --color-brand is defined in @theme and no class in your markup uses it. How do you fix it?
Show answer
Tailwind v4 only emits theme variables that are used. Declare the variable with @theme static { ... } so it is always written to the generated CSS and the script can read it.
Exercise
You want only white, black, and a brand color in your project, with none of Tailwind's default palette. Write the @theme block.
Show answer
@theme { --color-*: initial; --color-white: #ffffff; --color-black: #000000; --color-brand: #3b82f6; }