Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Create a Colorful Gradient Background with Tailwind CSS
📖 Scenario: You want to make a webpage section that looks bright and colorful using a smooth gradient background. This will make your page look modern and attractive, like a sunset sky blending colors.
🎯 Goal: Build a simple webpage section with a gradient background that uses Tailwind CSS gradient utilities: from, via, and to. The gradient should smoothly blend three colors horizontally.
📋 What You'll Learn
Use Tailwind CSS classes to create a horizontal gradient background
Use from-pink-500 as the start color
Use via-yellow-400 as the middle color
Use to-green-500 as the end color
Add text inside the section that is easy to read on the gradient background
Make sure the section is at least 20rem tall and full width
Use semantic HTML with a section element
💡 Why This Matters
🌍 Real World
Gradient backgrounds are popular in modern web design to create visually appealing sections like headers, banners, or call-to-action areas.
💼 Career
Knowing how to use Tailwind CSS gradient utilities helps you quickly style websites with professional-looking color transitions, a valuable skill for frontend developers.
Progress0 / 4 steps
1
Create the basic HTML structure with a section
Write the basic HTML5 structure with a section element that has the classes h-80 and w-full to set height and width. Inside the section, add a p tag with the text "Welcome to the colorful gradient section!".
Tailwind
Hint
Use a section tag with Tailwind classes h-80 for height and w-full for full width. Add a paragraph inside with the exact text.
2
Add the gradient background with bg-gradient-to-r
Add the Tailwind class bg-gradient-to-r to the existing section element to create a horizontal gradient background from left to right.
Tailwind
Hint
Add bg-gradient-to-r to the section class list to make the gradient go from left to right.
3
Add the gradient colors using from-pink-500 and to-green-500
Add the Tailwind classes from-pink-500 and to-green-500 to the section element to set the start and end colors of the gradient.
Tailwind
Hint
Add from-pink-500 and to-green-500 to the class attribute to set gradient start and end colors.
4
Add the middle gradient color using via-yellow-400 and style the text
Add the Tailwind class via-yellow-400 to the section element to add a middle color to the gradient. Also add flex, items-center, justify-center, and text-white classes to center the text vertically and horizontally and make the text white for good contrast.
Tailwind
Hint
Add via-yellow-400 to add the middle gradient color. Use flex, items-center, and justify-center to center the text. Use text-white for readable text color.
Practice
(1/5)
1. What does the Tailwind class from-blue-500 do when used with a gradient background?
easy
A. It changes the text color to blue-500.
B. It sets the starting color of the gradient to blue-500.
C. It sets the ending color of the gradient to blue-500.
D. It sets the middle color of the gradient to blue-500.
Solution
Step 1: Understand gradient color stops
The from- class in Tailwind sets the first color stop in a gradient.
Step 2: Identify the role of from-blue-500
This class sets the gradient's start color to the blue-500 shade.
Final Answer:
It sets the starting color of the gradient to blue-500. -> Option B
Quick Check:
from- means start color [OK]
Hint: Remember: from = start color, via = middle, to = end [OK]
Common Mistakes:
Confusing 'from-' with 'to-' or 'via-' classes
Thinking it changes text color instead of background
Assuming it sets the gradient direction
2. Which of the following is the correct Tailwind syntax to create a gradient that starts from red, passes through yellow, and ends in green?
easy
A. bg-gradient-from-red-500 via-yellow-400 to-green-500
B. bg-gradient-to-r from-red-500 to-yellow-400 via-green-500
C. bg-gradient-to-r from-red-500 via-yellow-400 to-green-500
D. bg-gradient-to-r via-red-500 from-yellow-400 to-green-500
Solution
Step 1: Check gradient direction syntax
The correct direction syntax is bg-gradient-to-{direction}, here to-r means left to right.
Step 2: Verify color stop order
The order must be from- (start), via- (middle), then to- (end). bg-gradient-to-r from-red-500 via-yellow-400 to-green-500 follows this order correctly.
Final Answer:
<code>bg-gradient-to-r from-red-500 via-yellow-400 to-green-500</code> -> Option C
Quick Check:
Correct syntax and order = bg-gradient-to-r from-red-500 via-yellow-400 to-green-500 [OK]
Hint: Use bg-gradient-to-{dir} with from-, via-, to- in order [OK]
Common Mistakes:
Mixing order of from, via, to classes
Using bg-gradient-from- instead of bg-gradient-to-
Placing via- after to-
3. What will be the visible background gradient of this div?
A. The order of color stops is incorrect; 'via-' should come between 'from-' and 'to-'.
B. The gradient direction 'to-l' is invalid in Tailwind.
C. The 'via-' color must be the same as 'from-' color.
D. There is no error; this is a valid gradient class.
Solution
Step 1: Check gradient direction
bg-gradient-to-l is valid and means gradient goes leftwards.
Step 2: Verify color stop order
Order is from-green-400, to-yellow-500, and via-blue-500. Tailwind allows via- 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 D
Quick Check:
Gradient direction and colors are valid [OK]
Hint: Order of classes in attribute doesn't affect gradient correctness [OK]
Common Mistakes:
Thinking class order matters for gradient stops
Assuming 'to-l' is invalid direction
Believing via- color must match from- color
5. You want a smooth diagonal gradient from blue to purple with a soft pink in the middle on a button. Which Tailwind classes correctly achieve this effect?
hard
A. bg-gradient-to-br from-blue-600 via-pink-300 to-purple-700
B. bg-gradient-to-tr from-purple-700 via-pink-300 to-blue-600
C. bg-gradient-to-br from-pink-300 via-blue-600 to-purple-700
D. bg-gradient-to-bl from-blue-600 via-purple-700 to-pink-300
Solution
Step 1: Choose correct gradient direction
Diagonal from top-left to bottom-right is bg-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-700 matches 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 A