Skip to content

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.

Updated 23 November 2026Tested with
  • 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.

A brand-coloured shadcn button, an outline button and a tinted badge, in light and dark mode
The brand variant from this page in our test project, light and dark. The dark value is lighter so it keeps its contrast on a dark background.

Add a brand colour in three places

  1. 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 (--primary and --primary-foreground).

    src/index.cssCSS
    :root {  --brand: oklch(0.55 0.2 260);  --brand-foreground: oklch(0.985 0 0);  /* …the existing variables */}
  2. Step 2: Give it a dark value

    Same names in the .dark block. 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);  /* … */}
  3. Step 3: Map it in @theme inline

    This is the line that creates bg-brand, text-brand, border-brand, ring-brand and 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:

src/components/ui/button.tsxTSX
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-brand class at all. The @theme inline line is missing, or it says --brand instead 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 .dark class. The mapping uses @theme instead of @theme inline. Without inline, the utility reads a copy resolved on :root, which a nested .dark can'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.

Was this page helpful?

Still stuck? We answer every message ourselves. Write to us

This page is built from UI Pro parts: the docs article block (docs-quernly) with the code, terminal, install command, steps, contents and feedback components. They ship in the next UI Pro release; the install command and the contents rail are free.