Gradient Backgrounds
What you'll learn: How to use CSS gradients as background images to create smooth color transitions instead of solid colors.
The Core Idea
You've already learned about linear-gradient() and radial-gradient() for creating gradients. Now it's time to use them specifically as background images. Instead of setting a flat background-color, you can apply a gradient to the background-image property to create visually interesting color transitions across an element.
Think of it like painting a wall: instead of using one solid paint color, you're blending two or more colors together smoothly, either in a straight line (linear) or radiating from a center point (radial).
Using Gradients as Backgrounds
Gradients are treated as images in CSS, not colors. This means they go in the background-image property:
.gradient-box {
background-image: linear-gradient(to right, blue, green);
padding: 2rem;
color: white;
}
This creates a smooth horizontal transition from blue on the left to green on the right.
For a radial gradient that radiates from the center outward:
.radial-box {
background-image: radial-gradient(circle, yellow, orange);
width: 200px;
height: 200px;
}
Combining with Other Background Properties
Since gradients are background images, you can combine them with properties like background-size, background-position, and even layer multiple backgrounds:
.layered {
background-image: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)),
linear-gradient(to bottom, purple, pink);
}
Key Takeaway: Gradients function as background images in CSS, allowing you to create smooth color transitions using linear-gradient() and radial-gradient() in the background-image property instead of solid colors.