Sub topic 12
Adding your own colors to the theme
The default palette covers most interfaces. Brand colors, a client's style guide, or a semantic naming scheme such as primary and danger need colors the palette doesn't have. This chapter shows how to add them in Tailwind CSS v3 and v4, how the new names become utility classes, and which mistakes cause missing styles.
It assumes you can write utility classes such as bg-indigo-600 and border-slate-300, and that you have a working Tailwind project. It also assumes you know hex notation like #6366f1. Check which major version you have installed, because the two versions configure themes differently.
How a theme color becomes a utility
Tailwind builds its color utilities from the theme. Each color name in the theme is combined with each color-aware utility prefix, so one entry produces many classes.
If the theme contains a color named brand with shade 500, you can use:
bg-brand-500text-brand-500border-brand-500ring-brand-500shadow-brand-500divide-brand-500- variants and modifiers on any of those, such as
hover:bg-brand-500andbg-brand-500/50
You define the color once. Tailwind generates classes for it only when it finds them in your source files.
Tailwind v3: theme.extend.colors
In v3 you add colors in tailwind.config.js. Putting them under theme.extend keeps the default palette and adds yours beside it.
// tailwind.config.js (Tailwind CSS v3)
module.exports = {
theme: {
extend: {
colors: {
brand: {
500: '#6366f1',
600: '#4f46e5',
},
},
},
},
}With this config, bg-brand-500 and border-brand-600 both work, and bg-indigo-600 still works too.
The shape of the value decides which class names you get:
| Config value | Classes you can write |
|---|---|
| `brand: '#6366f1'` | `bg-brand`, `text-brand`, `border-brand` |
| `brand: { 500: '#6366f1' }` | `bg-brand-500` |
| `brand: { DEFAULT: '#6366f1', dark: '#4f46e5' }` | `bg-brand`, `bg-brand-dark` |
The key DEFAULT produces the bare class name with no suffix.
extend versus replace
The placement of colors matters.
// tailwind.config.js (Tailwind CSS v3)
module.exports = {
theme: {
// Replaces the whole palette. bg-indigo-600 no longer exists.
colors: {
brand: '#6366f1',
white: '#ffffff',
black: '#000000',
},
},
}Defining theme.colors directly throws away the default palette. That is useful when a design system forbids off-brand colors, but it also removes white, black, transparent, and current. Add them back yourself if you replace the palette, or components that use text-white will silently lose their color.
Tailwind v4: @theme variables
Version 4 moves theme configuration into CSS. You declare theme variables inside an @theme block, and the variable name determines the utility name.
/* app.css (Tailwind CSS v4) */
@import "tailwindcss";
@theme {
--color-brand-500: #6366f1;
--color-brand-600: #4f46e5;
}The --color- namespace tells Tailwind that these variables are colors. Everything after the prefix becomes the name, so --color-brand-500 creates bg-brand-500, border-brand-500, and the rest of the family. A variable named --color-brand with no shade creates the bare bg-brand.
Tailwind also writes each theme variable into the output CSS as a custom property. You can read it from your own CSS with var(--color-brand-500), which helps when you need the color in a non-utility context such as an SVG fill or a third-party widget.
To remove the default palette in v4, reset the namespace first:
@import "tailwindcss";
@theme {
--color-*: initial;
--color-white: #ffffff;
--color-black: #000000;
--color-brand-500: #6366f1;
--color-brand-600: #4f46e5;
}--color-*: initial clears every default color. As with v3, you then restore the basics you still need.
Using custom colors with modifiers and variants
Custom colors behave like default ones. The slash opacity modifier works on them, and so do state and dark variants.
<button class="rounded-md bg-brand-500 px-4 py-2 text-white hover:bg-brand-600 focus:outline-none focus:ring-2 focus:ring-brand-500/50">
Save changes
</button>The hover state moves to the darker shade, and the focus ring uses the same color at 50% opacity. Because the whole family comes from two theme values, changing the brand color later means editing one place.
Semantic names and CSS variables
Naming colors after their role (primary, surface, danger) makes themes easier to swap than naming them after their hue. There are two approaches.
The first points a semantic name at a palette color. In v4:
@import "tailwindcss";
@theme inline {
--color-primary: var(--color-indigo-600);
--color-primary-hover: var(--color-indigo-500);
}The inline option makes the generated utility use the referenced value directly instead of a nested var() chain. This avoids a known problem where a variable resolves in the wrong scope.
The second approach, in v3, stores the color in a CSS variable so it can change at runtime, for example per theme. The opacity modifier needs to know where the alpha goes, so you provide the <alpha-value> placeholder:
/* global.css */
:root {
--brand-rgb: 99 102 241;
}// tailwind.config.js (Tailwind CSS v3)
module.exports = {
theme: {
extend: {
colors: {
brand: 'rgb(var(--brand-rgb) / <alpha-value>)',
},
},
},
}Here bg-brand/50 works because Tailwind replaces <alpha-value> with 0.5. If you write brand: 'var(--brand)' with a plain hex value instead, bg-brand/50 cannot be computed and the class is not generated.
Choosing a shade scale
A shade scale such as 50 to 950 lets people reuse the same mental model they have for the default palette: 100 for tints, 500 for the base, 600 and 700 for hover and pressed states, and 900 for dark text. Define at least the shades your components actually use. A tool that generates a scale from a base color saves time, but check the results by eye.
Use these checks before you ship a scale:
- Text on your 500 background should still be readable. If white on
brand-500is hard to read, that shade fails contrast, and you should darken it or use dark text. - Each step should be visibly different from its neighbors, or hover states will look broken.
- Dark mode usually wants different shades. Plan for
dark:bg-brand-600as well asbg-brand-500.
When to use arbitrary values instead
If a color appears once, such as a social brand color on a single button, an arbitrary value like bg-[#1da1f2] is simpler than a theme entry. If it appears in three or more places, or if designers might change it, add it to the theme. A theme name documents the intent and gives you one place to edit.
Common errors
- Dynamic class names. Writing `
bg-${color}-500in JavaScript means the full class never appears in your source, so Tailwind never generates it. Write complete names such asbg-brand-500` in a lookup object and pick between them. - Config in the wrong place. In v3, colors added outside
themeortheme.extendare ignored. In v4, variables placed outside@themeare plain CSS variables and create no utilities. - Unintended palette removal. Putting
colorsdirectly underthemein v3 removesslate,indigo,white, and the rest. - Mixing versions. A
tailwind.config.jscolors block does nothing in a v4 project unless you load it with@config. Prefer@themethere. - Missing rebuild. Config changes in v3 require the dev server to pick up the file again. If new classes do nothing, restart the build and check that the file is covered by your
contentpaths. - Wrong variable prefix. In v4,
--colors-brandor--brand-500is not in the color namespace. Only--color-*creates color utilities.
Exercise
In a Tailwind v3 project, you add brand: 'var(--brand)' under theme.extend.colors, where --brand holds a hex value. bg-brand works, but bg-brand/50 produces no background. What is wrong, and how do you fix it?
Show answer
Tailwind cannot insert an alpha value into a plain var() reference, so the opacity class cannot be generated. Store the color as space-separated RGB channels, for example --brand-rgb: 99 102 241;, and define the color as 'rgb(var(--brand-rgb) / <alpha-value>)'. Tailwind then replaces <alpha-value> with the opacity you write, so bg-brand/50 works.
Exercise
In a Tailwind v4 project, you write --brand-600: #4f46e5; inside @theme and try border-brand-600. Nothing happens. Why, and what is the corrected variable name?
Show answer
The variable is not in the --color- namespace, so Tailwind does not treat it as a color and generates no utilities. Rename it to --color-brand-600, and border-brand-600, bg-brand-600, and the other utilities become available.