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.

句法

/* 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);
/* Color stop: 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);
/* Color hint: A gradient going from the left to right,
   starting red, getting to the midpoint color
   10% of the way across the length of the gradient,
   taking the rest of the 90% of the length to change to blue */
linear-gradient(.25turn, red, 10%, blue);
/* Multi-position color stop: A gradient tilted 45 degrees,
   with a red bottom-left half and a blue top-right half,
   with a hard line where the gradient changes from red to blue */
linear-gradient(45deg, red 0 50%, blue 50% 100%);
					

<side-or-corner>
The position of the gradient line's starting point. If specified, it consists of the word to and up to two keywords: one indicates the horizontal side ( left or right ), and the other the vertical side ( top or bottom ). The order of the side keywords does not matter. If unspecified, it defaults to to bottom .
The values to top , to bottom , to left ,和 to right are equivalent to the angles 0deg , 180deg , 270deg ,和 90deg , respectively. The other values are translated into an angle.
<angle>
The gradient line's angle of direction. A value of 0deg 相当于 to top ; increasing values rotate clockwise from there.
<linear-color-stop>
A color-stop's <color> value, followed by one or two optional stop positions, (each being either a <percentage> <length> along the gradient's axis).
<color-hint>

The color-hint is an interpolation hint defining how the gradient progresses between adjacent color stops. The length defines at which point between two color stops the gradient color should reach the midpoint of the color transition. If omitted, the midpoint of the color transition is the midpoint between two color stops.

注意: Rendering of color stops in CSS gradients follows the same rules as color stops in SVG gradients .

Note also that the first example above does not exactly render as depicted in Mozilla Firefox (particularly version 80.0b3). You'll have to set the html height property to 100% or 100vh to render as depicted.

描述

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.

因为 <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.

linear-gradient.png

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.

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.

Customizing Gradients

By adding more color-stop points on the gradient line, you can create a highly customized transition between multiple colors. A color-stop's position can be explicitly defined by using a <length> <percentage> . If you don't specify the location of a color, it is placed halfway between the one that precedes it and the one that follows it. The following two gradients are equivalent.

linear-gradient(red, orange, yellow, green, blue);
linear-gradient(red 0%, orange 25%, yellow 50%, green 75%, blue 100%);
					

By default, colors transition smoothly from the color at one color stop to the color at the subsequent color stop, with the midpoint between the colors being the half way point between the color transition. You can move this midpoint to any position between two color stops by adding an unlabelled % color hint between the two colors to indicate where the middle of the color transition should be. The following example is solid red from the start to the 10% mark and solid blue from 90% to the end. Between 10% and 90% the color transitions from red to blue, however the midpoint of the transition is at the 30% mark rather than 50% as would have happened without the 30% color hint.

linear-gradient(red 10%, 30%, blue 90%);
					

If two or more color stops are at the same location, the transition will be a hard line between the first and last colors declared at that location.

Color stops should be listed in ascending order. Subsequent color stops of lower value will override the value of the previous color stop creating a hard transition. The following changes from red to yellow at the 30% mark, and then transitions from yellow to blue over 35% of the gradient

linear-gradient(red 40%, yellow 30%, blue 65%);
					

Multi-position color stop are allowed. A color can be declared as two adjacent color stops by including both positions in the CSS declaration. The following three gradients are equivalent:

linear-gradient(red 0%, orange 10%, orange 30%, yellow 50%, yellow 70%, green 90%, green 100%);
linear-gradient(red, orange 10% 30%, yellow 50% 70%, green 90%);
linear-gradient(red 0%, orange 10% 30%, yellow 50% 70%, green 90% 100%);
					

By default, if there is no color with a 0% stop, the first color declared will be at that point. Similarly, the last color will continue to the 100% mark, or be at the 100% mark if no length has been declared on that last stop.

范例

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 60%, cyan);
}
					

Gradient with multi-position color stops

This example uses multi-position color stops, with adjacent colors having the same color stop value, creating a striped effect.

body {
  width: 100vw;
  height: 100vh;
}
					
body {
  background: linear-gradient(to right,
     red 20%, orange 20% 40%, yellow 40% 60%, green 60% 80%, blue 80%);
}
					

More linear-gradient examples

Please see Using CSS gradients 了解更多范例。

规范

规范 状态 注释
CSS Images Module Level 4
The definition of 'Gradient Color-Stops' in that specification.
工作草案 Adds interpolation hints.
CSS Images Module Level 3
The definition of 'linear-gradient()' in that specification.
候选推荐 初始定义。

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
linear-gradient() Chrome 完整支持 26 Edge 完整支持 12 Firefox 完整支持 16 注意事项
完整支持 16 注意事项
注意事项 Before Firefox 36, gradients weren't applied on the pre-multiplied color space, leading to shades of grey unexpectedly appearing when used with transparency.
完整支持 3.6 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -moz-
注意事项 Since Firefox 42, the prefixed version of gradients can be disabled by setting layout.css.prefixes.gradients to false .
注意事项 Considers <angle> to start to the right, instead of the top. I.e. it considered an angle of 0deg as a direction indicator pointing to the right.
preference (needs to be set to ). To change preferences in Firefox, visit about:config.
IE 完整支持 10 注意事项
完整支持 10 注意事项
注意事项 Internet Explorer 5.5 through 9.0 supported gradients via a proprietary filter: -ms-filter: progid:DXImageTransform.Microsoft.Gradient() .
Opera 完整支持 12.1
完整支持 12.1
不支持 11 — 15 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -o-
注意事项 Considers <angle> to start to the right, instead of the top. I.e. it considered an angle of 0deg as a direction indicator pointing to the right.
完整支持 15 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Considers <angle> to start to the right, instead of the top. I.e. it considered an angle of 0deg as a direction indicator pointing to the right.
Safari 完整支持 6.1
完整支持 6.1
完整支持 5.1 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Safari 4 was supporting an experimental -webkit-gradient(linear,…) function. It is more limited than the later standard version: you cannot specify both a position and an angle like in linear-gradient() . This old outdated syntax is still supported for compatibility purposes.
注意事项 Considers <angle> to start to the right, instead of the top. I.e. it considered an angle of 0deg as a direction indicator pointing to the right.
WebView Android 完整支持 ≤37 Chrome Android 完整支持 26 Firefox Android 完整支持 16 注意事项
完整支持 16 注意事项
注意事项 Before Firefox 36, gradients weren't applied on the pre-multiplied color space, leading to shades of grey unexpectedly appearing when used with transparency.
完整支持 4 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -moz-
注意事项 Since Firefox 42, the prefixed version of gradients can be disabled by setting layout.css.prefixes.gradients to false .
注意事项 Considers <angle> to start to the right, instead of the top. I.e. it considered an angle of 0deg as a direction indicator pointing to the right.
preference (needs to be set to ). To change preferences in Firefox, visit about:config.
Opera Android 完整支持 12.1
完整支持 12.1
不支持 11 — 14 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -o-
注意事项 Considers <angle> to start to the right, instead of the top. I.e. it considered an angle of 0deg as a direction indicator pointing to the right.
完整支持 14 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Considers <angle> to start to the right, instead of the top. I.e. it considered an angle of 0deg as a direction indicator pointing to the right.
Safari iOS 完整支持 6.1
完整支持 6.1
完整支持 6 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Safari 4 was supporting an experimental -webkit-gradient(linear,…) function. It is more limited than the later standard version: you cannot specify both a position and an angle like in linear-gradient() . This old outdated syntax is still supported for compatibility purposes.
注意事项 Considers <angle> to start to the right, instead of the top. I.e. it considered an angle of 0deg as a direction indicator pointing to the right.
Samsung Internet Android 完整支持 1.5
Double-position color stops Chrome 完整支持 71 Edge 完整支持 79 Firefox 完整支持 64 IE 不支持 No Opera 完整支持 58 Safari 完整支持 12.1 WebView Android 完整支持 71 Chrome Android 完整支持 71 Firefox Android 完整支持 64 Opera Android 完整支持 50 Safari iOS 完整支持 12.2 Samsung Internet Android 完整支持 10.0
Interpolation Hints / Gradient Midpoints Chrome 完整支持 40 Edge 完整支持 79 Firefox 完整支持 36 IE 不支持 No Opera 完整支持 27 Safari 完整支持 6.1 WebView Android 完整支持 40 Chrome Android 完整支持 40 Firefox Android 完整支持 36 Opera Android 完整支持 27 Safari iOS 完整支持 6.1 Samsung Internet Android 完整支持 4.0
to keyword Chrome 完整支持 26 Edge 完整支持 12 Firefox 完整支持 10 IE 完整支持 10 Opera 完整支持 12.1 Safari 完整支持 6.1 WebView Android 完整支持 ≤37 Chrome Android 完整支持 26 Firefox Android 完整支持 10 Opera Android 完整支持 14 Safari iOS 完整支持 6.1 Samsung Internet Android 完整支持 1.5
Unitless 0 for <angle> Chrome 完整支持 26 Edge 完整支持 12 Firefox 完整支持 55
完整支持 55
部分支持 46 注意事项
注意事项 Accepted only in -webkit-linear-gradient() and -moz-linear-gradient() , not linear-gradient() .
IE 不支持 No Opera 完整支持 16 Safari 完整支持 6.1 WebView Android 完整支持 ≤37 Chrome Android 完整支持 26 Firefox Android 完整支持 55
完整支持 55
部分支持 46 注意事项
注意事项 Accepted only in -webkit-linear-gradient() and -moz-linear-gradient() , not linear-gradient() .
Opera Android 完整支持 14 Safari iOS 完整支持 6.1 Samsung Internet Android 完整支持 1.5

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改:
  1. CSS
  2. CSS 参考

Copyright  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1