TTKTheTextKit

How CSS Gradients Work: Linear, Radial and Conic Explained

How CSS gradients work: angles, colour stops, hard edges, repeating patterns and transparency. Checked on 800 gradients and 200,000 pixels in Chromium.

By TheTextKit TeamPublished

CSS gradients checked in Chromium: 800 gradients drawn, 202,186 pixels compared, largest difference 1 level of 255

A table of three results for the CSS gradient generator. 800 random gradients were drawn in Chromium. 202,186 pixels were compared with an independent calculation. The largest difference was 1 level of colour out of 255.

CSS gradients are images the browser draws for you, from a list of colours placed along a line or around a point. A linear-gradient blends colours along a line at an angle, a radial-gradient spreads them out from a centre, and a conic-gradient sweeps them around it. The value goes in the background property, for example linear-gradient(90deg, #ff512f 0%, #dd2476 100%). This guide explains the parts of that value, from angles to hard edges to transparency. It also shows how we checked our CSS gradient generator, by drawing 800 gradients in Chromium and comparing more than 200,000 pixels.

How CSS gradients work

Every gradient has the same parts: a shape that decides where the colours run, and a list of colour stops. Each stop is a colour and a position, and the browser blends smoothly between neighbours.

TypeThe colours changeTypical use
LinearAlong a line at an angleBackgrounds, buttons, fades
RadialOutward from a pointSpotlights, glows
ConicAround a pointColour wheels, pie charts
The three kinds of CSS gradient: linear along a line, radial outward from a point, conic around a point

A table of the three kinds of CSS gradient and how the colours change. A linear gradient changes along a line. A radial gradient changes outward from a point. A conic gradient changes around a point.

The shape decides where the colours run.

You can build any of these CSS gradients in the CSS Gradient Generator, which previews the result and writes the CSS. The rest of this guide is what the generator does under the hood.

Angles and the gradient line

A linear gradient runs along an imaginary line, the gradient line, and the colour at any point of the box depends on where that point falls along it. The direction is the angle. According to MDN, 0deg is the same as to top, increasing values rotate clockwise, and to right is 90deg. If you give no direction, it defaults to to bottom.

AnglePointsColours run
0degUpBottom to top
90degRightLeft to right
180degDownTop to bottom
270degLeftRight to left
Linear gradient angles and directions: 0deg points up, 90deg points right, 180deg points down, 270deg points left

A table of four angles of a linear gradient and the direction each points. 0deg points up. 90deg points right. 180deg points down. 270deg points left.

Angles turn clockwise from the top.

The gradient line is not always as long as the box is wide. The CSS Images specification gives its length as the absolute value of the width times the sine of the angle plus the absolute value of the height times the cosine. At 90 degrees that is just the width, and at 180 degrees just the height. At 135 degrees on a box 200 pixels wide and 100 tall, it is 141.4 plus 70.7, which is 212.1 pixels. The line is set so that the corners of the box get the first and last colours exactly.

Colour stops, hard edges and stripes

A stop is a colour followed by a position. In linear-gradient(90deg, #ff512f 0%, #dd2476 100%) the first colour sits at the start and the second at the end. Positions are usually percentages. The specification says that before the first stop the line has the colour of the first stop. After the last stop it has the colour of the last. So stops from 20% to 80% leave solid bands at each end.

MDN explains what happens when two stops share a position: the transition becomes a hard line between the first and last colours declared at that location. That is how stripes are made. Put blue at 50% and white at 50%, and the colour jumps with no blend.

A repeating gradient repeats its stops. According to the specification, the stops repeat in both directions, shifted by the distance between the first and the last one. So repeating-linear-gradient(45deg, #2e5cff 0%, #2e5cff 10%, #ffffff 10%, #ffffff 20%) makes diagonal stripes each 20% of the line long. The specification warns that where the pattern repeats, the last and first stops meet, so you get a sharp edge unless the gradient starts and ends on the same colour.

Radial and conic gradients

A radial gradient has a shape, a circle or an ellipse, and a centre, which defaults to the middle of the box. Colours run from the centre outward, and by default the shape is sized to reach the farthest corner, so the last colour just touches it. In the generator you can place the centre at any edge or corner, which is how spotlights and corner glows are made.

A conic gradient has no radius at all. MDN says its starting point is north, the 12 o'clock position, and the colours go clockwise around the centre, and that the from angle rotates the start. If you do not give an angle for the first and last stop, they are 0 and 360 degrees. Since the gradient wraps, a colour wheel starts and ends on the same colour, such as red.

Fading to transparent

Gradients that fade out are common: a colour on the left, nothing on the right. You might expect the middle to go grey, because the word transparent stands for black with no opacity. It doesn't, because of a rule in the specification: colours between two stops are blended in premultiplied RGBA space.

We checked this in Chromium. Take linear-gradient(90deg, #ff0000, transparent) over a white page. At the halfway point the pixel was 255, 128, 128, which is red at half opacity. If the colours were mixed as plain numbers, the half way colour would be 127.5, 0, 0 at half opacity, which comes out at 191, 128, 128 over white, a duller red. The result was the same when transparent was written as rgba(0,0,0,0) and as rgba(255,0,0,0), so the hidden colour makes no difference.

BlendMiddle pixel over white
Premultiplied, as CSS does it255, 128, 128
Plain mix of the numbers191, 128, 128
Fading from red to transparent: the middle pixel is 255, 128, 128 in CSS, against 191, 128, 128 for a plain mix

A table of the middle pixel of a gradient from red to transparent over a white page. As CSS draws it, with premultiplied colours, the pixel is 255, 128, 128. A plain mix of the numbers would give 191, 128, 128.

Premultiplied blending keeps the fade red instead of dull.

A fallback colour

Browsers that cannot draw a gradient ignore the declaration. So put a plain colour first, then the gradient, and the older browser keeps the colour: background-color: #ff512f; followed by background: linear-gradient(...). Our generator adds that line when you tick the fallback option.

How we tested the generator

The generator writes a CSS string, and the test is whether a browser draws what the string says. A script made 800 random gradients: linear, radial and conic, plain and repeating. Angles ran from 0 to 360 including decimals, and all nine centre positions were used. Each gradient had two to five stops with random hex colours. One colour in four had transparency, and about a quarter of the gradients had hard edges. Each was drawn in Chromium in a box of a random size.

Separately we wrote down in Python what the CSS rules say each pixel should be. It used the gradient line length above, the farthest corner size for radial gradients, the clockwise sweep for conic ones, the repeat rule and premultiplied blending, with the result composited on white. Then we compared the two at about 250 points per picture. We skipped pixels next to a hard edge or a repeat seam, where one pixel can fall either way.

TypeGradients drawn
Linear, plain and repeating391
Radial, plain and repeating208
Conic, plain and repeating201
Gradients drawn in Chromium by type: 391 linear, 208 radial and 201 conic, 800 in total, all matching the calculation

A table of the 800 gradients drawn in Chromium by type. 391 were linear. 208 were radial. 201 were conic. All matched an independent calculation to within 1 level of colour out of 255.

All 800 matched to within one level of colour.

In total 202,186 pixels were compared, and the largest difference was 1 level out of 255. In the first run, three points disagreed, all on a conic seam, where the angle wraps from 360 back to 0. That was a flaw in our check, which only skipped one side of the seam, and once it skipped both sides, no point disagreed. Every gradient was also accepted by Chromium as valid.

Limits

The generator writes colours as hex values, including the short form and eight digit values with transparency. These CSS gradients are limited to what hex colours can express. It does not write named colours, hsl(), or colour spaces such as oklch, and it does not set the size keywords of a radial gradient or give a stop two positions. The check ran in Chromium only, so another browser may differ by a level or two in how it rounds.

Frequently asked questions

How do I make a CSS gradient?

You write a gradient as the value of the background property, for example linear-gradient(90deg, #ff512f 0%, #dd2476 100%). The first part sets the direction, and each colour is followed by its position. A generator builds the value and shows a preview.

What do the angles of a linear gradient mean?

An angle of 0 degrees points to the top, 90 degrees to the right, 180 degrees to the bottom and 270 degrees to the left. Angles turn clockwise, so 135 degrees runs from the top left corner towards the bottom right.

How do I make a hard edge or stripes in a gradient?

Give two colour stops the same position, such as blue at 50% and white at 50%. The colour then changes at once with no blend. For stripes, repeat the pattern with repeating-linear-gradient and stops that start and end with the same colour.

What is a conic gradient?

A conic gradient turns colours around a centre point, like the slices of a pie, instead of along a line. It starts at the top and goes clockwise, and the from angle rotates the start. It is used for colour wheels and pie charts.

Why does a fade to transparent not turn grey?

Because browsers blend gradient colours in premultiplied form, so a fade from red to transparent stays red and only the opacity changes. Whatever hidden colour the word transparent stands for makes no difference to the result.

Make your gradient now

Open the CSS Gradient Generator to build CSS gradients, preview them and copy the CSS. For the shadow that goes with them, use the CSS Shadow Generator, and to convert colours, the Color Converter. Everything runs in your browser, so nothing you design is uploaded.

Try these free tools

100% in your browser

Keep reading

All articles →