This content originally appeared on DEV Community and was authored by Ruqaiya Beguwala
Welcome to Day 2 of 15 Days of Tailwind Tips
In this series, “15 Days of Tailwind Tips (Under 60 Seconds),” we’re exploring quick, practical ways to improve your UI development workflow using Tailwind CSS. Each post covers a focused topic you can apply immediately in real-world projects — from basic layout to advanced styling.
Today, we’ll dive into one of the most fundamental parts of building a modern, accessible interface: color.
Tailwind CSS makes managing colors extremely efficient with its utility-first color system. Rather than writing custom CSS or switching back and forth between HEX codes, Tailwind lets you apply color using semantic, scalable class names like bg-blue-500
, text-gray-700
, or border-red-300
.
Tailwind’s Color Naming Convention
Tailwind uses a color family + shade level structure:
text-{color}-{shade}
bg-{color}-{shade}
border-{color}-{shade}
For example:
-
bg-blue-500
: A medium blue background -
text-gray-700
: A dark gray text color -
border-red-300
: A light red border
Each color has shades typically ranging from 50 (lightest) to 950 (darkest), with 500
being the base shade in most cases.
Example: Button with Semantic Tailwind Colors
<button class="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600">
Click Me
</button>
Explanation:
-
bg-blue-500
: Primary background color -
text-white
: Ensures contrast for readability -
hover:bg-blue-600
: Darker blue on hover -
px-4 py-2
: Padding for sizing -
rounded
: Smooth corners for a modern look
This approach allows you to style consistent UI components quickly without writing a single line of custom CSS.
Common Color Families in Tailwind
Tailwind includes a broad selection of color families, such as:
gray, red, yellow, green, blue, indigo, purple, pink, orange,
teal, cyan, emerald, lime, amber, rose, slate, zinc, neutral, stone
Each family includes shades from 50
to 950
, allowing you to fine-tune tone and contrast easily.
Semantic Thinking: Choose Colors by Purpose
When working with colors, it’s best to think semantically, not just visually. This helps with long-term maintainability and accessibility.
Intent | Recommended Color Example |
---|---|
Primary |
bg-blue-500 , text-blue-700
|
Success |
bg-green-500 , text-green-800
|
Warning |
bg-yellow-100 , text-yellow-800
|
Danger/Error |
bg-red-500 , text-white
|
Neutral UI |
bg-gray-100 , text-gray-700
|
This approach makes it easier to build design systems and theme your UI across large applications.
Tips & Tricks
Here are some advanced but practical tips to get the most out of Tailwind’s color utilities.
1. Use Opacity Utilities for Subtle Effects
Tailwind provides bg-opacity-*
and text-opacity-*
for layered designs.
<div class="bg-blue-500 bg-opacity-20 p-4">
Light blue with soft opacity
</div>
Useful for overlays, notifications, or gentle UI backgrounds.
2. Pair Text and Background Colors for Contrast
Always check contrast between text and background.
<div class="bg-yellow-100 text-yellow-800 p-4 rounded">
Warning: Please check your input.
</div>
Pairing a light background with a dark text from the same color family keeps your UI accessible and easy to scan.
3. Add Dark Mode Support with dark:
Variants
Tailwind’s dark:
modifier makes styling dark mode easy.
<div class="bg-white text-black dark:bg-gray-800 dark:text-white p-4">
Adaptive light and dark theme
</div>
Apply the dark
class to your <html>
or <body>
tag and Tailwind does the rest.
4. Use ring-*
for Accessible Focus States
Improving accessibility is easy with Tailwind’s ring utilities:
<input class="focus:ring-2 focus:ring-blue-500 outline-none" />
This ensures keyboard users see clear focus indicators.
5. Create Reusable Theme Classes with @apply
You can use Tailwind’s @apply
directive in a CSS file to create custom color-based components:
.btn-primary {
@apply bg-blue-500 text-white hover:bg-blue-600 px-4 py-2 rounded;
}
Now you can use class="btn-primary"
in your HTML for consistent button styling.
6. Use Gradients with Built-In Colors
Tailwind supports gradients using semantic colors:
<div class="bg-gradient-to-r from-indigo-500 to-purple-500 text-white p-6">
Gradient background
</div>
These are great for headers, hero sections, or CTA buttons.
7. Build Your Own Color Palette in tailwind.config.js
Tailwind allows you to customize and extend the default color palette:
// tailwind.config.js
module.exports = {
theme: {
extend: {
colors: {
brand: {
light: '#E0F2FF',
DEFAULT: '#3B82F6',
dark: '#1E40AF',
},
},
},
},
}
Now you can use bg-brand
, bg-brand-dark
, text-brand-light
, and more throughout your project.
Summary: Tailwind Color Utilities At a Glance
Task | Example Class |
---|---|
Background color | bg-blue-500 |
Text color | text-gray-700 |
Border color | border-red-300 |
Hover background | hover:bg-green-600 |
Text opacity | text-opacity-70 |
Background opacity | bg-opacity-30 |
Dark mode color | dark:bg-gray-900 |
Gradient background | bg-gradient-to-r from-blue-500 to-teal-500 |
Focus ring color | focus:ring-purple-500 |
Coming Up Tomorrow
In Day 3, we’ll build a fully styled, reusable Tailwind CSS button with hover states and accessibility in mind — all using utility classes only. You’ll also learn how to avoid repetition using custom classes and @apply
.
This content originally appeared on DEV Community and was authored by Ruqaiya Beguwala