To create a css linear gradient, set background-image: linear-gradient(direction, color-stop, color-stop). The browser draws a smooth blend between the colors along a straight line, and you control the angle with a keyword like to right or a value like 45deg. A gradient is an image generated by CSS, so it needs no file and scales to any size.
This guide covers direction, color stops, hard edges, transparency and the repeating variant.
The function takes an optional direction followed by two or more color stops.
Without a direction the default is to bottom (180 degrees), so the first color is at the top. Because a gradient is an image, it goes in background-image or the background shorthand. It does not work in background-color.
You can use keywords or angles.
| Value | Gradient runs toward |
|---|---|
to top | the top (same as 0deg) |
to right | the right (same as 90deg) |
to bottom | the bottom (same as 180deg) |
to left | the left (same as 270deg) |
to top right | the top-right corner |
45deg | diagonal toward the top-right |
Angles are measured clockwise from the top. Corner keywords such as to bottom right adjust the angle so the 50 percent line passes through the other two corners, which is not always exactly 45 degrees on a rectangle.
Each color can have a position, using a length or a percentage along the gradient line.
Without positions, stops are spread evenly. With positions, you shift where each color is purest. Colors blend between neighboring stops.
Two stops at the same position produce a sharp line instead of a blend.
The two-position syntax (color start end) is supported in current browsers and keeps the code short. Use it for stripes, progress bars and flags.
Gradients can fade to transparent, which is handy for text overlays on photos.
The first layer darkens the bottom of the picture so white captions stay readable. Gradients that use transparent can look slightly gray in older engines because it was defined as transparent black. Writing the same hue with zero alpha, for example rgb(0 0 0 / 0), avoids that.
By default colors are interpolated in sRGB. Modern CSS lets you pick another space after the in keyword.
Spaces like oklch often avoid the muddy gray midpoint between complementary colors. Check browser support for your audience, and keep a plain gradient as the base declaration so older browsers still render something.
repeating-linear-gradient() tiles the pattern along the gradient line.
This draws diagonal stripes with a 20 pixel period and needs no image file.
The solid background-color is a fallback for any environment that does not render the gradient.
No. A gradient is an image value, so use background-image or background. You can still set background-color as a fallback underneath.
Use an angle such as 45deg or 135deg, or a corner keyword such as to bottom right.
Give two stops the same position, for example red 50%, blue 50%, or use the two-position form red 0 50%, blue 50% 100%.
Not in current browsers. The unprefixed linear-gradient() is supported everywhere that matters today.
A linear gradient is a CSS-generated image. Pick a direction with keywords or angles, list color stops with optional positions, repeat a position for hard edges, and fade to transparent for overlays. Use background-image rather than background-color, and keep a solid color underneath as a fallback.