How to Add Custom Colors to Tailwind CSS
Updated · 2 min read
Tailwind's default palette is excellent, but sooner or later you need your own brand colors as real utilities like bg-brand or text-brand-700. Here's how to do it in both Tailwind v3 and v4.
Tailwind CSS v4: the @theme directive
In v4, configuration moves into your CSS. Any variable in the --color-* namespace becomes a color utility:
@import "tailwindcss";
@theme {
--color-brand: #5b4bff;
--color-brand-50: #f0eeff;
--color-brand-500: #5b4bff;
--color-brand-900: #1e1966;
}
Now bg-brand, text-brand-500, border-brand-900 and the rest all work. The values are also available as normal CSS variables, e.g. var(--color-brand-500).
Tailwind CSS v3: tailwind.config.js
Add colors under theme.extend.colors so you keep the default palette:
// tailwind.config.js
module.exports = {
theme: {
extend: {
colors: {
brand: {
DEFAULT: '#5b4bff',
50: '#f0eeff',
500: '#5b4bff',
900: '#1e1966',
},
},
},
},
};
DEFAULT lets you write bg-brand without a number. If you put colors directly under theme (without extend), you replace the whole default palette.
One-off colors: arbitrary values
For a color you'll use only once, skip the config:
<div class="bg-[#5b4bff] text-[rgb(255_255_255)]">…</div>
If you find yourself repeating the same arbitrary value, move it into the theme.
Building a full 50-950 scale
Tailwind's own colors come in 11 shades. To create a matching scale for your brand color:
- Put your brand color at 500 (or at 600 if it's light).
- Create lighter steps (400 → 50) by mixing with white, and darker steps (600 → 950) by mixing with black. The color converter shows 9 tints and 9 shades of any color.
- Check contrast: aim for text shades (usually 700-900) that pass 4.5:1 on white. See the contrast guide.
- Compare with a similar default color: open its Tailwind color page and match the lightness of each step.
Colors from a logo or photo
Drop the logo into Palette from Image and choose the Tailwind export tab. It gives you a ready-to-paste color object. Rename the keys to meaningful names such as brand, accent and ink.
Theming with CSS variables (light and dark)
For themes that switch at runtime, point Tailwind at CSS variables and change the variables per theme:
/* v4 */
@theme {
--color-surface: var(--surface);
}
:root { --surface: #ffffff; }
.dark { --surface: #0f172a; }
Now bg-surface follows the active theme automatically.
Find a Tailwind color by HEX
Have a HEX code and want the closest Tailwind class? Every color page on this site lists the nearest Tailwind color, and each Tailwind shade page shows similar shades from other color families.