WebKit 是第一家支持渐变的引擎。
<div class="c" style="background-image: linear-gradient(to left,#000,#666);">-WebKit-gradient(<type>, <point> [,
<radius>]?,<point>[,<radius>]?[,<stop>]* )
</div>
div {
width: 400px;
height: 200px;
border: 2px solid #fcf;
padding: 4px;
background: -webkit-gradient(
linear,
left top,
left bottom,
from(blue),
to(red)
);
-webkit-background-origin: padding-box;
-webkit-background-clip: content-box;
}
使用 to() 函数
div {
width: 400px;
height: 200px;
border: 2px solid #fcf;
padding: 4px;
background: -webkit-gradient(
linear,
left top,
left bottom,
from(blue),
to(red),
color-stop(50%, green)
);
-webkit-background-origin: padding-box;
-webkit-background-clip: content-box;
}
二重渐变
div {
width: 400px;
height: 200px;
border: 2px solid #fcf;
padding: 4px;
background: -webkit-gradient(
linear,
left top,
left bottom,
from(blue),
to(red),
color-stop(0.5, #fff),
color-stop(0.5, #000)
);
-webkit-background-origin: padding-box;
-webkit-background-clip: content-box;
}
Gecko 渐变 线性渐变
-moz-linear-gradient([<point> |<angle>, ]?<stop>, <stop>[, <stop>] *);
div {
width: 400px;
height: 200px;
border: 2px solid #fcf;
padding: 4px;
background: -moz-linear-gradient(red, blue);
}
div {
width: 400px;
height: 200px;
border: 2px solid #fcf;
padding: 4px;
background: -moz-linear-gradient(top left, red, rgba(255, 0, 0, 0));
}
div {
width: 400px;
height: 200px;
border: 2px solid #fcf;
padding: 4px;
background: -moz-linear-gradient(
left,
red,
orange,
yellow,
green,
blue,
indigo,
violet
);
}
-moz-radial-gradient([<position>|
|<angle>,]?[<shape>|
|<size>,]?<stop>,<stop>[,<stop>]*)
div {
width: 400px;
height: 200px;
border: 2px solid #fcf;
padding: 4px;
background: -moz-radial-gradient(bottom left, red, yellow, blue 80%);
}
Gecko 定义了 -moz-repeating-linear-gradient 为重复性线性渐变,定义了 -moz-repeating-radial-gradient
IE 不支持 CSS 渐变,但提供了渐变滤镜,可以简单的渐变效果。 IE 浏览器渐变滤镜的效果:
filter: progid:DXImageTransform.Microsoft.Gradient(enabled=bEnabled,startColorStr=iWidth,endColorStr=iWidth);
body {
/*让渐变背景填满整个页面*/
padding: 1em;
margin: 0; /*为网页设计垂直渐变背景*/
filter: progid: DXImageTransform.Microsoft.Gradient(gradientType=0,
startColorStr=#9999FF, endColorStr=#ffffff);
} /* 定义标题样式 */
h1 {
color: white;
font-size: 18px;
height: 45px;
padding-left: 3em;
line-height: 65px; /* 控制文本显示位置 */
border-bottom: solid 2px #c72223; /*为标题插入一个装饰图标*/
background: url(images/icon4.png) no-repeat left center;
}
.box {
background: url(images/bg5.png) no-repeat right bottom; /* 设计插画背景 */
}
p {
text-indent: 2em;
}
W3C 于 21010 年 11月份才正式发布了渐变工作的工作草案,不过渐变设计并没有单独列为一个模块,而是作为图像值和图像被替换的一部分。该标准草案沿袭了 Gecko 引擎的渐变设计方法,语法和用法也基本相同。
linear-gradient([<angle>|to
<side-or-corner>,]?<color-stop>[,<color-stop>]+)
div {
width: 400px;
height: 200px;
border: 2px solid #fcf;
padding: 4px;
background-image: linear-gradient(to bottom, yellow, blue 30%, red);
}
repeating-linear-gradient()函数可以定义重复性渐变,用法与 linear-gradient() 语法相同。
radial-gradient([[<shape>|
|<size>][at <position>]?,| at
<position>,]?<color-stop>[,<color-stop>]+)
size 如果是 circle ,则指定一个圆的半径;如果是 ellipse ,指定两个值分别设置椭圆的 x 轴和 y 轴 的半径,取值包括长度、百分比、关键字。关键字为以下
farthest-corner :可渐变到离渐变起点最远的一个角
position 值位于 shape 和 size 的后面。