Look at the sky at sunset: the orange slowly turns into purple, with no line between the two colours. A smooth change from one colour to another, like the orange to purple of the sunset sky, is called a gradient, or a colour blend.
In CSS, a gradient is an image that the browser draws. Because it is an image, it goes in the background property, as a background image. You don't need an image file or a design app.
A straight gradient with linear-gradient()
This rule puts a straight gradient on a banner:
.banner {
background: linear-gradient(to right, orange, deeppink);
}
backgroundis where the gradient goes.background-imageworks too.linear-gradient( ... )is a function: a name, then parentheses holding inputs separated by commas. This function draws a colour blend along a straight line.to rightis the direction: the blend starts on the left and ends on the right.orange, deeppinkare the starting colour and the ending colour.
The result: a banner that is orange on the left and slowly turns pink towards the right.
One thing people often miss: background-color: linear-gradient(blue, purple) doesn't work. background-color takes only one colour, and a gradient is an image. The browser ignores that line, so the background stays plain.
Setting the direction
The direction comes first, before the colours, and a comma follows it. If you leave the direction out, the gradient runs from top to bottom. These four lines are four separate examples:
background: linear-gradient(blue, purple); /* blue at the top, purple at the bottom */
background: linear-gradient(to right, red, blue); /* red on the left, blue on the right */
background: linear-gradient(90deg, red, blue); /* same as to right */
background: linear-gradient(to right, red, yellow, green);
to right,to left,to topandto bottomname the side the gradient goes towards.to bottom rightgives a slanted direction, towards the bottom right corner.- A direction can also be an angle:
90deg(90 degrees) is the same asto right, and180degis the same asto bottom. - You can use more than two colours. In the last line, the gradient runs from red, to yellow, then to green.
- Each colour can get a position in percent, counted from where the gradient starts. Two colours at the same position make a sharp edge with no blend.
linear-gradient(to right, green 70%, lightgray 70%)draws a progress bar: the left 70% is green, the rest is light grey.
A circular gradient with radial-gradient()
This rule puts a circular gradient on a box:
.circle {
background: radial-gradient(white, blue);
}
radial-gradient() draws a blend that starts at the centre and spreads outwards. The first colour is in the middle, and the last colour is at the edge. The result: white in the middle, and more and more blue towards the edge, like a ball with light shining on it. The default shape is an ellipse (a stretched circle) that follows the shape of the box. So in a square box, the shape is a circle.
There is also conic-gradient(): its colours turn around the centre point, like a pie chart. Try all three functions in the Lab.
⚠️ Commas, function names, and text that disappears
- A missing comma.
linear-gradient(to right red, blue)is not valid, because there is no comma afterto right. The direction and every colour must be separated by commas. - The function name in the wrong order.
gradient-linear()does not exist. The real ones arelinear-gradient(),radial-gradient()andconic-gradient(). - White text on a bright gradient. The bright part of the gradient makes white text hard to read. Pick colours that are dark enough behind the text.
For white text on a photo, stack a dark gradient on top of the photo:
.hero {
background: linear-gradient(rgb(0 0 0 / 60%), transparent), url("beach.jpg") center / cover;
}
background can hold several layers, separated by commas, and the layer written first is on top. Here the top layer is a black gradient that is 60% solid. It covers the top of the photo, then fades to clear (transparent) towards the bottom. The bottom layer is url("beach.jpg"), the photo file. center / cover puts the photo in the middle and makes it cover the whole box.
TL;DR: A gradient is an image, drawn by the browser, of one colour changing smoothly into another. Put a gradient in background, not in background-color. linear-gradient(blue, purple) runs straight from top to bottom: blue at the top, purple at the bottom. To change the direction, write it before the colours: to right or 90deg makes the gradient run from left to right. radial-gradient(white, blue) spreads from the centre: the first colour is in the middle, the last colour at the edge.
⚡ A straight gradient
Write:
.banner {
background: linear-gradient(to right, orange, deeppink);
}
Meaning: A gradient is a smooth change from one colour to another. linear-gradient() draws a gradient along a straight line. Inside the parentheses are a direction (to right), then the starting colour and the ending colour, all separated by commas. So this banner is orange on the left and pink on the right. With no direction, the gradient runs from top to bottom.
⚡ An image, not a colour
Guess first: does background-color: linear-gradient(blue, purple) work?
No. background-color takes only one colour, and a gradient is an image. The browser ignores the line, so the background stays plain.
Meaning: A gradient is an image that the browser draws, not a colour. Put a gradient in background or background-image.
⚡ Spreading from the centre
Write:
.circle {
background: radial-gradient(white, blue);
}
Meaning: radial-gradient() draws a gradient that starts at the centre and spreads outwards. The first colour is in the middle, and the last colour is at the edge. Here the middle is white, and it gets more and more blue towards the edge.