A CSS gradient is an image the browser draws from a few parameters. It costs no network request, scales to any size, and can be layered, sized and repeated like any background image. There are three kinds, each with a repeating variant.
linear-gradient
Colors change along a straight line:
background: linear-gradient(#3b82f6, #9333ea); /* top to bottom (default) */
background: linear-gradient(to right, #3b82f6, #9333ea); /* left to right */
background: linear-gradient(135deg, #3b82f6, #9333ea); /* angle */
Angles start at the top and go clockwise: 0deg points up, 90deg right, 180deg down. to top right points at the corner, so the angle adapts to the element's shape, while 45deg stays fixed.
Color stops
Each color can have a position. Without positions, colors are spread evenly:
/* Even: 0%, 50%, 100% */
linear-gradient(90deg, red, yellow, green);
/* Most of the space is red */
linear-gradient(90deg, red 60%, yellow, green);
/* Hard stop: no blending between colors at the same position */
linear-gradient(90deg, red 50%, blue 50%);
/* Shorthand for a band: blue from 30% to 70% */
linear-gradient(90deg, white 30%, blue 30% 70%, white 70%);
A position without a color is a hint that moves the midpoint of the blend: linear-gradient(red, 20%, blue) reaches the halfway color at 20%.
radial-gradient
Colors radiate from a center point:
background: radial-gradient(#fde68a, #f59e0b); /* ellipse, from the center */
background: radial-gradient(circle, #fde68a, #f59e0b);
background: radial-gradient(circle at top left, #fde68a, transparent 60%);
background: radial-gradient(circle 200px at 30% 40%, #fde68a, #f59e0b);
The size keyword controls where the last stop lands: farthest-corner (default), closest-side, closest-corner or farthest-side. Radial gradients fading to transparent are the basis of spotlight and glow effects.
conic-gradient
Colors rotate around a center, like the hands of a clock:
background: conic-gradient(red, yellow, lime, aqua, blue, magenta, red); /* color wheel */
background: conic-gradient(from 90deg at 50% 50%, #3b82f6, #9333ea);
With hard stops, conic gradients draw pie charts without SVG:
.pie {
width: 160px;
aspect-ratio: 1;
border-radius: 50%;
background: conic-gradient(#3b82f6 0 40%, #22c55e 40% 75%, #f59e0b 75% 100%);
}
Repeating gradients
repeating-linear-gradient, repeating-radial-gradient and repeating-conic-gradient repeat the stop pattern:
/* Diagonal stripes */
background: repeating-linear-gradient(45deg, #e5e7eb 0 10px, #f9fafb 10px 20px);
/* Checkerboard */
background: repeating-conic-gradient(#e5e7eb 0 25%, #fff 0 50%) 0 0 / 24px 24px;
Layering
background accepts several images, separated by commas. The first is on top:
.hero {
background:
linear-gradient(to bottom, rgb(0 0 0 / 0.6), transparent 50%),
url("hero.jpg") center / cover;
}
That overlay keeps white text readable on a photo. Layered radial gradients with transparent edges create "mesh" style backgrounds:
background:
radial-gradient(at 20% 30%, #a5b4fc 0, transparent 50%),
radial-gradient(at 80% 20%, #f9a8d4 0, transparent 50%),
radial-gradient(at 50% 90%, #6ee7b7 0, transparent 50%),
#fff;
Gradient text
.gradient-text {
background: linear-gradient(90deg, #3b82f6, #9333ea);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
}
Keep a solid color fallback for very old browsers if you need it, and check contrast against the background at both ends of the gradient.
Smoother blends with color interpolation
By default, gradients blend in sRGB, and complementary colors pass through a muddy gray in the middle (blue to yellow is the classic example). Modern browsers let you choose the color space:
background: linear-gradient(in oklch, #3b82f6, #facc15);
background: linear-gradient(in oklab to right, #3b82f6, #facc15);
oklab gives even, perceptually smooth blends; oklch keeps colors vivid by moving around the hue wheel. The color formats guide explains why perceptual spaces look smoother.
Common mistakes
- Using
background-colorfor a gradient. It must bebackgroundorbackground-image. - Gradients on
bodynot filling the page. Short pages repeat the gradient. Addmin-height: 100vhto the element, orbackground-attachment: fixedfor a fixed backdrop. - Fading to
transparentin old engines. It used to mean transparent black and produced a gray band. Current browsers interpolate premultiplied colors, so it's fine today. - Text on gradients without a contrast check. Test the lightest and darkest point behind the text.
Gradients also pair well with box shadows and keyframe animations for depth and movement.