`
snoopy7713
  • 浏览: 1141007 次
  • 性别: Icon_minigender_2
  • 来自: 火星郊区
博客专栏
Group-logo
OSGi
浏览量:0
社区版块
存档分类
最新评论

CSS3 Gradient

    博客分类:
  • css
阅读更多

CSS3 发布很久了,现在在国外的一些页面上常能看到他的身影,这让我羡慕已久,只可惜在国内为了兼容IE,让这一项技术受到很大的限制,很多Web前端人员都望而止步。虽然如此但还是有很多朋友在钻研CSS3 在web中的应用,为了不被淘汰,我也开始向CSS3 进发,争取跟上技术的前沿。从现在开始我会不断的发布一些CSS3 的应用,和大家一起分享,今天我们首先要看的就是:CSS3: Gradient─CSS3渐变。

CSS3 Gradient 分为linear-gradient (线性渐变)和radial-gradient (径向渐变)。而我们今天主要是针对线性渐变来剖析其具体的用法。为了更好的应用CSS3 Gradient ,我们需要先了解一下目前的几种现代浏览器的内核,主流内容主要有Mozilla (熟悉的有Firefox,Flock等浏览器)、WebKit (熟悉的有Safari、Chrome等浏览器)、Opera (Opera 浏览器)、Trident(讨厌的IE浏览器)。本文照常忽略IE不管,我们主要看看在Mozilla、Webkit、Opera下的应用,当然在IE下 也可以实现,他需要通过IE特有的滤镜来实现,在后面会列出滤镜的使用语法,但不会具体介绍如何实用,感兴趣的可以搜索相关技术文档。那我们了解了这些, 现在就开始今天的主题吧。

CSS3的线性渐变

一、线性渐变在Mozilla下的应用

语法:

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

 

参数: 其共有三个参数,第一个参数表示线性渐变的方向,top是从上到下、left是从左到右,如果定义成left top,那就是从左上角到右下角。第二个和第三个参数分别是起点颜色和终点颜色。你还可以在它们之间插入更多的参数,表示多种颜色的渐变。如图所示:

根据上面的介绍,我们先来看一个简单的例子:

HTML:

<div class="example example1"></div>

 

CSS:

.example {
   width: 150px;
   height: 80px;
 }

 

(如无特殊说明,我们后面的示例都是应用这一段html和css 的基本代码)

现在我们给这个div应用一个简单的渐变样式:

.example1 {
   background: -moz-linear-gradient( top,#ccc,#000);
}

 

效果如下:

注:这个效果暂时只有在Mozilla内核的浏览器下才能正常显示。

二、线性渐变在Webkit下的应用

语法:

-webkit-linear-gradient( [<point> || <angle>,]? <stop>, <stop> [, <stop>]* )//最新发布书写语法
-webkit-gradient(<type>, <point> [, <radius>]?, <point> [, <radius>]? [, <stop>]*) //老式语法书写规则

 

参数: -webkit-gradient是webkit引擎对渐变的实现参数,一共有五个。第 一个参数表示渐变类型(type),可以是linear(线性渐变)或者radial(径向渐变)。第二个参数和第三个参数,都是一对值,分别表示渐变起 点和终点。这对值可以用坐标形式表示,也可以用关键值表示,比如 left top(左上角)和left bottom(左下角)。第四个和第五个参数,分别是两个color-stop函数。color-stop函数接受两个参数,第一个表示渐变的位置,0为 起点,0.5为中点,1为结束点;第二个表示该点的颜色。如图所示:

我们先来看一个老式的写法示例:

background: -webkit-gradient(linear,center top,center bottom,from(#ccc), to(#000));

 

效果如下所示

接着我们在来看一下新式的写法:

-webkit-linear-gradient(top,#ccc,#000);

 

这个效果我就不在贴出来了,大家在浏览器中一看就明白了,他们是否一致的效果。仔细对比,在Mozilla和Webkit下两者的 学法都基本上一致了,只是其前缀的区别,当然哪一天他们能统一成一样,对我们来说当然是更好了,那就不用去处理了。将大大节省我们的开发时间哟。

三、线性渐变在Opera下的应用

语法:

-o-linear-gradient([<point> || <angle>,]? <stop>, <stop> [, <stop>]); /* Opera 11.10+ */

 

参数:-o-linear-gradient有三个参数。第一个参数表示线性渐变的方向,top是从上到下、left是从左到右, 如果定义成left top,那就是从左上角到右下角。第二个和第三个参数分别是起点颜色和终点颜色。你还可以在它们之间插入更多的参数,表示多种颜色的渐变。 (注:Opera支持的版本有限,本例测试都是在Opera11.1版本下,后面不在提示),如图所示:

示例:

background: -o-linear-gradient(top,#ccc, #000);

 

效果如图所示

四、线性渐变在Trident (IE)下的应用

语法:

filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0, startColorstr=#1471da, endColorstr=#1C85FB);/*IE<9>*/
-ms-filter: "progid:DXImageTransform.Microsoft.gradient (GradientType=0, startColorstr=#1471da, endColorstr=#1C85FB)";/*IE8+*/

 

IE依靠滤镜实现渐变。startColorstr表示起点的颜色,endColorstr表示终点颜色。GradientType表示渐变类型,0为缺省值,表示垂直渐变,1表示水平渐变。如图所示:

上面我们主要介绍了线性渐变在上述四大核心模块下的实现方法,接着我们主要针对线性渐变在MOZ、Webkit、Opera三大模块下实现各种不同线性渐变实例:

从上面的语法中我们可以很清楚的知道,要创建一个线性渐变,我们需要创建一个起点和一个渐变方向(或角度),定义一个起始色:

-moz-linear-gradient( [<point> || <angle>,]? <stop>, <stop> [, <stop>]* )
-webkit-linear-gradient( [<point> || <angle>,]? <stop>, <stop> [, <stop>]* )
-o-linear-gradient( [<point> || <angle>,]? <stop>, <stop> [, <stop>]* )

 

具体应用如下:

background:-moz-linear-gradient(left,#ace,#f96);/*Mozilla*/
background:-webkit-gradient(linear,0 50%,100% 50%,from(#ace),to(#f96));/*Old gradient for webkit*/
background:-webkit-linear-gradient(left,#ace,#f96);/*new gradient for Webkit*/
background:-o-linear-gradient(left,#ace,#f96); /*Opera11*/

 

效果如下:

起 始点(Starting Point)的工作方式类似于background position。您可以设置水平和垂直位置为百分比,或以像素为单位,或在水平方向上可以使用left/center/right,在垂直方向上可以使 用top/center/bottom。位置起始于左上角。如果你不指定水平或垂直位置,它将默认为center。其工作方式主要包含:Top → Bottom、Left → Right、bottom → top、right → left等,接着我们主要一种一种来看其实现的效果:

1、开始于center(水平方向)和top(垂直方向)也就是Top → Bottom:

/* Firefox 3.6+ */
background: -moz-linear-gradient(top, #ace, #f96); 
/* Safari 4-5, Chrome 1-9 */ 
/* -webkit-gradient(,  [, ]?,  [, ]? [, ]*) */
background: -webkit-gradient(linear,top,from(#ace),to(#f96));
/* Safari 5.1+, Chrome 10+ */
background: -webkit-linear-gradient(top, #ace, #f96);
/* Opera 11.10+ */
background: -o-linear-gradient(top, #ace, #f96);

 

效果:

2、始于left(水平方向)和center(垂直方向)也是就Left → Right:

/* Firefox 3.6+ */
background: -moz-linear-gradient(left, #ace, #f96);
/* Safari 5.1+, Chrome 10+ */
background: -webkit-linear-gradient(left, #ace, #f96);
/* Opera 11.10+ */
background: -o-linear-gradient(left, #ace, #f96);

 

效果如下:

3、起始于left(水平方向)和top(垂直方向):

background: -moz-linear-gradient(left top, #ace, #f96);
background: -webkit-linear-gradient(left top, #ace, #f96);
background: -o-linear-gradient(left top, #ace, #f96);

 

效果如下:

4、Linear Gradient (with Even Stops):

/* Firefox 3.6+ */ 
background: -moz-linear-gradient(left, #ace, #f96, #ace, #f96, #ace); 
/* Safari 4-5, Chrome 1-9 */ 
background: -webkit-gradient(linear, left top, right top, from(#ace), color-stop(0.25, #f96), color-stop(0.5, #ace), color-stop(0.75, #f96), to(#ace)); 
/* Safari 5.1+, Chrome 10+ */ 
background: -webkit-linear-gradient(left, #ace, #f96, #ace, #f96, #ace); 
/* Opera 11.10+ */ 
background: -o-linear-gradient(left, #ace, #f96, #ace, #f96, #ace);

 

效果如下:

5、with Specified Arbitrary Stops:

 /* Firefox 3.6+ */ 
 background: -moz-linear-gradient(left, #ace, #f96 5%, #ace, #f96 95%, #ace); 
 /* Safari 4-5, Chrome 1-9 */ 
 background: -webkit-gradient(linear, left top, right top, from(#ace), color-stop(0.05, #f96), color-stop(0.5, #ace), color-stop(0.95, #f96), to(#ace)); 
 /* Safari 5.1+, Chrome 10+ */ 
 background: -webkit-linear-gradient(left, #ace, #f96 5%, #ace, #f96 95%, #ace); 
 /* Opera 11.10+ */ 
 background: -o-linear-gradient(left, #ace, #f96 5%, #ace, #f96 95%, #ace);

 

效果如下:

6、角度(Angle):

正如上面看到的示例,如果您不指定一个角度,它会根据起始位置自动定义。如果你想更多的控制渐变的方向,您不妨设置角度试试。例如,下面的两个渐变具有相同的起点left center,但是加上一个30度的角度。

没有角度的示例代码:

background: -moz-linear-gradient(left, #ace, #f96);
background: -webkit-linear-gradient(left,#ace,#f96);
background: -o-linear-gradient(left, #ace, #f96);

 

加上30度的角度代码:

background: -moz-linear-gradient(left 30deg, #ace, #f96);
background: -webkit-gradient(linear, 0 0, 100% 100%, from(#ace),to(#f96));
background: -o-linear-gradient(30deg, #ace, #f96);

 

效果图如下:

    

当指定的角度,请记住,它是一个由水平线与渐变线产生的的角度,逆时针方向。因此,使用0deg将产生一个左到右横向梯度,而90度将创建一个从底部到顶部的垂直渐变。我来看看你核心代码:

 
background: -moz-linear-gradient(<angle>, #ace, #f96);
background: -webkit-gradient(<type>,<angle>, from(#ace), to(#f96));
background: -webkit-linear-gradient(<angle>, #ace, #f96);
background: -o-linear-gradient(<angle>, #ace, #f96);

 

我们来看看各角度的区别

.deg0 {
  background: -moz-linear-gradient(0deg, #ace, #f96);
  background: -webkit-gradient(linear,0 50%,100% 50%,from(#ace),to(#f96));
  background: -webkit-linear-gradient(0deg, #ace, #f96);
  background: -o-linear-gradient(0deg, #ace, #f96);
}
    
.deg45 {
  background: -moz-linear-gradient(45deg, #ace, #f96);
  background: -webkit-gradient(linear,0 100%,100% 0%,from(#ace),to(#f96));
  background: -webkit-linear-gradient(45deg, #ace, #f96);
  background: -o-linear-gradient(45deg, #ace, #f96);
}
.deg90 {
  background: -moz-linear-gradient(90deg, #ace, #f96);
  background: -webkit-gradient(linear,50% 100%,50% 0%,from(#ace),to(#f96));
  background: -webkit-linear-gradient(90deg, #ace, #f96);
  background: -o-linear-gradient(90deg, #ace, #f96);
}
.deg135 {
  background: -moz-linear-gradient(135deg, #ace, #f96);
  background: -webkit-gradient(linear,100% 100%,0 0,from(#ace),to(#f96));
  background: -webkit-linear-gradient(135deg, #ace, #f96);
  background: -o-linear-gradient(135deg, #ace, #f96);
}
.deg180 {
  background: -moz-linear-gradient(180deg, #ace, #f96);
  background: -webkit-gradient(linear,100% 50%,0 50%,from(#ace),to(#f96));
  background: -webkit-linear-gradient(180deg, #ace, #f96);
  background: -o-linear-gradient(180deg, #ace, #f96);
}
.deg225 {
  background: -moz-linear-gradient(225deg, #ace, #f96);
  background: -webkit-gradient(linear,100% 0%,0 100%,from(#ace),to(#f96));
  background: -webkit-linear-gradient(225deg, #ace, #f96);
  background: -o-linear-gradient(225deg, #ace, #f96);
}
.deg270 {
  background: -moz-linear-gradient(270deg, #ace, #f96);
  background: -webkit-gradient(linear,50% 0%,50% 100%,from(#ace),to(#f96));
  background: -webkit-linear-gradient(270deg, #ace, #f96);
  background: -o-linear-gradient(270deg, #ace, #f96);
}
.deg315 {
  background: -moz-linear-gradient(315deg, #ace, #f96);
  background: -webkit-gradient(linear,0% 0%,100% 100%,from(#ace),to(#f96));
  background: -webkit-linear-gradient(315deg, #ace, #f96);
  background: -o-linear-gradient(315deg, #ace, #f96);
}
.deg360 {
  background: -moz-linear-gradient(360deg, #ace, #f96);
  background: -webkit-gradient(linear,0 50%,100% 50%,from(#ace),to(#f96));
  background: -webkit-linear-gradient(360deg, #ace, #f96);
  background: -o-linear-gradient(360deg, #ace, #f96);
}

 

效果如下:

除 了起始位置和角度,你应该指定起止颜色。起止颜色是沿着渐变线,将会在指定位置(以百分比或长度设定)含有指定颜色的点。色彩的起止数是无限的。如果您使 用一个百分比位置,0%代表起点和100%是终点,但区域外的值可以被用来达到预期的效果。 这也是通过CSS3 Gradient制作渐变的一个关键所在,其直接影响了你的设计效果,像我们这里的示例都不是完美的效果,只是为了能给大家展示一个渐变的效果,大家就这 样先用着吧。我们接着看一下不同的起址色的示例:

background: -moz-linear-gradient(top, #ace, #f96 80%, #f96);
background: -webkit-linear-gradient(top,#ace,#f96 80%,#f96);
background: -o-linear-gradient(top, #ace, #f96 80%, #f96);

 

效果如下:

如果没有指定位置,颜色会均匀分布。如下面的示例:

background: -moz-linear-gradient(left, red, #f96, yellow, green, #ace);
background: -webkit-linear-gradient(left,red,#f96,yellow,green,#ace);
background: -o-linear-gradient(left, red, #f96, yellow, green, #ace);

 

效果如下

7、渐变上应用透明─透明度(Transparency):

透明度还支持透明渐变。这对于制作一些特殊的效果是相当有用的,例如,当堆叠多个背景时。这里是两个背景的结合:一张图片,一个白色到透明的线性渐变。我们来看一个官网的示例吧:

background: -moz-linear-gradient(right, rgba(255,255,255,0), rgba(255,255,255,1)),url(http://demos.hacks.mozilla.org/openweb/resources/images/patterns/flowers-pattern.jpg);
background: -webkit-linear-gradient(right, rgba(255,255,255,0), rgba(255,255,255,1)),url(http://demos.hacks.mozilla.org/openweb/resources/images/patterns/flowers-pattern.jpg);
background: -o-linear-gradient(right, rgba(255,255,255,0), rgba(255,255,255,1)),url(http://demos.hacks.mozilla.org/openweb/resources/images/patterns/flowers-pattern.jpg);

 

接着看看效果吧

大家可以时入这里 和原图做一下比较,是不是很神奇呀。如果想体会的话,快点动手跟我一起做吧。

上面我们主要介绍了CSS3中线性渐变,文章一开始说过CSS3渐变包含两个部分,其一就是我们说的线性渐变,其二就是我们接下来要说的径向渐变。

CSS3的径向渐变

CSS3的径向渐变和其线性渐变是很相似的。我们首先来看其语法

 
 -moz-radial-gradient([<bg-position> || <angle>,]? [<shape> || <size>,]? <color-stop>, <color-stop>[, <color-stop>]*);
 -webkit-radial-gradient([<bg-position> || <angle>,]? [<shape> || <size>,]? <color-stop>, <color-stop>[, <color-stop>]*);

 

(需要特别说明一点的是,径向渐变到目前还不支持Opera的内核浏览器,所以我们径向渐变都是在firefox,safari,chrome底下进行测试完成的。)

除 了您已经在线性渐变中看到的起始位置,方向,和颜色,径向梯度允许你指定渐变的形状(圆形或椭圆形)和大小(最近端,最近角,最远端,最远角,包含或覆盖 (closest-side, closest-corner, farthest-side, farthest-corner, contain or cover))。 颜色起止(Color stops):就像用线性渐变,你应该沿着渐变线定义渐变的起止颜色。下面为了更好的理解其具体的用法,我们主要通过不同的示例来对比CSS3径向渐变的 具体用法

示例一:

 background: -moz-radial-gradient(#ace, #f96, #1E90FF);
 background: -webkit-radial-gradient(#ace, #f96, #1E90FF);

 

效果:

示例二:

 background: -moz-radial-gradient(#ace 5%, #f96 25%, #1E90FF 50%);
 background: -webkit-radial-gradient(#ace 5%, #f96 25%, #1E90FF 50%);

 

效果:

从 以上俩个示例的代码中发现,他们起止色想同,但就是示例二定位了些数据,为什么会造成这么大的区别呢?其实在径向渐变中虽然具有相同的起止色,但是在没有 设置位置时,其默认颜色为均匀间隔,这一点和我们前面的线性渐变是一样的,但是设置了渐变位置就会按照渐变位置去渐变,这就是我们示例一和示例的区别之 处:虽然圆具有相同的起止颜色,但在示例一为默认的颜色间隔均匀的渐变,而示例二每种颜色都有特定的位置。

示例三

  background: -moz-radial-gradient(bottom left, circle, #ace, #f96, #1E90FF);
  background: -webkit-radial-gradient(bottom left, circle, #ace, #f96, #1E90FF);

 

效果

示例四

background: -moz-radial-gradient(bottom left, ellipse, #ace, #f96, #1E90FF);
background: -webkit-radial-gradient(bottom left, ellipse, #ace, #f96, #1E90FF);

 

效果

示 例三和示例四我们从效果中就可以看出,其形状不一样,示例三程圆形而示例四程椭圆形状,也是就是说他们存在形状上的差异。然而我们在回到两个示例的代码 中,显然在示例三中设置其形状为circle而在示例四中ellipse,换而言之在径向渐变中,我们是可以会渐变设置其形状的。

示例五

background: -moz-radial-gradient(ellipse closest-side, #ace, #f96 10%, #1E90FF 50%, #f96);
background: -webkit-radial-gradient(ellipse closest-side, #ace, #f96 10%, #1E90FF 50%, #f96);

 

效果:

示例六

background: -moz-radial-gradient(ellipse farthest-corner, #ace, #f96 10%, #1E90FF 50%, #f96);
background: -webkit-radial-gradient(ellipse farthest-corner, #ace, #f96 10%, #1E90FF 50%, #f96);

 

效果:

从 示例五和示例六中的代码中我们可以清楚知道,在示例五中我人应用了closest-side而在示例六中我们应用了farthest-corner。这样 我们可以知道在径向渐变中我们还可以为其设置大小(Size):size的不同选项(closest-side, closest-corner, farthest-side, farthest-corner, contain or cover)指向被用来定义圆或椭圆大小的点。 示例:椭圆的近边VS远角 下面的两个椭圆有不同的大小。示例五是由从起始点(center)到近边的距离设定的,而示例六是由从起始点到远角的的距离决定的。

示例七:

background: -moz-radial-gradient(circle closest-side, #ace, #f96 10%, #1E90FF 50%, #f96);
background: -webkit-radial-gradient(circle closest-side, #ace, #f96 10%, #1E90FF 50%, #f96);

 

效果:

示例八:

background: -moz-radial-gradient(circle farthest-side, #ace, #f96 10%, #1E90FF 50%, #f96);
background: -webkit-radial-gradient(circle farthest-side, #ace, #f96 10%, #1E90FF 50%, #f96);

 

效果:

示例七和示例八主要演示了圆的近边VS远边 ,示例七的圆的渐变大小由起始点(center)到近边的距离决定,而示例八的圆则有起始点到远边的距离决定。

示例九:

background: -moz-radial-gradient(#ace, #f96, #1E90FF);
background: -webkit-radial-gradient(#ace, #f96, #1E90FF);

 

效果:

示例十:

background: -moz-radial-gradient(contain, #ace, #f96, #1E90FF);
background: -webkit-radial-gradient(contain, #ace, #f96, #1E90FF);

 

效果:

示例九和示例十演示了包含圆 。在这里你可以看到示例九的默认圈,同一渐变版本,但是被包含的示例十的圆。

最后我们在来看两个实例一个是应用了中心定位和full sized,如下所示:

 /* Firefox 3.6+ */ 
 background: -moz-radial-gradient(circle, #ace, #f96); 
 /* Safari 4-5, Chrome 1-9 */ 
 /* Can't specify a percentage size? Laaaaaame. */ 
 background: -webkit-gradient(radial, center center, 0, center center, 460, from(#ace), to(#f96)); 
 /* Safari 5.1+, Chrome 10+ */ 
 background: -webkit-radial-gradient(circle, #ace, #f96);         

 

效果如下:

下面这个实例应用的是Positioned, Sized,请看代码和效果

/* Firefox 3.6+ */ 
/* -moz-radial-gradient( [ || ,]? [ || ,]? , [, ]* ) */
background: -moz-radial-gradient(80% 20%, closest-corner, #ace, #f96); 
/* Safari 4-5, Chrome 1-9 */
background: -webkit-gradient(radial, 80% 20%, 0, 80% 40%, 100, from(#ace), to(#f96)); 
/* Safari 5.1+, Chrome 10+ */
background: -webkit-radial-gradient(80% 20%, closest-corner, #ace, #f96);

 

效果:

到此关于CSS3的两种渐变方式我们都介绍完了。在浪费大家一点时间,我们看看CSS的重复渐变的应用。

如果您想重复一个渐变,您可以使用-moz-repeating-linear-gradient和-moz-repeating-radial-gradient。 在下面的例子,每个实例都指定了两个起止颜色,并无限重复。

background: -moz-repeating-radial-gradient(#ace, #ace 5px, #f96 5px, #f96 10px);
background: -webkit-repeating-radial-gradient(#ace, #ace 5px, #f96 5px, #f96 10px);

 

 
background: -moz-repeating-linear-gradient(top left -45deg, #ace, #ace 5px, #f96 5px, #f96 10px);
background: -webkit-repeating-linear-gradient(top left -45deg, #ace, #ace 5px, #f96 5px, #f96 10px);

 

效果:

        

有关于CSS3渐变的东西就完了,大家看完了肯定会想,他主要用在哪些方面呢?这个说起来就多了,最简单的就是制作背景,我们还可以应用其制作一些漂亮的按钮,还可以用他来制作patterns,我在这里列出几种制作patterns的示例代码吧:

HTML代码:

<ul>
   <li class="gradient gradient1"></li>
   <li class="gradient gradient2"></li>
   <li class="gradient gradient3"></li>
   <li class="gradient gradient4"></li>
   <li class="gradient gradient5"></li>
   <li class="gradient gradient6"></li>
   </ul>

 

CSS 代码:

ul {
  overflow: hidden;
  margin-top: 20px;
}
li{
  width: 150px;
  height: 80px;
  margin-bottom: 10px;
  float: left;
  margin-right: 5px;
  background: #ace;
  /*Controls the size*/
  -webkit-background-size: 20px 20px;
  -moz-background-size: 20px 20px;
  background-size: 20px 20px; 
}
    
li.gradient1 {
  background-image: -webkit-gradient(
    linear,
    0 100%, 100% 0,
    color-stop(.25, rgba(255, 255, 255, .2)), 
    color-stop(.25, transparent),
    color-stop(.5, transparent), 
    color-stop(.5, rgba(255, 255, 255, .2)),
    color-stop(.75, rgba(255, 255, 255, .2)), 
    color-stop(.75, transparent),
    to(transparent)
    );
  background-image: -moz-linear-gradient(
    45deg, 
    rgba(255, 255, 255, .2) 25%, 
    transparent 25%,
    transparent 50%, 
    rgba(255, 255, 255, .2) 50%, 
    rgba(255, 255, 255, .2) 75%,
    transparent 75%, 
    transparent
    );
  background-image: -o-linear-gradient(
    45deg, 
    rgba(255, 255, 255, .2) 25%, 
    transparent 25%,
    transparent 50%, 
    rgba(255, 255, 255, .2) 50%, 
    rgba(255, 255, 255, .2) 75%,
    transparent 75%, 
    transparent
  );
  background-image: linear-gradient(
    45deg, 
    rgba(255, 255, 255, .2) 25%, 
    transparent 25%,
    transparent 50%, 
    gba(255, 255, 255, .2) 50%, 
    rgba(255, 255, 255, .2) 75%,
    transparent 75%, 
    transparent
    );
}

li.gradient2 {
   background-image: -webkit-gradient(linear, 0 0, 100% 100%,
      color-stop(.25, rgba(255, 255, 255, .2)), color-stop(.25, transparent),
      color-stop(.5, transparent), color-stop(.5, rgba(255, 255, 255, .2)),
      color-stop(.75, rgba(255, 255, 255, .2)), color-stop(.75, transparent),
      to(transparent));
   background-image: -moz-linear-gradient(-45deg, rgba(255, 255, 255, .2) 25%, transparent 25%,
      transparent 50%, rgba(255, 255, 255, .2) 50%, rgba(255, 255, 255, .2) 75%,
      transparent 75%, transparent);
   background-image: -o-linear-gradient(-45deg, rgba(255, 255, 255, .2) 25%, transparent 25%,
      transparent 50%, rgba(255, 255, 255, .2) 50%, rgba(255, 255, 255, .2) 75%,
      transparent 75%, transparent);
   background-image: linear-gradient(-45deg, rgba(255, 255, 255, .2) 25%, transparent 25%,
      transparent 50%, rgba(255, 255, 255, .2) 50%, rgba(255, 255, 255, .2) 75%,
      transparent 75%, transparent);
}
    
li.gradient3 {
  background-image: -webkit-gradient(linear, 0 0, 0 100%, color-stop(.5, rgba(255, 255, 255, .2)), color-stop(.5, transparent), to(transparent));
  background-image: -moz-linear-gradient(rgba(255, 255, 255, .2) 50%, transparent 50%, transparent);
  background-image: -o-linear-gradient(rgba(255, 255, 255, .2) 50%, transparent 50%, transparent);
  background-image: linear-gradient(rgba(255, 255, 255, .2) 50%, transparent 50%, transparent);
}
    
li.gradient4 {
  background-image: -webkit-gradient(linear, 0 0, 100% 0, color-stop(.5, rgba(255, 255, 255, .2)), color-stop(.5, transparent), to(transparent));
  background-image: -moz-linear-gradient(0deg, rgba(255, 255, 255, .2) 50%, transparent 50%, transparent);
  background-image: -o-linear-gradient(0deg, rgba(255, 255, 255, .2) 50%, transparent 50%, transparent);
  background-image: linear-gradient(0deg, rgba(255, 255, 255, .2) 50%, transparent 50%, transparent);
}
    
li.gradient5 {
  background-image: -webkit-gradient(linear, 0 0, 100% 100%, color-stop(.25, #555), color-stop(.25, transparent), to(transparent)),
      -webkit-gradient(linear, 0 100%, 100% 0, color-stop(.25, #555), color-stop(.25, transparent), to(transparent)),
      -webkit-gradient(linear, 0 0, 100% 100%, color-stop(.75, transparent), color-stop(.75, #555)),
      -webkit-gradient(linear, 0 100%, 100% 0, color-stop(.75, transparent), color-stop(.75, #555));
  background-image: -moz-linear-gradient(45deg, #555 25%, transparent 25%, transparent),
     -moz-linear-gradient(-45deg, #555 25%, transparent 25%, transparent),
     -moz-linear-gradient(45deg, transparent 75%, #555 75%),
     -moz-linear-gradient(-45deg, transparent 75%, #555 75%);
  background-image: -o-linear-gradient(45deg, #555 25%, transparent 25%, transparent),
     -o-linear-gradient(-45deg, #555 25%, transparent 25%, transparent),
     -o-linear-gradient(45deg, transparent 75%, #555 75%),
     -o-linear-gradient(-45deg, transparent 75%, #555 75%);
  background-image: linear-gradient(45deg, #555 25%, transparent 25%, transparent),
    linear-gradient(-45deg, #555 25%, transparent 25%, transparent),
    linear-gradient(45deg, transparent 75%, #555 75%),
    linear-gradient(-45deg, transparent 75%, #555 75%);
}
    
li.gradient6 {
  background-image: -webkit-gradient(linear, 0 0, 0 100%, color-stop(.5, transparent), color-stop(.5, rgba(200, 0, 0, .5)), to(rgba(200, 0, 0, .5))),
     -webkit-gradient(linear, 0 0, 100% 0, color-stop(.5, transparent), color-stop(.5, rgba(200, 0, 0, .5)), to(rgba(200, 0, 0, .5)));
  background-image: -moz-linear-gradient(transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)),
     -moz-linear-gradient(0deg, transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5));
  background-image: -o-linear-gradient(transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)),
     -o-linear-gradient(0deg, transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5));
  background-image: linear-gradient(transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5)),
     linear-gradient(0deg, transparent 50%, rgba(200, 0, 0, .5) 50%, rgba(200, 0, 0, .5));
}

 

 效果:

不错的效果吧,当然感兴趣的朋友可以到这里 学习制作更多的不同效果。

到此我们关于CSS3─Gradient就介绍到这里了,如果想学习制作渐变效果的,大家可以看看W3CPLUS 首页的彩色菜单,我采用的就是CSS3 Gradient实现的,当然大家还可以去看看这个DEMO 的效果。希望能给大家对学习CSS3有点帮助,如果感兴趣的朋友请观注W3CPLUS ,从今天开始我会系统的介绍一些CSS3的应用,希望大家能喜欢,更希望能跟大家一起探讨学习CSS3的相关技术。

更新一

今天为之 碰到一个IE9下的渐变色结合圆角制作的bug。换句话说:在IE9下你使用渐变色而且还需要制作圆角时,这个bug就出现了,在IE9下背景色不能完全切完。经过他仔细研究,借助Ultimate CSS Gradient Generator 工具终于找到了兼容IE9的方法,请看下面的截图:

我将代码截取如下:

/* IE9 SVG, needs conditional override of 'filter' to 'none' */
background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzJjZTA2MiIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiM3ZGI5ZTgiIHN0b3Atb3BhY2l0eT0iMCIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
background: -moz-linear-gradient(top,  rgba(44,224,98,1) 0%, rgba(125,185,232,0) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(44,224,98,1)), color-stop(100%,rgba(125,185,232,0))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top,  rgba(44,224,98,1) 0%,rgba(125,185,232,0) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top,  rgba(44,224,98,1) 0%,rgba(125,185,232,0) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top,  rgba(44,224,98,1) 0%,rgba(125,185,232,0) 100%); /* IE10+ */
background: linear-gradient(top,  rgba(44,224,98,1) 0%,rgba(125,185,232,0) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#2ce062', endColorstr='#007db9e8',GradientType=0 ); /* IE6-8 */
filter: none\9\0;/*ie9*/

下面列出本站关于CSS3的相关文章:

下一节: 《CSS3 RGBA

第三节: 《CSS3的圆角border-radius

第四节:《CSS3的文字阴影text-shadow

第五节:《CSS3 Box-shadow

第六节:《CSS3 Transform

第七节:《CSS3 Transition

第八节:《CSS3 Animation

如需转载请注明出处:W3CPLUS

分享到:
评论

相关推荐

    纯css3 gradient属性制作6种渐变色的按钮样式

    在网页设计中,CSS3(层叠样式表第三版)为开发者提供了许多强大的新特性,其中之一就是gradient(渐变)属性。本教程将详细介绍如何利用纯CSS3的gradient属性来制作6种不同颜色的渐变按钮样式,以提升网页界面的...

    纯css3 gradient颜色渐变立体文字特效代码

    随着技术的发展,CSS3引入了许多新的特性,其中包括gradient(渐变)效果,使得设计更加丰富多彩。本篇将深入探讨如何使用纯CSS3的gradient颜色渐变来创建立体文字特效。 首先,让我们理解CSS3中的两种主要类型的...

    纯CSS3 gradient属性制作36种漂亮的html网页渐变按钮样式.zip

    在网页设计中,CSS3(层叠样式表第三版)为开发者带来了许多强大的新特性,其中之一就是gradient属性。这个属性允许我们创建出丰富多彩、动态感十足的渐变效果,极大地提升了网页界面的视觉吸引力。本资源"纯CSS3 ...

    36种漂亮的CSS3gradient属性制作的html网页渐变按钮样式

    CSS3的gradient属性为创建各种动态、美观的按钮样式提供了强大的工具。本篇将详细介绍36种利用CSS3 gradient属性制作的HTML网页渐变按钮样式,并探讨如何运用这些技巧来提升网页设计的质量。 1. 渐变类型: CSS3...

    HTML5 CSS3 gradient、box-shadow按钮特效代码大全.rar

    HTML5 CSS3使用 gradient、box-shadow实现的按钮特效代码大全,带阴影的按钮,个个都很漂亮的CSS3按钮,你借助这些按钮效果,也可进一步学习下gradient、box-shadow的具体用法,对提高CSS3的编程水平有帮助。

    CSS3 Gradient and Shadow Generator-crx插件

    **CSS3 Gradient and Shadow Generator-crx插件** CSS3 Gradient and Shadow Generator是一款强大的浏览器扩展程序,专为前端开发者设计,帮助他们快速、便捷地创建和编辑CSS3中的渐变和盒子阴影效果。这款插件提供...

    前端开源库-postcss-filter-gradient

    渐变在现代CSS中是一种常见的效果,但IE8至IE9并不支持CSS3的渐变语法。这个插件能够帮助开发者在保持现代语法的同时,确保在旧版IE中也能展示出相似的渐变效果。 **渐变语法的转换** 在现代CSS中,我们可以使用`...

    CSS3,线性渐变(linear-gradient)的使用总结

    《CSS3 经典教程系列》的前一篇文章向大家详细介绍了 text-shadow 文本阴影特性的用法,今天这篇文章我们在一起来看看 CSS3 中实现渐变效果的 Gradient 属性的具体用法。在以前,渐变效果和阴影、圆角效果一样都是做...

    CSS3渐变和阴影生成器「CSS3 Gradient and Shadow Generator」-crx插件

    该扩展允许用户立即生成CSS3渐变和Box Shadow片段。 CSS渐变和Box Shadow Generator-类似于Photoshop的强大CSS渐变和阴影编辑器 支持语言:English

    css-gradient_脚本实例_

    在网页设计中,CSS渐变(CSS Gradient)是一种强大的工具,可以为元素添加丰富多彩的背景效果,提升视觉吸引力。在本实例中,我们将探讨如何利用jQuery来美化链接文本,实现渐变样式的应用。 首先,CSS渐变分为线性...

    CSS3中border-radius、box-shadow与gradient那点事儿

    在CSS3中,border-radius、box-shadow和gradient是三个非常重要的样式属性,它们极大地提升了网页设计的美观性和表现力。让我们逐一深入探讨这三个概念。 首先,`border-radius`属性允许我们为元素的边框添加圆角,...

    详解CSS3中使用gradient实现渐变效果的方法

    CSS3 Gradient分为linear-gradient(线性渐变)和radial-gradient(径向渐变)。线性渐变在Webkit下的应用 CSS Code复制内容到剪贴板 -webkit-linear-gradient( [ || ,]? ,  [, ]* )//最新发布书写语法   第...

    HTML5 CSS3 chm中文参考手册_html5_CSS3_css3.chm_参考手册_

    此外,CSS3还增加了对边框半径(border-radius)、阴影(box-shadow)和渐变(linear-gradient、radial-gradient)的支持,使得网页设计更加丰富多样。 压缩包中的"CSS3.0(飘零雾雨版).chm"很可能是针对CSS3的一...

    css3网页动态渐变背景动画特效

    在CSS3中,我们可以使用`linear-gradient()`函数来定义渐变。基本语法如下: ```css background-image: linear-gradient(direction, color-stop1, color-stop2, ...); ``` 这里的`direction`参数指定了渐变的方向...

    css3参考手册

    **CSS3参考手册详解** CSS3(层叠样式表第三版)是Web设计领域中一个重要的里程碑,它扩展了CSS2.1的功能,引入了许多新的特性和改进,为网页设计师提供了更为丰富的表现力和控制力。这篇详尽的指南将深入解析CSS3...

    ie-css3.htc 免费版

    3. **linear-gradient**:线性渐变是CSS3中的一种背景效果,ie-css3.htc通过创建多个背景层并逐步改变颜色来模拟这一效果,使得在不支持CSS3的IE浏览器上也可以呈现渐变背景。 使用ie-css3.htc的方法很简单,只需要...

    css3-image-gradient-hover.zip

    "css3-image-gradient-hover.zip"这个压缩包可能包含了一个示例项目,展示了如何使用CSS3的渐变效果来实现图片的悬停效果。这种效果通常用于增加用户交互性,当鼠标悬停在图像上时,图像的颜色或背景会逐渐变化,...

Global site tag (gtag.js) - Google Analytics