在CSS中可以使用linear-gradient()和radial-gradient()函数来实现颜色的线性渐变和径向渐变。
linear-gradient() 函数--线性渐变
linear-gradient() 函数用于创建一个线性渐变的 "图像"。
为了创建一个线性渐变,你需要设置一个起始点和一个方向(指定为一个角度)的渐变效果。你还要定义终止色。终止色就是你想让Gecko去平滑的过渡,并且你必须指定至少两种,当然也会可以指定更多的颜色去创建更复杂的渐变效果。
linear-gradient(<point> || <angle>, <stop>, <stop> , <stop>)
第一个参数是渐变起始点或角。第二个参数是一种颜色停止点(color stops)。至少需要两种颜色(起点和终点),你可以添加任意种颜色来增加颜色渐变的丰富程度。对颜色停止点的定义可以是一种颜色,或一种颜色加一个百分比。
代码(考虑浏览器兼容性):
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>线性渐变</title> <style> .demo{ width:500 ; height: 300; margin: 50px auto; } .demo *{ width: 200px; height: 200px; margin: 20px; text-align: center; line-height: 200px; color: #fff; font-size: 16px; float: left; } .demo1{ /* 底色 */ background-color: #fd0d0d; /* chrome 2+, safari 4+; multiple color stops */ background-image:-webkit-gradient(linear, left bottom, left top, color-stop(0.32, #fd0d0d), color-stop(0.66, #d89e3c), color-stop(0.83, #97bb51)); /* chrome 10+, safari 5.1+ */ background-image: -webkit-linear-gradient(#fd0d0d, #d89e3c, #97bb51); /* firefox; multiple color stops */ background-image: -moz-linear-gradient(top,#fd0d0d, #d89e3c, #97bb51); /* ie 6+ */ filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fd0d0d',endColorstr='#d89e3c'); /* ie8 + */ -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr='#fd0d0d', endColorstr='#d89e3c')"; /* ie10 */ background-image: -ms-linear-gradient(#fd0d0d, #d89e3c, #97bb51); /* opera 11.1 */ background-image: -o-linear-gradient(#fd0d0d, #d89e3c, #97bb51); /* 标准写法 */ background-image: linear-gradient(#fd0d0d, #d89e3c, #97bb51); } .demo2{ /* 底色 */ background-color:#d41a1a; /* chrome 2+, safari 4+; multiple color stops */ background-image:-webkit-gradient(linear, left bottom, right top, color-stop(0.32, #d41a1a), color-stop(0.66, #d9e60c), color-stop(0.83, #5c7c99)); /* chrome 10+, safari 5.1+ */ background-image:-webkit-linear-gradient(45deg, #d41a1a, #d9e60c, #5c7c99); /* firefox; multiple color stops */ background-image:-moz-linear-gradient(45deg, #d41a1a, #d9e60c, #5c7c99); /* ie10 */ background-image: -ms-linear-gradient(45deg, #d41a1a 0%, #d9e60c 100%); /* opera 11.1 */ background-image: -o-linear-gradient(45deg, #d41a1a, #d9e60c); /* 标准写法 */ background-image: linear-gradient(45deg, #d41a1a, #d9e60c); } </style> </head> <body> <div class="demo"> <div class="demo1">基本线性渐变--自上而下</div> <div class="demo2">基本线性渐变--45度角</div> </div> </body> </html>
效果图:
radial-gradient()函数--径向渐变
radial-gradient() 函数用径向渐变创建 "图像"。
径向渐变由中心点定义。
为了创建径向渐变你必须设置两个终止色。
css径向颜色渐变(Radial Gradients)跟线性渐变(linear gradients)不一样,它不是沿着一个方向渐变,而是以一个点为中心,向四周辐射渐变,360度的。
语法:
radial-gradient(shape size at position, start-color, ..., last-color)
值 | 描述 |
---|---|
shape | 确定圆的类型:
|
size | 定义渐变的大小,可能值:
|
position | 定义渐变的位置。可能值:
|
start-color, ..., last-color | 用于指定渐变的起止颜色。 |
示例:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>径向渐变</title> <style> .demo{ width:500 ; height: 300; margin: 50px auto; } .demo *{ width: 200px; height: 200px; margin: 20px; text-align: center; line-height: 200px; color: #fff; font-size: 16px; float: left; } .demo1{ background-image: -moz-radial-gradient(#ecff05, red); /* old */ background-image: -webkit-gradient(radial, center center, 0, center center, 220, from(#ecff05), to(red)); /* new syntax */ background-image: -webkit-radial-gradient(#ecff05, red); background-image: radial-gradient(#ecff05, red); } .demo2{ background-image: -moz-radial-gradient(45px 45px 45deg, circle cover, #ecff05 0%, orange 100%, red 95%); background-image: -webkit-radial-gradient(45px 45px, circle cover, #ecff05, red); background-image: radial-gradient(45px 45px 45deg, circle cover, #ecff05 0%, orange 100%, red 95%); } </style> </head> <body> <div class="demo"> <div class="demo1">径向渐变</div> <div class="demo2">径向渐变</div> </div> </body> </html>
效果图:
更多CSS相关知识,可访问 CSS教程 !!
以上就是css3如何实现颜色渐变?的详细内容,更多请关注0133技术站其它相关文章!