CSS gradients: linear, radial and conic syntax
How to write linear-gradient, radial-gradient and conic-gradient in CSS, with color stops, angles and copy-paste examples.
Updated
A CSS gradient is an image generated by the browser that blends between two or more colors. You write it as a function, linear-gradient(), radial-gradient() or conic-gradient(), and use it anywhere an image is accepted, most often in background. Gradients need no image file and scale to any size.
Linear gradients
A linear gradient blends along a straight line.
.banner {
background: linear-gradient(to right, #6d28d9, #db2777);
}
The first argument sets the direction. It is optional and defaults to to bottom.
| Direction | Result |
|---|---|
to right |
Left to right |
to bottom right |
Toward the bottom right corner |
0deg |
Bottom to top |
90deg |
Left to right |
180deg |
Top to bottom, the same as the default |
Angles turn clockwise, with 0deg pointing up.
Color stops
After the direction comes a list of colors. Each may be followed by a position.
background: linear-gradient(90deg, #0ea5e9 0%, #6d28d9 60%, #db2777 100%);
- With no positions, the colors are spaced evenly.
- Two positions on one color hold it solid across that range:
#6d28d9 30% 60%. - Two neighboring colors at the same position make a hard edge with no blend, which is how stripes are made:
background: linear-gradient(90deg, #6d28d9 50%, #db2777 50%);
Radial gradients
A radial gradient spreads outward from a center point.
background: radial-gradient(circle at top left, #fde68a, #6d28d9);
- The shape is
circleorellipse. The default isellipse, which stretches to match the box. atsets the center, using keywords, percentages or lengths. The default iscenter.- A size keyword sets where the gradient ends:
closest-side,closest-corner,farthest-sideorfarthest-corner, which is the default.
Conic gradients
A conic gradient sweeps colors around a center point, like the hand of a clock.
.pie {
border-radius: 50%;
background: conic-gradient(#6d28d9 0 40%, #db2777 40% 70%, #fde68a 70% 100%);
}
Add from 90deg to change the starting angle and at 30% 50% to move the center. With hard stops, as above, a conic gradient draws a pie chart.
Repeating gradients
repeating-linear-gradient(), repeating-radial-gradient() and repeating-conic-gradient() tile the list of stops. The pattern repeats after the last stop position.
background: repeating-linear-gradient(45deg, #ebe6fb 0 10px, #d3caf0 10px 20px);
Layering
background accepts several images separated by commas. The first one listed is drawn on top, so upper layers need some transparency.
background:
linear-gradient(rgb(0 0 0 / 0.5), rgb(0 0 0 / 0.5)),
url("photo.jpg") center / cover;
This puts a dark overlay on a photo so that text over it stays readable.
Things that catch people out
- A gradient is an image, not a color.
background-color: linear-gradient(...)does nothing. Usebackgroundorbackground-image. - Fading to the keyword
transparentcan look grey in older browsers. Fading to a transparent version of the same color avoids it. - Gradients cannot be animated with a plain
transition. Animatebackground-positionon an oversized gradient, or the opacity of a layer, instead. - Check the contrast of text against both ends of a gradient, not just the middle.
Related: box-shadow syntax and clip-path shapes.