Sub topic 12
Customizing the theme and design tokens
This chapter assumes you know basic HTML and core CSS (the box model, selectors, specificity). It also assumes you can install packages with npm and have already set up Tailwind CSS v4, as covered earlier in this tutorial. You should know the default utilities for spacing, color, and typography.
By the end, you will be able to add your own design tokens, override or remove default ones, and decide when a token is better than a one-off value.
What a design token is
A design token is a named value that your design system uses repeatedly. Examples are a brand color, a spacing step, a font family, or a breakpoint. Instead of writing #4f46e5 in fifty places, you name it once and refer to the name.
Tailwind's defaults are tokens. Names such as indigo-600, p-4, text-xl, and rounded-lg all point to values on a built-in scale. Customizing the theme means adding to that scale, changing it, or replacing it.
In Tailwind v4, tokens live in your CSS file as CSS custom properties (also called CSS variables). You declare them inside an @theme block. This replaces the theme section of tailwind.config.js that older versions used.
Adding a token with @theme
The smallest customization adds one color.
@import "tailwindcss";
@theme {
--color-brand: #4f46e5;
}Tailwind reads the variable name and creates utilities from it. The part after --color- becomes the utility's name. You can now write these classes:
bg-brandtext-brandborder-brandring-brandbg-brand/50(the brand color at 50% opacity)
You can use them with variants as well, for example hover:bg-brand or dark:text-brand.
<button class="rounded-lg bg-brand px-4 py-2 text-white hover:bg-brand/90">
Subscribe
</button>Namespaces decide which utilities exist
The prefix of a variable is its namespace. The namespace tells Tailwind which kind of utilities to generate. A variable outside a known namespace is just a CSS variable and creates no utilities.
| Namespace | Utilities it creates | Example variable |
|---|---|---|
| `--color-*` | `bg-*`, `text-*`, `border-*`, `fill-*`, and others | `--color-brand` |
| `--font-*` | `font-*` (font family) | `--font-display` |
| `--text-*` | `text-*` (font size) | `--text-huge` |
| `--spacing` | `p-*`, `m-*`, `gap-*`, `w-*`, `h-*`, and others | `--spacing: 0.25rem` |
| `--breakpoint-*` | Responsive variants such as `md:` | `--breakpoint-3xl` |
| `--radius-*` | `rounded-*` | `--radius-card` |
| `--shadow-*` | `shadow-*` | `--shadow-soft` |
The same --color-* namespace feeds many utilities. You define the color once, and every color-related utility gets it.
Building a small design system
A realistic theme adds several related tokens at once. This example defines a brand palette, a display font, a custom radius, and an extra breakpoint.
@import "tailwindcss";
@theme {
--color-brand-50: #eef2ff;
--color-brand-500: #6366f1;
--color-brand-600: #4f46e5;
--color-brand-700: #4338ca;
--font-display: "Satoshi", "Inter", sans-serif;
--radius-card: 0.75rem;
--breakpoint-3xl: 120rem;
}This gives you bg-brand-600, font-display, rounded-card, and a 3xl: responsive prefix. A card can now read:
<article class="rounded-card bg-brand-50 p-6 3xl:p-10">
<h2 class="font-display text-2xl text-brand-700">Pricing</h2>
</article>Notice that the markup describes intent (brand, card), not raw values. If the brand color changes, you edit one line.
Extending versus replacing
By default, @theme adds to the built-in theme. Your --color-brand-600 sits next to indigo-600, red-500, and the rest.
If you define a token with the same name as a default, yours wins. For example, --color-blue-500: #0b5fff; changes bg-blue-500 everywhere.
You can also remove defaults. Setting a whole namespace to initial clears it:
@import "tailwindcss";
@theme {
--color-*: initial;
--color-white: #ffffff;
--color-black: #000000;
--color-brand: #4f46e5;
--color-ink: #0f172a;
}After this, bg-red-500 no longer exists. Only white, black, brand, and ink remain. This is useful when a design team wants to forbid off-palette colors. To remove everything, including spacing, fonts, and breakpoints, use --*: initial;.
The spacing token
Spacing works differently from colors. Instead of a list of named steps, Tailwind v4 uses one base value, --spacing, and multiplies it. The default is 0.25rem. So p-4 is calc(var(--spacing) * 4), which is 1rem.
@theme {
--spacing: 0.3rem;
}With this change, p-4 becomes 1.2rem, and every spacing, sizing, and gap utility scales together. This is how you adjust the whole density of a design from one line. It also means classes such as w-17 or p-13 work, because any number is multiplied by the base.
Tokens are CSS variables at runtime
Tailwind also emits theme values as real CSS variables on :root. This has practical benefits. You can read a token in your own CSS or in JavaScript, and you can reference one inside an arbitrary value.
.hero {
background: var(--color-brand-600);
padding: calc(var(--spacing) * 12);
}<div class="bg-(--color-brand-600) p-4">
Uses a theme variable through the arbitrary-value shorthand.
</div>By default, Tailwind only outputs variables that your markup actually uses. If you need every token available as a variable, for example for runtime access from JavaScript, import the theme with the static option:
@import "tailwindcss/theme" theme(static);Referencing other tokens
A token can be built from another. Use @theme inline when the value refers to a variable that is defined elsewhere, such as a variable you switch at runtime.
@import "tailwindcss";
:root {
--surface: #ffffff;
--ink: #0f172a;
}
.dark {
--surface: #0f172a;
--ink: #f8fafc;
}
@theme inline {
--color-surface: var(--surface);
--color-ink: var(--ink);
}Now bg-surface and text-ink follow whichever values :root or .dark set. This is called a semantic token: its name says what it is for, not what it looks like. The same markup works in both color schemes without dark: prefixes on every element.
The inline keyword matters. Without it, Tailwind writes the utility as background-color: var(--color-surface), and --color-surface is resolved where it is declared (on :root), which can miss overrides on nested elements. With inline, the utility is written as background-color: var(--surface), so it resolves at the element that uses it.
Choosing between a token and a one-off value
Not every value deserves a token. Use this rule of thumb.
| Situation | Better choice |
|---|---|
| The value appears in many places, or is part of the brand | Add a token in `@theme` |
| The value is for one element and unlikely to repeat | Use an arbitrary value such as `w-[320px]` |
| The value changes at runtime, such as a theme switch | Semantic token with `@theme inline` |
| You want to forbid off-scale values | Clear the namespace with `initial` |
A common mistake is to reach for arbitrary values every time the design differs slightly from the defaults. If you write bg-[#4f46e5] in ten files, you have created an untracked token. Promote it.
Common errors
- The utility does not exist. You wrote
--brand-color: #4f46e5;and expectedbg-brand-color. The variable is outside any namespace. Rename it to--color-brand-color. - Edits have no effect.
@thememust be in a CSS file that Tailwind processes, after the@import "tailwindcss";line. Check that this file is the one your build loads. - Dark mode tokens do not switch. You defined
--color-surface: var(--surface)withoutinline. Add@theme inline. - Old v3 config ignored. In v4,
tailwind.config.jsis not read automatically. Move your values into@theme, or load the file explicitly with@config "./tailwind.config.js";. - A cleared palette breaks existing pages. Remove old classes first, or keep the defaults you still use.
- Dynamic names fail. A token such as
--color-branddoes not make `text-${name}` detectable. Class names must still appear in full in your source files, as discussed in the chapter on dynamic class names.
How the token flows into CSS
The diagram shows how one token becomes a utility in the generated stylesheet.
Defining a token does not add CSS by itself. The CSS appears only when a file uses the class.
Exercise
Your team wants only these colors in a project: white, black, and one brand color #0ea5e9. You also want to be able to write bg-brand and text-brand. Write the @theme block that does this, and explain what happens to an existing element that uses bg-red-500.
Show answer
Use the namespace reset, then define the allowed colors:
@theme {
--color-: initial;
--color-white: #ffffff;
--color-black: #000000;
--color-brand: #0ea5e9;
}
The `--color-: initial; line removes every default color. The three variables that follow create utilities such as bg-brand and text-brand through the --color-* namespace. An element with bg-red-500` keeps the class in its markup, but Tailwind no longer generates any CSS for it, so the element gets no red background. There is no build error.
Exercise
You define --color-surface: var(--surface); inside a plain @theme block. A nested element sets its own --surface value, but bg-surface ignores it. What is the fix, and why does it work?
Show answer
Change @theme to @theme inline. With inline, Tailwind writes the utility as background-color: var(--surface) instead of var(--color-surface). The variable is then resolved at the element that uses the class, so the nested element's own --surface value applies.