Sub topic 12
Customizing the theme
This chapter assumes you can build a Vite and React project with Tailwind v4 installed, and that you know the core utilities for spacing, color and typography. Here you will define your own design tokens so your components use project names like brand instead of raw values.
Theme variables in Tailwind v4
Tailwind v4 has no tailwind.config.js for theming. You declare design tokens in CSS with the @theme directive. Each token is a CSS variable whose name follows a namespace. Tailwind reads the namespace and generates matching utilities.
@import "tailwindcss";
@theme {
--color-brand-500: #3b82f6;
--font-display: "Inter", sans-serif;
}This block produces bg-brand-500, text-brand-500, border-brand-500 and font-display. It also exposes var(--color-brand-500) as a normal CSS variable at runtime, so plain CSS and inline styles can read it.
export function Hero() {
return (
<section className="bg-brand-500 p-8 text-white">
<h1 className="font-display text-3xl">Welcome</h1>
</section>
)
}How namespaces map to utilities
The prefix of the variable name decides which utilities appear.
| Namespace | Example variable | Utilities generated |
|---|---|---|
| `--color-*` | `--color-brand-500` | `bg-brand-500`, `text-brand-500`, `border-brand-500` |
| `--font-*` | `--font-display` | `font-display` |
| `--text-*` | `--text-huge` | `text-huge` |
| `--spacing-*` | `--spacing-gutter` | `p-gutter`, `mx-gutter`, `gap-gutter` |
| `--breakpoint-*` | `--breakpoint-3xl` | `3xl:` variant |
| `--radius-*` | `--radius-card` | `rounded-card` |
A variable outside these namespaces is still emitted as a CSS variable, but it does not create a utility.
Extending versus replacing the defaults
By default, @theme adds your tokens next to Tailwind's built-in ones. bg-blue-600 and bg-brand-500 both work.
To remove a whole namespace, set it to initial. Then only the colors you define exist.
@import "tailwindcss";
@theme {
--color-*: initial;
--color-white: #ffffff;
--color-ink: #111827;
--color-brand-500: #3b82f6;
--color-brand-600: #2563eb;
}To override a single default, redefine it with the same name, such as --breakpoint-sm.
Choosing names that describe purpose
Name tokens by role when your app has a stable visual identity. A role name lets you change a color in one place.
@import "tailwindcss";
@theme {
--color-primary: #2563eb;
--color-primary-hover: #1d4ed8;
--color-surface: #ffffff;
}export function PrimaryAction() {
return (
<button className="rounded-md bg-primary px-4 py-2 text-white hover:bg-primary-hover">
Continue
</button>
)
}Changing --color-primary now recolors every button. Keep a numbered scale such as brand-500 when you need several shades of one hue.
Common mistakes
- Putting
@themebefore the import. Place it after@import "tailwindcss";so your tokens merge with the defaults. - Using the wrong namespace.
--colour-brandor--brand-colorcreates nobg-brandutility. The prefix must be--color-. - Duplicating a token as an arbitrary value. Writing
bg-[#3b82f6]in ten components defeats the theme. Add it to@themeonce. - Defining tokens in a TypeScript file. Tailwind reads the CSS file. A constant in
.tsdoes not create utilities.
Exercise
You add --color-accent: #f59e0b; inside @theme. Which class do you use to give a div that background color, and which CSS expression reads the same value in a plain stylesheet rule?
Show answer
Use bg-accent on the div. In plain CSS, read the value with var(--color-accent).
Exercise
A teammate writes @theme { --brand-color: #3b82f6; } and finds that bg-brand-color does nothing. What is wrong, and what is the fix?
Show answer
The variable does not start with the --color- namespace, so Tailwind generates no color utilities from it. Rename it to --color-brand and use bg-brand.