Gradients add smooth color changes to backgrounds. Tailwind makes it easy to create them with simple classes.
Gradient utilities (from, via, to) in Tailwind
Start learning this pattern below
Jump into concepts and practice - no test required
bg-gradient-to-{direction} from-{color} via-{color} to-{color}bg-gradient-to-{direction} sets the gradient direction (like t for top, r for right).
from-, via-, and to- set the colors at the start, middle, and end of the gradient.
bg-gradient-to-r from-blue-500 to-green-500
bg-gradient-to-b from-red-400 via-yellow-300 to-green-400
bg-gradient-to-tr from-purple-600 to-pink-400
This page shows a box with a smooth gradient background from blue to purple to pink horizontally. The text is centered and easy to read.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Gradient Utilities Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="flex items-center justify-center min-h-screen bg-gray-100"> <div class="w-64 h-32 rounded-lg bg-gradient-to-r from-blue-500 via-purple-500 to-pink-500 flex items-center justify-center text-white font-bold text-lg"> Gradient Box </div> </body> </html>
You can use as many colors as you want by combining from-, via-, and to-.
Make sure your text color contrasts well with the gradient for readability.
Use bg-gradient-to-{direction} to control the gradient angle easily.
Use from-, via-, and to- classes to create smooth color gradients.
Set gradient direction with bg-gradient-to-{direction}.
Gradients add visual interest and modern style to your web elements.
Practice
from-blue-500 do when used with a gradient background?Solution
Step 1: Understand gradient color stops
Thefrom-class in Tailwind sets the first color stop in a gradient.Step 2: Identify the role of
This class sets the gradient's start color to the blue-500 shade.from-blue-500Final Answer:
It sets the starting color of the gradient to blue-500. -> Option BQuick Check:
from-means start color [OK]
- Confusing 'from-' with 'to-' or 'via-' classes
- Thinking it changes text color instead of background
- Assuming it sets the gradient direction
Solution
Step 1: Check gradient direction syntax
The correct direction syntax isbg-gradient-to-{direction}, hereto-rmeans left to right.Step 2: Verify color stop order
The order must befrom-(start),via-(middle), thento-(end).bg-gradient-to-r from-red-500 via-yellow-400 to-green-500follows this order correctly.Final Answer:
<code>bg-gradient-to-r from-red-500 via-yellow-400 to-green-500</code> -> Option CQuick Check:
Correct syntax and order =bg-gradient-to-r from-red-500 via-yellow-400 to-green-500[OK]
- Mixing order of from, via, to classes
- Using bg-gradient-from- instead of bg-gradient-to-
- Placing via- after to-
<div class="bg-gradient-to-b from-purple-400 via-pink-500 to-red-500 h-32 w-32"></div>
Solution
Step 1: Understand gradient direction
bg-gradient-to-bmeans gradient goes from top to bottom (vertical).Step 2: Analyze color stops
from-purple-400is start (top),via-pink-500is middle,to-red-500is end (bottom).Final Answer:
A vertical gradient from purple at top, through pink in middle, to red at bottom. -> Option AQuick Check:
to-b = vertical gradient, from/via/to colors in order [OK]
- Confusing to-b with to-r (vertical vs horizontal)
- Ignoring the via- color in the middle
- Thinking it creates stripes instead of smooth gradient
class="bg-gradient-to-l from-green-400 to-yellow-500 via-blue-500"
Solution
Step 1: Check gradient direction
bg-gradient-to-lis valid and means gradient goes leftwards.Step 2: Verify color stop order
Order isfrom-green-400,to-yellow-500, andvia-blue-500. Tailwind allowsvia-anywhere between from and to; order in class attribute does not matter.Final Answer:
There is no error; this is a valid gradient class. -> Option DQuick Check:
Gradient direction and colors are valid [OK]
- Thinking class order matters for gradient stops
- Assuming 'to-l' is invalid direction
- Believing via- color must match from- color
Solution
Step 1: Choose correct gradient direction
Diagonal from top-left to bottom-right isbg-gradient-to-br.Step 2: Set color stops in correct order
Start with blue (from-blue-600), middle pink (via-pink-300), end purple (to-purple-700).Step 3: Verify options
bg-gradient-to-br from-blue-600 via-pink-300 to-purple-700matches direction and color order perfectly; others mix colors or directions incorrectly.Final Answer:
<code>bg-gradient-to-br from-blue-600 via-pink-300 to-purple-700</code> -> Option AQuick Check:
Diagonal + correct from/via/to colors =bg-gradient-to-br from-blue-600 via-pink-300 to-purple-700[OK]
- Mixing up gradient directions (br vs tr vs bl)
- Placing colors in wrong from/via/to order
- Using middle color as start or end
