CSS Reference ยท Functions
The linear-gradient() function
Reference for the CSS linear-gradient() function: directions and angles, color stops, hard stops, hints, color spaces, repeating patterns and examples.
The linear-gradient() function creates an image that fades between two or more colors along a straight line. Because it is an image, you use it where images are accepted, most often background-image (and the background shorthand), but also in border-image, mask-image and list-style-image. It is not valid for background-color.
.hero { background: linear-gradient(to right, #7c3aed, #ec4899); }
Try it
Syntax
linear-gradient(
[ <angle> | to <side-or-corner> ]? [ in <color-space> ]?,
<color-stop-list>
)
The result is an <image>. Gradient images are not animatable as a whole (background-image changes between gradients are discrete), but you can animate registered custom properties used inside them.
Syntax and arguments
| Argument | Description |
|---|---|
| Direction | An angle (0deg points up, angles increase clockwise) or to top, to right, to bottom right, and so on. Default: to bottom (180deg). |
in <color-space> |
Optional interpolation space, such as oklab or oklch. Default is srgb. |
| Color stop | A color with an optional position (length or percentage), e.g. red 20%. Two positions make a stop that spans a range. |
| Color hint | A bare position between two stops, e.g. 20%, that moves the midpoint of the blend. |
Usage notes
- Hard edges. Put two stops at the same position (
red 50%, blue 50%) for a sharp line with no blend. - Corner directions.
to bottom rightis computed so the 50% line passes through the other two corners, which differs from a fixed135degon non-square boxes. - Transparency. Gradients are interpolated with premultiplied alpha, so fading a color to
transparentdoes not turn gray in current browsers. - Stacking. Several gradients can be layered, separated by commas; the first is on top.
- Banding and text. Smooth, low-contrast gradients can show banding on some screens. Ensure text on top still has enough contrast along the whole gradient.
- Repeats. Use
repeating-linear-gradient()for stripes. - Performance. Gradients are drawn by the browser and are cheaper than image files for simple effects.
Browser support
Supported in every modern browser. Optional in <color-space> syntax is newer; check a compatibility table before depending on it.