Tailwind v4 custom colors: how to add them, and why they don’t work
Add a --color-* variable in @theme and every colour utility follows. The four reasons a custom colour silently does nothing, each reproduced.
- Tailwind CSS 4.3.5
In Tailwind CSS v4 you add a colour by declaring a --color-* variable inside @theme, in the same CSS file that imports Tailwind. --color-brand-500 gives you bg-brand-500, text-brand-500, ring-brand-500/50 and every other colour utility. If your new class does nothing, it is almost always one of the four mistakes further down.
Add a colour
@import "tailwindcss"; @theme { --color-brand-50: oklch(0.97 0.02 260); --color-brand-500: oklch(0.62 0.19 259); --color-brand-600: oklch(0.55 0.2 260); --color-brand-900: oklch(0.32 0.12 262);}Use it like any built-in colour:
<a className="rounded-md bg-brand-600 px-4 py-2 text-white hover:bg-brand-500"> Start free trial</a>Tailwind compiles that to a plain variable lookup, so the value can change at runtime:
npx @tailwindcss/cli -i src/styles/global.css | grep -A2 "bg-brand-500 {" .bg-brand-500 { background-color: var(--color-brand-500); }Opacity modifiers work without extra setup. ring-brand/50 becomes a color-mix() with transparent, with a fallback for browsers that lack it. A single colour without a scale (--color-brand) is fine too; you then write bg-brand.
You can use any CSS colour: hex, rgb(), hsl() or oklch(). Tailwind's own palette is in oklch since v4. We use oklch for new scales because equal steps in lightness look like equal steps, which makes a 50 to 950 scale easier to get right. Our palette generator builds a full scale from one hex value and outputs the @theme block, and hex to Tailwind finds the nearest built-in colour if you'd rather not add one.
Custom colours not working: the four usual causes
We reproduced each of these on Tailwind 4.3.5. In every case the build succeeds and the class silently does nothing, which is what makes them annoying.
1. The variable is in :root, not in @theme
A variable in :root is just a CSS variable. Tailwind only creates utilities from @theme.
/* No utility: Tailwind never sees this */:root { --color-brand-500: oklch(0.62 0.19 259); } /* Generates bg-brand-500 */@theme { --color-brand-500: oklch(0.62 0.19 259); }If you want the value in :root (for example to switch it in dark mode), keep it there and map it in @theme inline. That's what shadcn/ui does; adding a colour to a shadcn theme shows the full pattern.
2. The name is missing the --color- prefix
@theme { --brand-500: … } is valid CSS and generates nothing. The namespace tells Tailwind which utilities to build: --color-* for colours, --font-* for families, --text-* for sizes.
3. The colour is in tailwind.config.js
v4 doesn't read the config file unless you ask it to. Either move the colour to @theme or add @config "../tailwind.config.js"; under the import. More on that in does Tailwind 4 have a config file.
4. The class name is built at runtime
Tailwind finds classes by reading your files as text. `bg-${color}-500` is never written out in full, so bg-brand-500 is never generated. Write the full names in a map instead:
const tone = { brand: "bg-brand-500 text-white", muted: "bg-zinc-100 text-zinc-700",}; <span className={tone[variant]}>…</span>If the names really come from data, list them once with @source inline("bg-brand-{50,500,600,900}");.
Replacing the default palette
To ship only your own colours, reset the namespace before you add yours:
@theme { --color-*: initial; --color-white: #fff; --color-ink: oklch(0.15 0 0); --color-brand-500: oklch(0.62 0.19 259);}After the reset, bg-red-500 and the rest are gone, which keeps a design system honest. Remember to add back white, black and transparent if you use them; transparent and current are not part of the colour namespace, so they keep working.
Questions
How do I add a custom colour in Tailwind v4 without a scale?
Declare one variable: @theme { --color-brand: #2563eb; }. You get bg-brand, text-brand, bg-brand/20 and so on.
Can I use a CSS variable from :root as a Tailwind colour?
Yes, with @theme inline { --color-brand: var(--brand); }. The inline keyword makes the utility use var(--brand) directly, so a .dark block that changes --brand also changes the utility.
Why does my colour work in the browser but not in my editor's autocomplete?
The Tailwind CSS IntelliSense extension needs to find your main CSS file. In VS Code, set "tailwindCSS.experimental.configFile": "src/styles/global.css" if it doesn't pick it up on its own.
Related articles
- shadcn/uiAdd a custom colour to a shadcn/ui themeThree places in your CSS: the value in :root, the dark value in .dark, and one line in @theme inline. Plus a brand button variant and a contrast check.
- UI designHow to choose a colour palette for a websiteOne brand scale, one neutral scale and a few status colours, with steps assigned by measured contrast. Tailwind’s own scales measured in light and dark.
- Tailwind CSS v4Does Tailwind CSS v4 have a config file?Not by default: v4 is configured in CSS with @theme. Where every v3 setting went, how to load an old tailwind.config.js with @config, and when to keep it.
Still stuck? We answer every message ourselves. Write to us