Gradients — CSS

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…

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);
}

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);

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

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.

What you will learn