Skip to content

Tailwind CSS - Gradient Color Stops

Gradients are a powerful tool for creating modern and vibrant designs. Tailwind CSS provides an intuitive set of utilities to create linear, radial, and conic gradients by defining their direction and color stops.

Creating a gradient in Tailwind involves three main types of utilities:

  • Direction (bg-gradient-to-{direction}): Specifies the gradient’s angle or shape. For example, bg-gradient-to-r creates a gradient flowing from left to right. Other directions include t (top), b (bottom), l (left), and diagonals like tr (top-right). There are also utilities for bg-radial and bg-conic.
  • Starting Color (from-{color}): Sets the initial color of the gradient. You can use any color from your theme’s palette.
  • Ending Color (to-{color}): Sets the final color of the gradient.
  • Middle Color (via-{color}): (Optional) Adds one or more intermediate colors, creating a multi-color gradient.

You can also specify the position of a color stop using percentage-based utilities like from-10%, via-50%, or to-90% for more precise control.

Instead of an exhaustive list, here is the pattern for these utilities. Replace {color} with a color from your palette (e.g., blue-500, pink-300) and {percentage} with a value from the spacing scale (e.g., 10, 50, 90).

Class PatternPurposeExample
from-{color}Sets the starting color.from-emerald-400
via-{color}Sets an optional middle color.via-cyan-500
to-{color}Sets the ending color.to-blue-600
from-{percentage}%Sets the starting position of the from color.from-10%
via-{percentage}%Sets the position of the via color.via-45%
to-{percentage}%Sets the ending position of the to color.to-90%

Let’s apply these utilities to create a vibrant button and a gradient text effect, which are common in modern web design.

<!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>Gradient Example</title>
</head>
<body class="bg-slate-900 text-white flex flex-col items-center justify-center min-h-screen p-8 gap-12">
<!-- Example 1: Gradient Button with Hover Effect -->
<button class="px-8 py-4 font-bold rounded-lg text-white
bg-gradient-to-br from-pink-500 via-red-500 to-yellow-500
background-size-200
hover:bg-right-center transition-all duration-500">
Hover Me
</button>
<!-- Example 2: Gradient Text -->
<h1 class="text-6xl font-black text-transparent
bg-clip-text
bg-gradient-to-r from-emerald-400 to-cyan-400">
Gradient Text
</h1>
</body>
<!-- Custom utility for the button hover effect -->
<style type="text/tailwindcss">
@layer utilities {
.background-size-200 {
background-size: 200% auto;
}
.bg-right-center {
background-position: right center;
}
}
</style>
</html>

A common use case is fading an image or section into the background. You can do this by setting a to or via color to transparent. Tailwind automatically handles this by fading the from color to a fully transparent version of itself if no to color is specified.

<!-- This div will have a gradient from blue-500 to transparent -->
<div class="h-32 bg-gradient-to-t from-blue-500"></div>
<!-- You can also be explicit -->
<div class="h-32 bg-gradient-to-t from-blue-500 to-transparent"></div>