Course contentsShow
← Web Development
Lesson 622 of 2,98211. Utility-First CSS with TailwindFree lesson

Background Utilities: Colors and Gradients

Apply solid backgrounds with bg-color-* and create gradients using from-*, via-*, to-* utilities.

Background Utilities: Colors and Gradients

What you'll learn: How to apply solid background colors and create beautiful gradients using Tailwind's utility classes.

In Tailwind CSS, you don't write custom CSS for backgrounds—you apply pre-built utility classes directly to your HTML elements. This keeps your styling consistent and speeds up development.

Solid Background Colors

To apply a solid background, use the bg-{color}-{shade} pattern. For example:

<div class="bg-blue-500">Blue background</div>
<div class="bg-red-300">Light red background</div>
<div class="bg-gray-900">Dark gray background</div>

The color name (like blue, red, gray) comes from Tailwind's color palette, and the shade number (100-900) controls intensity—lower numbers are lighter, higher numbers are darker.

Creating Gradients

Gradients blend multiple colors smoothly. Tailwind makes this easy with three utilities working together:

  • bg-gradient-to-{direction} — Sets the gradient direction (r for right, l for left, t for top, b for bottom, or diagonal combinations like br)
  • from-{color}-{shade} — Defines the starting color
  • via-{color}-{shade} — Adds an optional middle color
  • to-{color}-{shade} — Defines the ending color
<!-- Simple two-color gradient -->
<div class="bg-gradient-to-r from-purple-500 to-pink-500">
  Purple to pink gradient
</div>

<!-- Three-color gradient -->
<div class="bg-gradient-to-br from-green-400 via-blue-500 to-purple-600">
  Multi-color diagonal gradient
</div>

Think of it like mixing paint: from- is where you start, to- is where you end, and via- adds a color in between for more complexity.

Key Takeaway: Use bg-{color}-{shade} for solid backgrounds and combine bg-gradient-to-{direction} with from-*, via-*, and to-* utilities to create eye-catching gradients—all without writing custom CSS.