CSS3属性之线性渐变

名称:linear-gradient


语法如下

-webkit-gradien(<type>,<point>[,<radius>]?,<point>[,<radius>]?[,<stop>]*)  // webkit老式语

-webkit-linear-gradient([<point>||<angle>,]?<stop>,<stop>[,<stop>]*)  // webkit语法

-moz-linear-gradient([<point>||<angle>,]?<stop>,<stop>[,<stop>]*)  // Gecko语法

-o-linear-gradient([<point>||<angle>,]?<stop>,<stop>[,<stop>]*)  // Presto语法

linear-gradient([[<angle> | to <side-or-corner> ],]?<color-stop>[,<color-stop>]+)  // W3C语法


说明:语法基本划分3种,一种是webkit的老式语法,一种是Webkit、webkit、Presto的语法(加上各自的私有属性,语法都一样)、一种是W3C的语法(目前个各浏览器最新版基本都支持),虽然语法不一致,但包括的参数基本一致,如渐变的方向以及渐变断点的位置及颜色。


一些demo


html代码:

<div class="linear-gradient1">从左到右</div>
<div class="linear-gradient2">从右到左</div>
<div class="linear-gradient3">从上到下</div>
<div class="linear-gradient4">从下到上</div>
<div class="linear-gradient5">从左到右,多色渐变</div>
<div class="linear-gradient6">多色渐变,指定颜色范围</div>
<div class="linear-gradient7">重复渐变-1</div>
<div class="linear-gradient8">重复渐变-2</div>


css代码:

div {
  width: 160px;
  height: 80px;
  margin: 10px;
  border: 1px solid black;
  line-height: 80px;
  text-align: center;
  font-size: 12px;
}
.linear-gradient1 {
  background-image: -webkit-gradient(linear, left center, right center, color-stop(0%, red), color-stop(100%, white));
  background-image: -webkit-linear-gradient(left, red, white);
  background-image: -moz-linear-gradient(left, red, white);
  background-image: -o-linear-gradient(left, red, white);
  background-image: linear-gradient(to right, red, white);
}
.linear-gradient2 {
  background-image: -webkit-gradient(linear, right center, left center, color-stop(0%, red), color-stop(100%, white));
  background-image: -webkit-linear-gradient(right, red, white);
  background-image: -moz-linear-gradient(right, red, white);
  background-image: -o-linear-gradient(right, red, white);
  background-image: linear-gradient(to left, red, white);
}
.linear-gradient3 {
  background-image: -webkit-gradient(linear, center top, center bottom, color-stop(0%, red), color-stop(100%, white));
  background-image: -webkit-linear-gradient(top, red, white);
  background-image: -moz-linear-gradient(top, red, white);
  background-image: -o-linear-gradient(top, red, white);
  background-image: linear-gradient(to bottom, red, white);
}
.linear-gradient4 {
  background-image: -webkit-gradient(linear, center bottom, center top, color-stop(0%, red), color-stop(100%, white));
  background-image: -webkit-linear-gradient(bottom, red, white);
  background-image: -moz-linear-gradient(bottom, red, white);
  background-image: -o-linear-gradient(bottom, red, white);
  background-image: linear-gradient(to top, red, white);
}
.linear-gradient5 {
  background-image: -webkit-gradient(linear, left center, right center, color-stop(0%, red), color-stop(25%, green), color-stop(50%, blue), color-stop(75%, orange), color-stop(100%, white));
  background-image: -webkit-linear-gradient(left, red, green, blue, orange, white);
  background-image: -moz-linear-gradient(left, red, green, blue, orange, white);
  background-image: -o-linear-gradient(left, red, green, blue, orange, white);
  background-image: linear-gradient(to right, red, green, blue, orange, white);
}
.linear-gradient6 {
  background-image: -webkit-gradient(linear, left center, right center, color-stop(0%, red), color-stop(40%, green), color-stop(50%, blue), color-stop(60%, orange), color-stop(100%, white));
  background-image: -webkit-linear-gradient(left, red 0%, green 40%, blue 50%, orange 60%, white 100%);
  background-image: -moz-linear-gradient(left, red 0%, green 40%, blue 50%, orange 60%, white 100%);
  background-image: -o-linear-gradient(left, red 0%, green 40%, blue 50%, orange 60%, white 100%);
  background-image: linear-gradient(to right, red 0%, green 40%, blue 50%, orange 60%, white 100%);
}
.linear-gradient7 {
  background-image: -webkit-repeating-linear-gradient(left, red 0, red 14px, white 15px, white 29px);
  background-image: -moz-repeating-linear-gradient(left, red 0, red 14px, white 15px, white 29px);
  background-image: -o-repeating-linear-gradient(left, red 0, red 14px, white 15px, white 29px);
  background-image: repeating-linear-gradient(to right, red 0, red 14px, white 15px, white 29px);
}
.linear-gradient8 {
  background-image: -webkit-repeating-linear-gradient(45deg, red 0, red 14px, white 15px, white 29px);
  background-image: -moz-repeating-linear-gradient(45deg, red 0, red 14px, white 15px, white 29px);
  background-image: -o-repeating-linear-gradient(45deg, red 0, red 14px, white 15px, white 29px);
  background-image: repeating-linear-gradient(45deg, red 0, red 14px, white 15px, white 29px);
}


效果:


兼容性(来自http://caniuse.com

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值