The linear-gradient() CSS function creates an image consisting of a progressive transition between two or more colors along a straight line. Its result is an object of the <gradient> data type, which is a special kind of <image>.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
As with any gradient, a linear gradient has no intrinsic dimensions; i.e., it has no natural or preferred size, nor a preferred ratio. Its concrete size will match the size of the element it applies to.
To create a linear gradient that repeats so as to fill its container, use the repeating-linear-gradient() function instead.
Because <gradient>s belong to the <image> data type, they can only be used where <image>s can be used. For this reason, linear-gradient() won't work on background-color and other properties that use the <color> data type.
Composition of a linear gradient
A linear gradient is defined by an axis—the gradient line—and two or more color-stop points. Each point on the axis is a distinct color; to create a smooth gradient, the linear-gradient() function draws a series of colored lines perpendicular to the gradient line, each one matching the color of the point where it intersects the gradient line.

The gradient line is defined by the center of the box containing the gradient image and by an angle. The colors of the gradient are determined by two or more points: the starting point, the ending point, and, in between, optional color-stop points.
The starting point is the location on the gradient line where the first color begins. The ending point is the point where the last color ends. Each of these two points is defined by the intersection of the gradient line with a perpendicular line passing from the box corner which is in the same quadrant. The ending point can be simply understood as the symmetrical point of the starting point. These somewhat complex definitions lead to an interesting effect sometimes called magic corners: the corners nearest to the starting and ending points have the same color as their respective starting or ending points.
By adding more color-stop points on the gradient line, you can create a highly customized transition between the starting and ending colors. A color-stop's position can be explicitly defined by using a <length> or a <percentage>. If you don't specify the location, it is placed halfway between the one that precedes it and the one that follows it.
Syntax
/* A gradient tilted 45 degrees, starting blue and finishing red */ linear-gradient(45deg, blue, red); /* A gradient going from the bottom right to the top left corner, starting blue and finishing red */ linear-gradient(to left top, blue, red); /* A gradient going from the bottom to top, starting blue, turning green at 40% of its length, and finishing red */ linear-gradient(0deg, blue, green 40%, red);
Values
<side-or-corner>- The position of the gradient line's starting point. If specified, it consists of the word
toand up to two keywords: one indicates the horizontal side (leftorright), and the other the vertical side (toporbottom). The order of the side keywords does not matter. If unspecified, it defaults toto bottom. - The values
to top,to bottom,to left, andto rightare equivalent to the angles0deg,180deg,270deg, and90degrespectively. The other values are translated into an angle. <angle>- The gradient line's angle of direction. A value of
0degis equivalent toto top; increasing values rotate clockwise from there. <color-stop>- A color-stop's
<color>value, followed by an optional stop position (either a<percentage>or a<length>along the gradient's axis). -
Note: Rendering of color stops in CSS gradients follows the same rules as color stops in SVG gradients.
Formal syntax
linear-gradient( [ <angle> | to <side-or-corner> ,]? <color-stop> [, <color-stop>]+ ) \---------------------------------/ \----------------------------/ Definition of the gradient line List of color stops where<side-or-corner> = [left | right] || [top | bottom]and<color-stop> = <color> [ <percentage> | <length> ]?
Examples
Gradient at a 45-degree angle
body {
width: 100vw;
height: 100vh;
}
body {
background: linear-gradient(45deg, red, blue);
}
Gradient that starts at 60% of the gradient line
body {
width: 100vw;
height: 100vh;
}
body {
background: linear-gradient(135deg, orange, orange 60%, cyan);
}
Note: Please see Using CSS gradients for more examples.
Specifications
| Specification | Status | Comment |
|---|---|---|
| CSS Images Module Level 4 The definition of 'Gradient Color-Stops' in that specification. |
Working Draft | Adds interpolation hints. |
| CSS Images Module Level 3 The definition of 'linear-gradient()' in that specification. |
Candidate Recommendation | Initial definition. |
Browser compatibility
| Desktop | Mobile | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
| Basic support | Chrome
Full support
26
| Edge Full support 12 | Firefox
Full support
16
| IE
Full support
10
| Opera
Full support
12.1
| Safari
Full support
6.1
| WebView Android
Full support
Yes
| Chrome Android
Full support
Yes
| Edge Mobile Full support Yes | Firefox Android
Full support
16
| Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
to keyword | Chrome Full support 26 | Edge Full support 12 | Firefox Full support 10 | IE Full support 10 | Opera Full support 12.1 | Safari Full support 6.1 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 10 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
| Interpolation Hints / Gradient Midpoints | Chrome Full support 40 | Edge No support No | Firefox Full support 36 | IE No support No | Opera Full support 27 | Safari Full support 6.1 | WebView Android Full support 40 | Chrome Android Full support 40 | Edge Mobile No support No | Firefox Android Full support 36 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
Unitless 0 for <angle> | Chrome Full support 26 | Edge Full support 12 | Firefox
Full support
55
| IE No support No | Opera Full support 16 | Safari Full support 6.1 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support 12 | Firefox Android
Full support
55
| Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
Legend
- Full support
- Full support
- No support
- No support
- See implementation notes.
- See implementation notes.
- User must explicitly enable this feature.
- User must explicitly enable this feature.
- Requires a vendor prefix or different name for use.
- Requires a vendor prefix or different name for use.
See also
- Using CSS gradients
- Other gradient functions:
repeating-linear-gradient(),radial-gradient(),repeating-radial-gradient() <image>