CSS3 Gradients
A CSS gradient is an image the browser generates from colours you list, blending smoothly from one to the next. Because gradients are images, you use them anywhere an image is accepted, most often in background-image. They replace many small image files for button shading, hero overlays, stripes and decorative shapes, and they stay sharp at any screen size.
Linear gradients
linear-gradient() blends colours along a straight line. The first argument sets the direction, either as an angle such as 135deg or with keywords such as to right or to bottom left. Without a direction, the gradient runs from top to bottom. Each colour can be followed by a stop position telling the browser where that colour is reached.
.banner {
background-image: linear-gradient(135deg, #4f46e5 0%, #06b6d4 100%);
}
/* Hard stops make stripes instead of a blend */
.flag {
background-image: linear-gradient(
to bottom,
#ff9933 0 33.33%,
#ffffff 33.33% 66.66%,
#138808 66.66% 100%
);
}
.ring {
width: 120px;
aspect-ratio: 1;
border-radius: 50%;
background: conic-gradient(#22c55e 0 70%, #e5e7eb 70% 100%);
}
Radial and conic gradients
radial-gradient() spreads colours outward from a centre point in a circle or ellipse. You can set the shape and position, for example radial-gradient(circle at top left, #fde68a, transparent 60%) for a soft glow in a corner.
conic-gradient() sweeps colours around a centre point like the hands of a clock. That makes it perfect for pie charts and progress rings, as the .ring example above shows with a seventy percent filled circle.
Several gradients can be stacked in one background declaration, separated by commas, just like image layers. Combining a horizontal and a vertical repeating gradient with thin transparent stops is a common way to draw a grid pattern without any image file. Because each layer is drawn on top of the next, give the upper layers transparent areas so the lower ones show through.
Repeating patterns
Each type has a repeating version: repeating-linear-gradient(), repeating-radial-gradient() and repeating-conic-gradient(). They tile the pattern defined by your stops. A short example is diagonal warning stripes: repeating-linear-gradient(45deg, #facc15 0 10px, #111 10px 20px).
Problems you might hit
Setting a gradient with background-color does nothing, because gradients are images, not colours. Use background-image or the background shorthand.
Fading to the keyword transparent used to produce a greyish band in some older browsers, since it was treated as transparent black. Modern browsers interpolate correctly, but if you see muddy middles, fade to the same colour with zero alpha, such as rgb(79 70 229 / 0).
Another common surprise is a gradient that repeats in bands down a long page. When the element has no height, or the body background has nothing to size against, the gradient tiles. Give the element a height, or add background-repeat: no-repeat and min-height: 100vh where needed.
Lastly, text over a bright gradient can become hard to read at one end. Check contrast at both ends, not just the middle.
Gradients cannot be transitioned directly, so a hover effect that swaps one gradient for another snaps instantly. A smooth alternative is to put the second gradient on a pseudo-element and fade its opacity.
Main points
- Gradients are generated images, so they go in
background-image. - Two stops at the same position create a hard edge.
- Linear for directions, radial for glows, conic for pies and rings.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.