Sub topic 12
Customizing the theme
This chapter assumes you can build React components with props and JSX. It also assumes you have a Vite project with Tailwind CSS v4 installed, as shown in the setup chapter, and that you know how CSS custom properties (variables) work. By the end you will define your own design tokens, use the utilities they generate, and avoid the common mistakes that make a theme silently fail.
What a theme is in Tailwind v4
A design token is a named value, such as a brand color or a font family, that you reuse across the whole interface. In Tailwind v4 the theme is the set of tokens that Tailwind turns into utility classes.
Earlier versions of Tailwind kept the theme in a JavaScript file named tailwind.config.js. Version 4 moves it into your CSS. You declare tokens with the @theme directive. Each token is a CSS variable whose name starts with a namespace prefix, and the prefix decides which utilities Tailwind generates.
@import "tailwindcss";
@theme {
--color-brand-500: #3b82f6;
--font-display: "Inter", sans-serif;
}This block does two things:
- It creates the utilities
bg-brand-500,text-brand-500,border-brand-500, and every other color utility for the namebrand-500. - It creates the utility
font-display, which setsfont-familyto the Inter stack.
Tailwind also emits the values as real CSS variables on :root, so you can read them from plain CSS or from inline styles with var(--color-brand-500).
Namespaces decide the utilities
The part of the variable name before your own name is the namespace. Use the wrong namespace and no utility appears.
| Namespace | Example variable | Utilities it creates |
|---|---|---|
| `--color-*` | `--color-brand-500` | `bg-brand-500`, `text-brand-500`, `border-brand-500`, `ring-brand-500` |
| `--font-*` | `--font-display` | `font-display` |
| `--text-*` | `--text-huge` | `text-huge` (font size) |
| `--spacing` | `--spacing: 0.25rem` | `p-4`, `m-2`, `w-10` and other spacing-based utilities |
| `--breakpoint-*` | `--breakpoint-3xl` | `3xl:` variant |
| `--radius-*` | `--radius-card` | `rounded-card` |
| `--shadow-*` | `--shadow-soft` | `shadow-soft` |
The --spacing variable is a special case. It is a single base unit, and numeric utilities multiply it. With the default of 0.25rem, p-4 means 1rem. If you set --spacing: 0.5rem, then p-4 becomes 2rem everywhere.
Extending the default theme
Anything you put inside @theme is added to the default theme. Names that already exist are overridden, and other defaults stay. So the example above keeps bg-blue-500 and bg-red-500 working and adds bg-brand-500 beside them.
A more realistic setup defines a color scale, so you can write bg-brand-50 for a tint and bg-brand-700 for a hover state.
@import "tailwindcss";
@theme {
--color-brand-50: #eff6ff;
--color-brand-100: #dbeafe;
--color-brand-500: #3b82f6;
--color-brand-600: #2563eb;
--color-brand-700: #1d4ed8;
--font-display: "Inter", sans-serif;
--radius-card: 0.75rem;
--shadow-soft: 0 2px 8px rgb(0 0 0 / 0.08);
--breakpoint-3xl: 120rem;
}You can now use those tokens in a component like any built-in utility.
export function PromoCard({ title }: { title: string }) {
return (
<section className="rounded-card bg-brand-50 p-6 shadow-soft 3xl:p-10">
<h2 className="font-display text-xl font-semibold text-brand-700">
{title}
</h2>
<button className="mt-4 rounded-md bg-brand-500 px-4 py-2 text-white hover:bg-brand-600">
Learn more
</button>
</section>
)
}The component never mentions a hex code. If the brand color changes, you edit one line in the stylesheet.
Replacing a whole namespace
Sometimes you want only your colors, not Tailwind's palette. Set the namespace wildcard to initial first. This removes every default in that namespace.
@import "tailwindcss";
@theme {
--color-*: initial;
--color-white: #ffffff;
--color-black: #000000;
--color-brand-500: #3b82f6;
--color-ink: #111827;
}After this, bg-blue-500 no longer exists. Tailwind generates no CSS for it, and the class has no effect. Using --*: initial; clears the entire default theme, which is rarely what you want.
How tokens reach the browser
Understanding the output helps when you debug. Tailwind reads your @theme block, then writes the variables into the generated stylesheet. A utility such as bg-brand-500 compiles to a rule that reads the variable.
Two points follow from this flow:
- A token only produces a utility if the class name appears in your source. Defining a hundred colors does not bloat the stylesheet.
- Because utilities read a CSS variable, you can change a value at run time. Overriding
--color-brand-500inside a.theme-greenancestor re-colors everybg-brand-500below it, with no JavaScript class swapping.
Referencing other variables
Use @theme inline when a token's value points to another variable. Without inline, Tailwind writes var(--other) inside the utility, and the variable is resolved at the place where :root defines it. That can be the wrong place if you redefine the other variable lower in the tree.
@import "tailwindcss";
:root {
--surface: #ffffff;
}
.dark {
--surface: #0f172a;
}
@theme inline {
--color-surface: var(--surface);
}With this setup, bg-surface follows whichever --surface is active at the element. A .dark ancestor switches the color without a dark: variant on every element. This pattern is common for semantic tokens such as surface, muted, and accent, where the name describes the role and not the shade.
Choosing between theme tokens and other approaches
Not every value belongs in the theme. Use this guide when you decide.
| Situation | Best choice |
|---|---|
| A value used in many places, such as a brand color | `@theme` token |
| A value that changes by context, such as light and dark surfaces | Semantic variable plus `@theme inline` |
| A one-off size or color used once | Arbitrary value, such as `w-[320px]` |
| A repeated combination of utilities | A React component |
The last row matters. Do not create a token for every pattern you repeat. If five buttons share the same padding, color, and radius, extract a Button component and keep the tokens for the underlying values.
Common errors
- Wrong namespace.
--brand-500: #3b82f6;creates a CSS variable but no utility, because--brand-is not a namespace Tailwind knows. Use--color-brand-500. - Defining tokens outside
@theme. A variable in:rootis a normal CSS variable. It does not createbg-*utilities. Put design tokens in@theme. - Using
@themeinside a selector or media query. The directive is top level only. For values that change by context, define plain variables in the selector and reference them from an@theme inlinetoken. - Forgetting the import order.
@import "tailwindcss";must come first, since@themeextends what it loads. - Expecting a font to load.
--font-display: "Inter", sans-serif;sets the font stack. It does not download Inter. Load the font with a<link>tag,@font-face, or a font package. - Building token names dynamically.
text-${size}in a template string is not detected, as covered in the chapter on class detection. Map each value to a full class name.
Putting it together
A small theme with a semantic layer shows the main tools in one file.
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));
:root {
--surface: #ffffff;
--ink: #111827;
}
.dark {
--surface: #0f172a;
--ink: #f1f5f9;
}
@theme inline {
--color-surface: var(--surface);
--color-ink: var(--ink);
}
@theme {
--color-brand-500: #3b82f6;
--color-brand-600: #2563eb;
--font-display: "Inter", sans-serif;
}export function Panel({ children }: { children: React.ReactNode }) {
return (
<div className="bg-surface p-6 text-ink">
<h2 className="font-display text-lg font-semibold text-brand-600">
Account
</h2>
{children}
</div>
)
}Panel has no dark: classes. The semantic tokens handle both modes, and toggling the dark class on an ancestor from React is enough.
Exercise
You add --brand-500: #3b82f6; inside @theme and write <div className="bg-brand-500">. The background does not change. What is wrong, and what is the fix?
Show answer
The variable uses the namespace --brand-, which Tailwind does not recognize, so it generates no bg-brand-500 utility. Rename it to --color-brand-500. The --color- prefix tells Tailwind to create color utilities such as bg-, text-, and border-.
Exercise
You want p-4 to equal 1.25rem across the whole app. Which theme change achieves this, and why does it affect other utilities too?
Show answer
Set --spacing: 0.3125rem; in @theme, because p-4 is four times the base unit and 4 × 0.3125rem is 1.25rem. Padding, margin, gap, width, and height utilities all multiply the same --spacing variable, so they change together.
Exercise
A token --color-surface: var(--surface); inside a plain @theme block does not follow a .dark ancestor that redefines --surface. What change fixes it?
Show answer
Use @theme inline { --color-surface: var(--surface); }. With inline, the utility reads --surface at the element where it is used, so the nearest redefinition under .dark applies.