How CSS gradients work
A CSS gradient is an image the browser draws from a short line of code. Once you understand the gradient line, the angle and the color stops, you can write any gradient by hand and predict how it will look.
Gradient builder
The three gradient functions
background-image: linear-gradient(90deg, #1a5fb4, #e66100);
background-image: radial-gradient(circle, #1a5fb4, #e66100);
background-image: conic-gradient(from 0deg, #1a5fb4, #e66100, #1a5fb4);
- Linear changes color along a straight line.
- Radial changes color outward from a centre point, in a circle or an ellipse.
- Conic changes color around a centre point, like the hands of a clock. Repeat the first color at the end to avoid a hard seam.
Gradients are images, so they go in background-image (or the background shorthand), not in background-color.
Which way does the angle point?
CSS angles start at the top and turn clockwise. 0deg runs bottom to top, 90deg runs left to right, 180deg runs top to bottom (the default) and 270deg runs right to left. Keywords also work: to right equals 90deg. to top right is special: it aims at the corner, so its angle depends on the box’s shape.
This differs from school maths, where 0° points right and angles turn counter-clockwise. If a gradient goes the wrong way, that mix-up is the usual reason.
How long is the gradient line?
For an angle that is not a multiple of 90, the browser makes the gradient line long enough that the corners get exactly the first and last colors. For a box W wide and H tall at angle a:
Example: a 400 × 200 pixel box. Results rounded to two decimals.
- At 90deg: 400 × 1 + 200 × 0 = 400 px, the width.
- At 45deg: 400 × 0.7071 + 200 × 0.7071 = 424.26 px.
- At 30deg: 400 × 0.5 + 200 × 0.8660 = 373.21 px.
Every percentage stop is measured along this longer line. At 45deg, a stop at 10% sits 42.43 px along it, not 40 px.
Color stops and hints
Each color can carry a position. Two colors at the same position make a hard edge, which is how you draw stripes:
linear-gradient(90deg, #1a5fb4 0 50%, #e66100 50% 100%)
A bare percentage between two colors is a color hint. It moves the point where the blend is half-way. linear-gradient(90deg, #1a5fb4, 30%, #e66100) reaches the half-way color at 30% instead of 50%, so the orange takes up more of the box. The midpoint field in the builder above writes this hint for you.
Avoiding the grey middle
Two colors from opposite sides of the wheel, such as blue and orange, pass through a dull grey-brown when blended channel by channel in sRGB. Three fixes:
- Add a middle stop by hand with a brighter in-between color.
- Pick two colors closer on the wheel, such as blue and violet.
- Ask the browser to interpolate in a perceptual space, for example
linear-gradient(in oklch 90deg, #1a5fb4, #e66100). Current versions of the major browsers support this syntax; older ones ignore the whole declaration, so put a plain gradient on the line before it as a fallback.
The color mixing guide explains why sRGB averages look darker in the middle.
Common mistakes
- Banding. Long, subtle gradients across large areas can show visible steps, because there are only 256 levels per channel. Shorten the distance or increase the color difference.
- Text over a gradient. Check contrast at both ends and in the middle, not only at one end.
- Forgetting a fallback color. Set a
background-colortoo, so text stays readable if the image fails.
Questions people ask
Do gradients slow a page down?
They are drawn by the browser from a few bytes of code, so they are usually far smaller than an image file. Very large animated gradients can cost repaint time on slow devices.
Can I layer gradients?
Yes. Separate them with commas in background-image. The first one listed is drawn on top, so give it some transparency.