css使用linear-gradient设置div背景渐变色小结


css使用linear-gradient设置div背景渐变色小结
( 预览   )

在一段相当长的时间里,在做制作页面背景渐变的效果的时候,都是使用ps做成效果图,然后再切图再设为背景。在这样做的时候,总有个念头出来,能不能使用css的linear-gradient设置div的背景渐变色呢?这里就是本人查找了一些资料后的小结,简供大家参看。

css3:linear-gradient

比如:黑色渐变到白色,代码如下:

.gradient{
    background: -moz-linear-gradient(top, #000000 0%, #ffffff 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#000000), color-stop(100%,#ffffff));
    background: -webkit-linear-gradient(top, #000000 0%,#ffffff 100%);
    background: -o-linear-gradient(top, #000000 0%,#ffffff 100%);
    background: -ms-linear-gradient(top, #000000 0%,#ffffff 100%);
    background: linear-gradient(to bottom, #000000 0%,#ffffff 100%);
}

附上linear-gradient说明:

语法:

<linear-gradient>:linear-gradient([ <point>,]? <color-stop>[, <color-stop>]+);

<point>:[ left | right ]? [ top | bottom ]? || <angle>?

<color-stop>:<color> [ <length> | <percentage> ]?

取值:

<point>

left:

    设置左边为渐变起点的横坐标值。

right:

    设置右边为渐变起点的横坐标值。

top:

    设置顶部为渐变起点的纵坐标值。

bottom:

    设置底部为渐变起点的纵坐标值。

<angle>:

    用角度值指定渐变的方向(或角度)。

<color-stop>:

    指定渐变的起止颜色。

<color-stop>

<color>:

    指定颜色。请参阅颜色值

<length>:

    用长度值指定起止色位置。不允许负值

<percentage>:

    用百分比指定起止色位置。

说明:

用线性渐变创建图像。

Firefox还支持使用<percentage>、<length>和center特殊值定义渐变起点,并支持起点与角度一起使用。


11.jpg

ie 滤镜:filter

linear-gradient 在 ie9 以下是不支持的,所以对于 ie6 - ie8 我们可以使用滤镜来解决,代码如下:

.gradient{
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#000000', endColorstr='#ffffff',GradientType=0 );
}

由于 filter 是 ie 的私有属性,所以我们需要针对 ie9 单独处理滤镜效果,代码如下:

:root {filter:none;}


结合上面的叙述,线性渐变的css如下设置:

.gradient{
    background: #000000;
    background: -moz-linear-gradient(top,  #000000 0%, #ffffff 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#000000), color-stop(100%,#ffffff));
    background: -webkit-linear-gradient(top,  #000000 0%,#ffffff 100%);
    background: -o-linear-gradient(top,  #000000 0%,#ffffff 100%);
    background: -ms-linear-gradient(top,  #000000 0%,#ffffff 100%);
    background: linear-gradient(to bottom,  #000000 0%,#ffffff 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#000000', endColorstr='#ffffff',GradientType=0 );
}
:root .gradient{filter:none;}


注:本文转载自拾图网[http://www.tshinet.com],转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除。
上一篇 下一篇

淘宝热销

  • Mistine小黄帽防晒霜乳SPF50泰版面部防紫外线隔离男女军训蜜丝婷

    【推荐理由】赠运费险

    【优惠券】20   【券后价】59.00

    【下单链接】18¥ CZ0001 G5fHdEqOsnB¥

    >>>>>> 马上打开

  • G7旗舰店越南进口美式纯黑咖啡粉速溶无糖0脂减燃正品学生提神

    【推荐理由】库存紧张,好评过万

    【优惠券】3    【券后价】26.90

    【下单链接】27¥ CZ0001 zb0bdEqLV7G¥

    >>>>>> 马上打开