Tailwind v4 @theme — wiring a palette into utilities
Tailwind CSS v4 defines design tokens in CSS inside an @theme block rather than a JavaScript config file. Naming a variable --color-brand-500 generates utilities such as bg-brand-500 and text-brand-500. OKLCH values work as they are.
The basic shape
@import "tailwindcss";
@theme {
--color-brand-50: oklch(97% 0.02 250);
--color-brand-500: oklch(58% 0.16 250);
--color-brand-950: oklch(18% 0.04 250);
--color-bg: light-dark(oklch(97% 0.02 250), oklch(18% 0.04 250));
--color-text: light-dark(oklch(18% 0.04 250), oklch(97% 0.02 250));
}
Variables defined in @theme are emitted as real CSS variables and drive utility generation. --color-bg creates the bg-bg utility and is also usable as var(--color-bg).
Naming
| Variable | Utilities | Role |
|---|---|---|
| --color-brand-500 | bg-brand-500, text-brand-500 | palette step |
| --color-bg | bg-bg | page background |
| --color-surface | bg-surface | card surface |
| --color-text | text-text | body text |
| --color-muted | text-muted | secondary text |
| --color-primary | bg-primary | accent button |
Role names survive a theme change. With bg-blue-600, switching to a different step in dark mode forces a class rename. With a role name, only the variable changes.
With light-dark()
Tailwind v4 passes values straight through to CSS, so light-dark() works. It needs color-scheme: light dark, and a @supports fallback block keeps older browsers correct. The Tailwind export here writes that structure.
Cautions
- Variables inside @theme should be static. Change them in media queries or :hover with separate rules instead.
- Tailwind ships a default palette, but an explicit brand palette is clearer than overriding it.
- Arbitrary values like
bg-[oklch(58%_0.16_250)]are quick but are not tokens. Move repeated values into @theme. - If you also use the
dark:variant, make sure it does not overlap with light-dark(); pick one approach.
In short: put the palette steps in @theme, define role tokens with light-dark(), use role names in markup and let CSS handle the theme switch. Both blocks come out of the export tab in one copy.
Migrating from v3
Moving from v3 to v4 mostly means transferring colour definitions from the config file into @theme. Anything dynamic in theme.extend.colors, such as functions or conditional values, has to become static. Some default palette names also changed in v4, so existing utility classes need to be checked rather than assumed.
Do not run both systems side by side for long. With some screens on v3 classes and others on v4 tokens, colours drift subtly between pages. Migrate screen by screen and re-check contrast and gamut on each one. The Tailwind block from this tool is a good starting point for a new project; in an existing one, copy only the values.
The @theme syntax follows the official Tailwind v4 documentation.
Questions
How is v4 different from v3?
v4 uses @theme in CSS instead of tailwind.config.js. Tokens become CSS variables, which makes runtime theme switching straightforward.
Can I use OKLCH values directly?
Yes. Tailwind outputs the value unchanged, so browsers that support oklch() render it. Provide a fallback for older ones separately.
Do I have to use dark: classes?
No. light-dark() tokens switch without them. Avoid mixing both approaches in one project.
Copy the Tailwind v4 block from the export tab into your project.
Open the toolBasis date 28 November 2026. Written against the W3C specifications, the APCA documentation and published browser support tables; updated when they change.
Back to the guides