Skip to content

Tailwind CSS - Background Color

Mastering Background Colors in Tailwind CSS

Section titled “Mastering Background Colors in Tailwind CSS”

Tailwind CSS provides a rich, utility-first system for applying background colors. Instead of writing custom CSS, you use pre-defined classes directly in your HTML. This approach accelerates development, ensures consistency, and integrates seamlessly with Tailwind’s design system.

The core syntax for background colors is bg-{color}-{shade}. Tailwind includes a beautiful default color palette that is both comprehensive and easy to use.

  • bg-: The prefix for all background color utilities.
  • {color}: The name of the color, such as slate, red, green, or indigo.
  • {shade}: A numeric value from 50 (lightest) to 950 (darkest) that controls the intensity of the color. The 500 shade is typically the base color.

Tailwind’s default palette includes a set of neutral grays (Slate, Gray, Zinc, Neutral, Stone) and a full spectrum of vibrant colors (Red, Orange, Amber, Yellow, Lime, Green, Emerald, Teal, Cyan, Sky, Blue, Indigo, Violet, Purple, Fuchsia, Pink, Rose). You can explore the full, official palette in the Tailwind CSS Documentation.

Here is a representative sample of available classes. Note that each color name has shades from 50 to 950.

ClassCSS Properties
bg-inheritbackground-color: inherit;
bg-currentbackground-color: currentColor;
bg-transparentbackground-color: transparent;
bg-blackbackground-color: rgb(0 0 0);
bg-whitebackground-color: rgb(255 255 255);
bg-slate-50background-color: rgb(248 250 252);
bg-slate-500background-color: rgb(100 116 139);
bg-slate-900background-color: rgb(15 23 42);
bg-red-50background-color: rgb(254 242 242);
bg-red-500background-color: rgb(239 68 68);
bg-red-900background-color: rgb(127 29 29);
bg-green-50background-color: rgb(240 253 244);
bg-green-500background-color: rgb(34 197 94);
bg-green-900background-color: rgb(20 83 45);

You can easily control the background color’s opacity by adding a / suffix followed by a percentage value (from 0 to 100). This is a modern, intuitive syntax powered by Tailwind’s JIT engine.

<!-- A blue background with 50% opacity -->
<div class="bg-blue-500/50">...</div>
<!-- A slate background with 75% opacity on hover -->
<button class="bg-slate-200 hover:bg-slate-900/75">...</button>

If the default palette doesn’t have the exact color you need for a one-off scenario, you can use arbitrary values directly in your class name. This avoids the need to write custom CSS for a single-use case.

<!-- Use a specific hex code for a brand color -->
<div class="bg-[#1DA1F2]">...</div>
<!-- Use an RGB value -->
<div class="bg-[rgb(255,0,0)]">...</div>

For real-world projects, you’ll want to customize your color palette to match your brand’s identity. This is done in the tailwind.config.js file. By extending the theme, you can add your own colors while keeping Tailwind’s defaults.

tailwind.config.js
module.exports = {
theme: {
extend: {
colors: {
'brand-primary': '#3490dc',
'brand-secondary': '#ffed4a',
'brand-danger': '#e3342f',
},
},
},
}

You can then use these custom colors in your HTML: <button class="bg-brand-primary text-white">Click Me</button>

This example demonstrates how to use background color utilities, including state variants like hover, to build a modern, interactive card component.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.tailwindcss.com"></script>
<title>Modern Card with Backgrounds</title>
</head>
<body class="bg-slate-100 flex items-center justify-center h-screen">
<div class="max-w-sm rounded-lg overflow-hidden shadow-lg bg-white">
<div class="h-48 bg-cover bg-center" style="background-image: url('https://source.unsplash.com/random/400x300')"></div>
<div class="p-6">
<div class="font-bold text-xl mb-2 text-slate-800">Modern Component</div>
<p class="text-gray-700 text-base">
This card uses a white background for a clean look. The button below changes its background color on hover, providing clear visual feedback to the user.
</p>
</div>
<div class="px-6 pt-4 pb-6">
<button class="w-full bg-indigo-500 hover:bg-indigo-700 text-white font-bold py-2 px-4 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-400 focus:ring-opacity-75 transition-colors duration-300">
Learn More
</button>
</div>
</div>
</body>
</html>