Sub topic 12
Customizing the theme and design tokens
This chapter assumes you have a working Tailwind v4 project, know the default spacing and color scales, and understand how utilities are generated from the files Tailwind scans. You should also be comfortable with CSS custom properties (variables).
What a design token is
A design token is a named design decision, such as a brand color, a spacing step, or a font. Tailwind's default theme is a set of tokens. Examples are indigo-600, p-4, and rounded-xl. Customizing the theme means adding your own tokens or replacing the defaults, so that your utilities match your design system.
In Tailwind v4, you define tokens in CSS with the @theme directive. There is no JavaScript config file required for this.
Defining a token with @theme
Inside @theme, you declare CSS variables whose names use a recognized namespace. Tailwind reads the namespace and creates matching utilities.
@import "tailwindcss";
@theme {
--color-brand: #4f46e5;
}The --color-* namespace creates color utilities. This one declaration gives you bg-brand, text-brand, border-brand, ring-brand, and the other color utilities. You use them like any built-in color:
<button class="rounded-lg bg-brand px-4 py-2 text-white hover:bg-brand/90">
Subscribe
</button>The /90 opacity modifier also works on your custom color.
Common namespaces
| Namespace | Utilities it creates | Example |
|---|---|---|
| `--color-*` | `bg-*`, `text-*`, `border-*`, `ring-*` | `--color-brand` gives `bg-brand` |
| `--font-*` | `font-*` (font family) | `--font-display` gives `font-display` |
| `--text-*` | `text-*` (font size) | `--text-huge` gives `text-huge` |
| `--spacing` | The base unit for `p-*`, `m-*`, `w-*`, `gap-*` | `--spacing: 0.25rem` |
| `--breakpoint-*` | Responsive prefixes | `--breakpoint-3xl` gives `3xl:` |
| `--radius-*` | `rounded-*` | `--radius-card` gives `rounded-card` |
| `--shadow-*` | `shadow-*` | `--shadow-soft` gives `shadow-soft` |
A variable that does not use one of these namespaces is still emitted as a CSS variable, but it does not create a utility.
Extending versus replacing
Declaring a new name adds to the defaults. Declaring an existing name overrides that one token. To remove a whole namespace, set it to initial:
@import "tailwindcss";
@theme {
--color-*: initial;
--color-white: #ffffff;
--color-ink: #0f172a;
--color-brand: #4f46e5;
--color-brand-dark: #3730a3;
}Now bg-red-500 no longer exists. Only the four colors above do. This is useful when a design system should forbid off-palette colors.
Tokens are real CSS variables
Every @theme value is also exposed as a variable on :root. You can read it in your own CSS or inline styles:
.hero {
background: linear-gradient(var(--color-brand), var(--color-brand-dark));
}This is the main benefit of @theme over a plain :root block. A plain variable is available to CSS but creates no utility. A @theme variable does both.
flowchart LRTokens still follow the scanning rule from the earlier chapter. Defining --color-brand does not add bg-brand to the output until a scanned file contains that class name.
Adding a custom font and size
@import "tailwindcss";
@theme {
--font-display: "Poppins", sans-serif;
--text-huge: 4.5rem;
--breakpoint-3xl: 120rem;
}You can now write font-display, text-huge, and 3xl:grid-cols-4. Loading the Poppins font file (with @font-face or a <link>) is still your job. The token only names it.
Common errors
- Wrong namespace.
--brand-color: #4f46e5;creates nobg-brand-color. The name must start with--color-. - Theme values in the wrong place. Put
@themeat the top level of your CSS file, not inside a selector or media query. - Expecting an unused token in the output. A utility appears only after a scanned file uses it.
- Overriding by accident. Reusing a default name such as
--color-red-500changes it everywhere.
Exercise
You want a shadow-soft utility and a bg-surface utility. Write the @theme declarations, and explain why --surface: #f8fafc; would not give you bg-surface.
Show answer
Use @theme { --shadow-soft: 0 2px 8px rgb(0 0 0 / 0.08); --color-surface: #f8fafc; }. The variable --surface does not start with a recognized namespace such as --color-, so Tailwind emits it as a plain CSS variable but creates no utility.