可以设置水平和垂直位置为百分比,或以像素为单位,或在水平方向上可以使用left/center/right,在垂直方向上可以使用top/center/bottom。位置起始于左上角。如果你不指定水平或垂直位置,它将默认为center。其工作方式主要包含:Top → Bottom、Left → Right、bottom → top、right → left等。
效果图:
代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CSS3 线性渐变</title>
<style type="text/css">
*{margin:0;padding:0;}
.linear_div *
{
width:200px;
height:200px;
margin:10px;
float:left;
}
.div1
{
background: -ms-linear-gradient(top,#ccc,#000);
background: -webkit-linear-gradient(top,#ccc,#000);
background: -o-linear-gradient(top,#ccc,#000);
background: -moz-linear-gradient(top,#ccc,#000);">
}
// 左到右
.div2
{
background: -ms-linear-gradient(left,#ace,#f96);
background: -webkit-linear-gradient(left,#ace,#f96);
background: -o-linear-gradient(left,#ace,#f96);
background: -moz-linear-gradient(left,#ace,#f96);
}
// 上到下
.div3
{
background: -ms-linear-gradient(top,#ace,#f96);
background: -webkit-linear-gradient(top,#ace,#f96);
background: -o-linear-gradient(top,#ace,#f96);
background: -moz-linear-gradient(top,#ace,#f96);
}
// 左上到右下
.div4
{
background: -ms-linear-gradient(top left,#ace,#f96);
background: -webkit-linear-gradient(top left,#ace,#f96);
background: -o-linear-gradient(top left,#ace,#f96);
background: -moz-linear-gradient(top left,#ace,#f96);
}
.div5
{
background: -ms-linear-gradient(left,#ace,#f96,#ace,#f96,#ace);
background: -webkit-linear-gradient(left,#ace,#f96,#ace,#f96,#ace);
background: -o-linear-gradient(left,#ace,#f96,#ace,#f96#,#ace);
background: -moz-linear-gradient(left,#ace,#f96,#ace,#f96,#ace);
}
</style>
</head>
<body>
<div class="linear_div">
<div class="div1"></div>
<div class="div2"></div>
<div class="div3"></div>
<div class="div4"></div>
<div class="div5"></div>
</div>
</body>
</html>