Sub topic 12
Adding Your Own Colors to the Theme
This chapter assumes you can apply default palette colors with utilities such as bg-indigo-600, and that you know the slash opacity modifier. You also need a working Tailwind project. The default palette covers many cases, but most products have brand colors that are not in it. Adding them to the theme gives you named utilities that work with every variant and modifier you already know.
Why extend instead of replace
Tailwind builds color utilities from the theme's color values. Every key becomes a name you can use in bg-*, text-*, border-*, ring-*, divide-*, and shadow-*. If you add a color, the default palette stays available. If you replace the whole color set, classes like bg-slate-200 stop working.
Tailwind v3: theme.extend
In v3 you edit tailwind.config.js. Put your colors under theme.extend so the defaults are kept.
// tailwind.config.js (Tailwind CSS v3)
module.exports = {
theme: {
extend: {
colors: {
brand: {
500: '#6366f1',
600: '#4f46e5',
},
},
},
},
}The object nesting becomes the class name. brand plus 500 gives bg-brand-500, and brand plus 600 gives border-brand-600.
Tailwind v4: @theme
In v4 you define the color in CSS as a theme variable. The variable name follows the pattern --color-<name>-<shade>.
/* app.css (Tailwind CSS v4) */
@import "tailwindcss";
@theme {
--color-brand-500: #6366f1;
--color-brand-600: #4f46e5;
}This generates the same bg-brand-500 and border-brand-600 utilities.
Using the new color
Custom colors behave like built-in ones.
<button class="rounded-md bg-brand-500 px-4 py-2 text-white hover:bg-brand-600 focus:ring-2 focus:ring-brand-500/50">
Save changes
</button>The hover: variant, the focus:ring-2 utility, and the /50 opacity modifier all work with brand-500 without extra setup.
Common errors
- Class has no effect. The name or shade does not match the theme.
bg-brand-700does nothing if you only defined 500 and 600. - Defaults disappeared in v3. You put
colorsdirectly underthemeinstead oftheme.extend. - Changes not showing in v3. The config file is not in the project root, or the build was not restarted.
- Wrong variable name in v4. Color variables must start with
--color-. A variable such as--brand-500creates no utilities.
When a value is needed only once, an arbitrary value such as bg-[#1da1f2] is simpler than a theme entry. Add a theme color when you will reuse it.
Exercise
You add brand: { 500: '#6366f1' } directly under theme (not theme.extend) in a v3 config. What happens to bg-slate-200, and how do you fix it?
Show answer
bg-slate-200 stops working because the default colors were replaced by your object. Move the brand entry under theme.extend.colors to keep the defaults.
Exercise
In Tailwind v4, which theme variable creates the class border-brand-600?
Show answer
--color-brand-600, declared inside an @theme block.