Sub topic 9
Customize design tokens with @theme
This chapter assumes you have a working Vite project with Tailwind CSS v4 installed. That means @tailwindcss/vite is registered in vite.config.ts, @import "tailwindcss"; is in your main CSS file, and utility classes already render in the dev server. You should also be comfortable with CSS custom properties such as --my-var.
What a design token is here
A design token is a named value, such as a color, font, or spacing step, that your whole interface reuses. In Tailwind v3 you defined tokens in tailwind.config.js. In v4 you define them in CSS with the @theme directive. There is no config file for this in a typical project.
Define tokens in your CSS
Add an @theme block to the same CSS file that imports Tailwind:
@import "tailwindcss";
@theme {
--color-brand: #3b82f6;
--font-display: "Georgia", serif;
}Tailwind reads each variable in the block and generates utilities from it. After saving, these classes work:
bg-brand,text-brand, andborder-brandcome from--color-brand.font-displaycomes from--font-display.
Here is a small use of those utilities:
document.querySelector('#app').innerHTML = `
<h1 class="font-display text-3xl text-brand">Custom tokens</h1>
<button class="bg-brand px-4 py-2 text-white">Save</button>
`The heading should now appear in Georgia and in your blue. The dev server reloads the styles when you save the CSS file.
How variable names map to utilities
The prefix of a variable name, called its namespace, decides which utilities Tailwind creates. The rest of the name becomes the utility suffix.
| Variable | Utilities it creates |
|---|---|
| `--color-brand` | `bg-brand`, `text-brand`, `border-brand` |
| `--font-display` | `font-display` |
| `--spacing-gutter` | `p-gutter`, `m-gutter`, `gap-gutter` |
| `--breakpoint-3xl` | `3xl:` variant |
A variable outside a known namespace does not create a utility. Check the spelling of the prefix first when a class does nothing.
Extend or replace the defaults
Variables you add in @theme sit alongside Tailwind's defaults. --color-brand adds a color, and bg-blue-500 still works.
To remove a whole namespace of defaults, set it to initial:
@import "tailwindcss";
@theme {
--color-*: initial;
--color-brand: #3b82f6;
--color-ink: #111827;
}Now bg-brand and bg-ink exist, but bg-red-500 does not. Use this when you want a strict palette.
Tokens are also CSS variables
Tailwind emits every theme value as a real CSS variable on :root. You can read it in your own CSS:
.card {
border: 1px solid var(--color-brand);
font-family: var(--font-display);
}This is useful for custom components, inline styles, and JavaScript that calls getComputedStyle.
Common mistakes
- Writing
--brandinstead of--color-brand. No namespace means no utilities. - Putting tokens in
:rootinstead of@theme. A plain:rootvariable works withvar(), but Tailwind does not generate a utility from it. - Defining the block in a separate CSS file that nothing imports. Tailwind only sees
@themein files that reach the build through your stylesheet.
Exercise
You add --color-accent: #f97316; inside @theme, and bg-accent works. Later you write --accent-soft: #fdba74; in the same block and bg-accent-soft does nothing. What is wrong, and how do you fix it?
Show answer
--accent-soft has no recognized namespace prefix, so Tailwind creates no utility from it. Rename it to --color-accent-soft, which makes bg-accent-soft and text-accent-soft available.
Exercise
How do you make bg-red-500 stop working while keeping a custom bg-brand?
Show answer
In @theme, set --color-*: initial; to clear the default color namespace, then define --color-brand. Only the colors you define remain.