Tailwind CSS already gives us a ton of color options. You’ve probably used classes like text-blue-500
or bg-green-700
before. They're super handy. But what if you need a specific color that’s not in the default palette?
Let’s say you’re building a website for a coffee shop, and you need that perfect mocha brown. Or maybe you’re working with a tech brand that uses a very particular shade of blue. This is where custom colors come in.
Tailwind v4 makes this process easier than ever, thanks to a major shift: the CSS-first approach.
What's New in Tailwind CSS v4?
The CSS-First Approach
Before v4, if you wanted to add custom colors, you had to open the tailwind.config.js
file and write your colors there using JavaScript. After every change, you'd need to rebuild your styles, which could slow you down.
Now, things are different.
You don’t need to touch the config file anymore. You can define and use custom colors straight from your CSS file. This new CSS-first workflow is faster, more flexible, and honestly, just makes more sense.
How Custom Colors Worked Before
In older versions of Tailwind, you’d open tailwind.config.js
and add your color like this:
module.exports = { theme: { extend: { colors: { oceanBlue: '#1CA9C9', oceanLight: '#A0E9FF', oceanDark: '#005377', }, }, }, };
After saving, you’d wait for Tailwind to recompile. Then you could use classes like bg-oceanBlue
or text-oceanDark
.
It worked fine, but it wasn’t fast. If you made small changes often, it could get frustrating.
How It Works Now in Tailwind CSS v4
Now you can define everything directly in your CSS file. Here's what that looks like.
Step 1: Create a Custom Color Variable: Inside your style.css
file, define a CSS variable like this:
@theme { --color-brand-blue: #1E40AF; }
That’s it. You've just created your own custom color.
Step 2: Use It in Your HTML: Let’s say you want to use this color as a background. In your HTML, apply the class like this:
<button class="bg-[var(--color-brand-blue)] text-white px-4 py-2 rounded"> Click Me </button>
Boom — Tailwind applies your custom color. No config file, no rebuilds.
Why the square brackets? Tailwind needs those to understand that you're using a custom value instead of a built-in class. The var(--color-brand-blue)
part pulls the color you defined earlier.
Making a Full Custom Theme
Want to take it a step further? You can build a full color scale, just like Tailwind’s default system.
Step 1: Define Multiple Shades
@theme { --color-brand-green-100: #E6F4EA; --color-brand-green-300: #91D8A8; --color-brand-green-500: #2E8540; --color-brand-green-700: #1C5D2E; }
Step 2: Use Them in Your Classes
<div class="bg-[var(--color-brand-green-100)] text-[var(--color-brand-green-700)] border border-[var(--color-brand-green-500)] p-4 rounded"> Custom green color system in action! </div>
This setup mimics Tailwind’s green-100
, green-500
, etc., but with your own shades.
You Can Use Custom Colors Everywhere
You're not limited to just background or text. You can also use your custom variables for:
-
Borders:
border-[var(--color-brand-green-500)]
-
Shadows:
shadow-[0_4px_10px_var(--color-brand-green-700)]
-
Gradients
-
Hover states
-
Button effects
For example:
<button class="bg-[var(--color-brand-blue)] hover:bg-[var(--color-brand-green-300)] transition text-white px-4 py-2 rounded"> Hover me </button>
This gives you full control over your design.
A Quick Tip About Borders
When using borders in Tailwind, remember to add a width class. For example:
<div class="border border-2 border-[var(--brand-green-700)]"> I have a 2px green border </div>
If you don’t set a thickness, you might not see anything.
Customizing Text Colors
Just like bg-[...]
, you can use text-[...]
to apply your custom colors to text.
<p class="text-[var(--color-brand-green-500)]"> This text uses a custom green color </p>
Same goes for border-[...]
, shadow-[...]
, and more.
Bringing It All Together
Let’s combine background, border, and text using custom colors:
<div class="bg-[var(--color-brand-green-300)] border-[var(--color-brand-green-700)] border-4 text-[var(--color-brand-green-700)] p-6 rounded-md"> Custom theme example with green shades </div>
Now we’re using three levels of our custom color system — 300
, 500
, and 700
— just like Tailwind’s own color palette.
Conclusion
With Tailwind CSS v4, creating custom colors is simpler and faster. No more editing config files. No more rebuilding your project every time you make a small change.
You can define your entire color system in one place — your CSS file — and use it everywhere. This gives you more control, keeps things organized, and helps your site look exactly how you want.