<gradient> - CSS: Cascading Style Sheets 编辑

The <gradient> CSS data type is a special type of <image> that consists of a progressive transition between two or more colors.

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 CSS 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 to which it applies.

Syntax

The <gradient> data type is defined with one of the function types listed below.

Linear gradient

Linear gradients transition colors progressively along an imaginary line. They are generated with the linear-gradient() function.

Radial gradient

Radial gradients transition colors progressively from a center point (origin). They are generated with the radial-gradient() function.

Repeating gradient

Repeating gradients duplicate a gradient as much as necessary to fill a given area. They are generated with the repeating-linear-gradient() and repeating-radial-gradient() functions.

Conic gradient

Conic gradients transition colors progressively around a circle. They are generated with the conic-gradient() function.

Interpolation

As with any interpolation involving colors, gradients are calculated in the alpha-premultiplied color space. This prevents unexpected shades of gray from appearing when both the color and the opacity are changing. (Be aware that older browsers may not use this behavior when using the transparent keyword.)

Examples

Linear gradient example

A simple linear gradient.

<div class="linear-gradient">Linear gradient</div>
div {
  width: 240px;
  height: 80px;
}
.linear-gradient {
  background: linear-gradient(to right,
      red, orange, yellow, green, blue, indigo, violet);
}

Radial gradient example

A simple radial gradient.

<div class="radial-gradient">Radial gradient</div>
div {
  width: 240px;
  height: 80px;
}
.radial-gradient {
  background: radial-gradient(red, yellow, rgb(30, 144, 255));
}

Repeating gradient examples

Simple repeating linear and radial gradient examples.

<div class="linear-repeat">Repeating linear gradient</div>
<br>
<div class="radial-repeat">Repeating radial gradient</div>
div {
  width: 240px;
  height: 80px;
}
.linear-repeat {
  background: repeating-linear-gradient(to top left,
      lightpink, lightpink 5px, white 5px, white 10px);
}

.radial-repeat {
  background: repeating-radial-gradient(powderblue, powderblue 8px, white 8px, white 16px);
}

Conic gradient example

A simple conic gradient example. Note that this isn't supported widely across browser as of yet.

<div class="conic-gradient">Conic gradient</div>
div {
  width: 200px;
  height: 200px;
}
.conic-gradient {
  background: conic-gradient(lightpink, white, powderblue);
}

Specifications

SpecificationStatusComment
CSS Images Module Level 4
The definition of '<gradient>' in that specification.
Working DraftAdds conic-gradient
CSS Images Module Level 3
The definition of '<gradient>' in that specification.
Candidate RecommendationInitial definition.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:57 次

字数:10066

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文