Add a custom colour to a shadcn/ui theme
Three 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.
- shadcn 4.23.0
- Tailwind CSS 4.3.5
- Vite 8
- React 19.3
To add a colour to a shadcn/ui theme you touch three places in your main CSS file: the value in :root, the dark value in .dark, and one line in @theme inline that turns the variable into Tailwind utilities. Skip any of the three and you get the classic symptoms: no class at all, or a colour that ignores dark mode.

Add a brand colour in three places
Step 1: Give it a light value
Next to the other variables in
:root. Add a foreground too, for text on top of the colour; that's the convention every shadcn colour follows (--primaryand--primary-foreground).src/index.cssCSS :root { --brand: oklch(0.55 0.2 260); --brand-foreground: oklch(0.985 0 0); /* …the existing variables */}Step 2: Give it a dark value
Same names in the
.darkblock. A dark background usually wants a lighter, slightly less saturated version, and the foreground flips to dark.src/index.cssCSS .dark { --brand: oklch(0.72 0.15 255); --brand-foreground: oklch(0.2 0.05 260); /* … */}Step 3: Map it in @theme inline
This is the line that creates
bg-brand,text-brand,border-brand,ring-brandand the/50-style opacity versions.src/index.cssCSS @theme inline { --color-brand: var(--brand); --color-brand-foreground: var(--brand-foreground); /* … */}
Now use it. You can add a variant to the button so the colour is one prop away everywhere:
const buttonVariants = cva("…", { variants: { variant: { brand: "bg-brand text-brand-foreground hover:bg-brand/90", default: "bg-primary text-primary-foreground hover:bg-primary/80", // … }, },}) <Button variant="brand">Start free trial</Button>Editing button.tsx is normal in shadcn: it's your file. Keep in mind that a later shadcn add button --overwrite replaces it; updating components without losing edits explains how to keep your variant.
Changing primary instead of adding a colour
If you only want your brand where shadcn uses its main colour (default buttons, focus rings in some styles, active states), don't add anything. Change the values of --primary and --primary-foreground in both blocks. Every component follows. For the sidebar, also set --sidebar-primary; it has its own set of variables.
To try whole palettes before you pick one, the shadcn create page (ui.shadcn.com/create) builds presets you can apply with npx shadcn@latest apply --preset <code>. Commit first: it rewrites your theme and reinstalls components.
When the colour doesn't work
- No
bg-brandclass at all. The@theme inlineline is missing, or it says--brandinstead of--color-brand. Tailwind only builds colour utilities from the--color-namespace. - Right in light mode, wrong in dark. The variable is missing from
.dark. - Dark mode works on the page but not in a section with its own
.darkclass. The mapping uses@themeinstead of@theme inline. Withoutinline, the utility reads a copy resolved on:root, which a nested.darkcan't change. - The colour is transparent. Something wraps it:
hsl(var(--brand))around an oklch value is an invalid colour, which computes to transparent. That's a v3 habit; on v4 use the variable as it is. More v3 leftovers.
Check the contrast
A brand colour behind text has to pass contrast in both modes. For body-size text on a button, aim for 4.5:1 between --brand and --brand-foreground. Browser dev tools show the ratio when you inspect the text. We measured the pair above: 4.81:1 in light mode, and 7.33:1 for the lighter blue with its dark foreground in dark mode. White text on that lighter blue would fail, which is why the foreground changes too. Choosing a colour palette goes into picking values that work.
Questions
Where do I put custom colours in a shadcn project on Tailwind v4?
In the CSS file named under tailwind.css in components.json (often src/index.css or app/globals.css). There is no tailwind.config.js to edit.
Can I use hex instead of oklch?
Yes. Any valid CSS colour works as the value of --brand. The CLI writes oklch because it's what Tailwind v4's own palette uses; opacity modifiers and color-mix() work the same with hex.
Do UI Pro blocks pick up my colours?
Mostly, yes. Our blocks are built on the semantic variables (--primary, --muted, --border and so on), so backgrounds, text, borders and buttons follow your theme. Some use a fixed colour where the design needs one, for example white text over a photo or a green "in stock" dot. Search the block file for white, black or a palette name if you want those on your tokens too.
Related articles
- Tailwind CSS v4Tailwind v4 custom colors: how to add them, and why they don’t workAdd a --color-* variable in @theme and every colour utility follows. The four reasons a custom colour silently does nothing, each reproduced.
- 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.
- shadcn/uishadcn/ui dark mode not working? Check these seven thingsIs the dark class on <html>? If not: no provider, a flash on load, or Astro view transitions. If it is: colours without a dark value or @theme without inline.
Still stuck? We answer every message ourselves. Write to us