CSS3渐变:gradient

CSS3渐变:gradientWrite By Monkeyfly 以下内容均为原创 如需转载请注明出处 一 名词解释 gradient 英 gre d nt n 渐变 梯度 radial 英 re d l adj 半径的 放射状的 n 射线 光线 二 渐变的分类

大家好,我是讯享网,很高兴认识大家。

Write By Monkeyfly

以下内容均为原创,如需转载请注明出处。

一、名词解释:

gradient 英 ['greɪdɪənt] n.渐变 梯度 radial 英 ['reɪdɪəl] adj. 半径的;放射状的;n. 射线,光线

讯享网

二、渐变的分类:

CSS3的渐变分为两种:

讯享网线性渐变:linear-gradient;//向下/向上/向左/向右/对角方向 径向渐变:radial-gradient;//由它们的中心定义

三、线性渐变

前提:在使用前,我们必须知道渐变要从哪个方向开始。

【默认方向:从上到下】

说明:

  1. 为了创建一个线性渐变,你必须至少定义两种颜色结点。(颜色结点,即你想要呈现平稳过渡的颜色。)
  2. 同时,你也可以设置一个起点和一个方向(或一个角度)。
  3. 起点是用于对角渐变的,比如:从左下方开始,从右上方开始等。
3.1.方向渐变

语法:

<!--默认方向:从上到下--> background:linear-gradient(direction,color-stop1,color-stop2,...);

举例:

讯享网background:linear-gradient(red,yellow);

注意:IE 9 及之前的版本不支持渐变。

方向渐变的两种写法:

  • 指定方向:to right
  • 指定了起点:left
/* 从左到右*/ background:linear-gradient(left,green,blue); background:linear-gradient(to right,green,blue);

对角渐变的两种写法:

  • 指定方向:to bottom right
  • 指定了起点:top left
讯享网/*对角:左上->右下*/ background:linear-gradient(to right bottom,#000,gold); background:linear-gradient(top left,#000,gold);
3.2. 使用角度

你可以定义一个角度,而不用预定义方向(to bottom、to top、to right、to left、to bottom right,等等)。

语法:

background:linear-gradient(angle, color-stop1, color-stop2);

说明:

  • 角度是指水平线和渐变线之间的角度,逆时针方向计算。
  • 换句话说,0deg将创建一个从下到上的渐变,90deg将创建一个从左到右的渐变。

举例:

讯享网background:linear-gradient(30deg,red,yellow);
3.3.使用多个颜色结点

语法:

background:linear-gradient(red,yellow,blue);
3.4.不同比例的多种颜色渐变

语法:


讯享网

讯享网background:linear-gradient(red 20%,yellow 60%,blue 80%); <!-- 红色从20%的位置开始,一直渐变到60%的位置; 黄色从60%的位置开始,一直渐变到80%的位置; 蓝色从80%的位置开始,一直渐变到结束为止。 -->
3.5.使用透明度(transparent)

使用透明度可以用来将渐变的颜色减弱。

语法:

background:linear-gradient(left,rgba(255,0,0,0),rgba(255,255,0,1));

说明:

  • 为了添加透明度,我们使用rgba()函数来定义颜色结点。
  • rgba()函数中的最后一个参数可以是从 0 到 1 的值。它定义了颜色的透明度:0 表示完全透明,1 表示完全不透明。
3.6.重复的线性渐变

repeating-linear-gradient()函数用于重复线性渐变:

语法:

讯享网 background: repeating-linear-gradient(red, yellow 10%, green 20%);

说明:必须给不同的颜色结点不同的比例大小,如果不指定比例,是没办法重复出现的。

四、径向渐变

径向渐变由它的中心定义。

  • 为了创建一个径向渐变,你也必须至少定义两种颜色结点。
  • 同时,你也可以指定渐变的中心、形状(圆形或椭圆形)、大小。

说明:

  1. 默认情况下,渐变的中心是 center(表示在中心点)。
  2. 渐变的形状是 ellipse(表示椭圆形)。
  3. 渐变的大小是 farthest-corner(表示到最远的角落)。

语法:

background: radial-gradient(center, shape size, start-color, ..., last-color);

单词解释:

讯享网eclipse 英 [ɪ'klɪps] n. 日蚀,月蚀;黯然失色 ellipse 英 [ɪ'lɪps] n. [数] 椭圆形,[数] 椭圆 ellipsis 英 [ɪ'lɪpsɪs] n. 省略;省略符号;
4.1. 颜色结点均匀分布(默认情况下)

语法:

background: radial-gradient(red,pink,green,yellow);
4.2.颜色结点不均匀分布

语法:

讯享网background: radial-gradient(red 10%,pink 40%,green 50%,yellow 70%);
4.3.设置形状

语法:

<!--默认为ellipse(椭圆形),还可以设置为circle(圆形)--> background: radial-gradient(ellipse,red,yellow,green); background: radial-gradient(circle,red,yellow,green);
4.4.重复的径向渐变

语法:

讯享网background: repeating-radial-gradient(circle,orange,yellow 5%,green 10%);

五、练习示例:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>CSS3渐变</title> <style> #wrap{ display: inline-block; margin-right: 10px; height:auto; /*margin-top: 10px;*/ text-align: center; } div{ width: 200px; height: 200px; } /*一、线性渐变 语法:background:linear-gradient(direction,color-stop1,color-stop2,...); */ /*1.指定方向渐变*/ #box1{ /*默认方向:从上到下*/ background: -webkit-linear-gradient(red,yellow);/*Chrome/Safari*/ background: -moz-linear-gradient(red,yellow);/*Firefox*/ background: -o-linear-gradient(red,yellow);/*Opera*/ background: linear-gradient(red,yellow);/*标准的语法(必须放在最后)*/ } #box2{ /* 从左到右*/ background: -webkit-linear-gradient(left,green,blue);/*Chrome/Safari*/ background: -moz-linear-gradient(left,green,blue);/*Firefox*/ background: -o-linear-gradient(left,green,blue);/*Opera*/ background: linear-gradient(right,green,blue);/*标准的语法(必须放在最后)*/ } #box3{ /* 从右到左*/ background: -webkit-linear-gradient(to left,green,blue);/*Chrome/Safari*/ background: -moz-linear-gradient(to left,green,blue);/*Firefox*/ background: -o-linear-gradient(to left,green,blue);/*Opera*/ background: linear-gradient(to left,green,blue);/*标准的语法(必须放在最后)*/ } /*2.对角渐变*/ #box4{ /*对角:左下->右上*/ background: -webkit-linear-gradient(left bottom,#f40,purple);/*Chrome/Safari*/ background: -moz-linear-gradient(left bottom,#f40,purple);/*FireFox*/ background: -o-linear-gradient(left bottom,#f40,purple);/*Opera*/ background: linear-gradient(left bottom,#f40,purple);/*标准的语法(必须放在最后)*/ } #box5{ /*对角:右下->左上*/ background: -webkit-linear-gradient(right bottom,#000,gold);/*Chrome/Safari*/ background: -moz-linear-gradient(right bottom,#000,gold);/*FireFox*/ background: -o-linear-gradient(right bottom,#000,gold);/*Opera*/ background: linear-gradient(right bottom,#000,gold);/*标准的语法(必须放在最后)*/ } /*3.使用角度渐变*/ #box6{ background: -webkit-linear-gradient(0deg,gray,yellow);/*Chrome/Safari*/ background: -moz-linear-gradient(0deg,gray,yellow);/*FireFox*/ background: -o-linear-gradient(0deg,gray,yellow);/*Opera*/ background: linear-gradient(0deg,gray,yellow);/*标准的语法(必须放在最后)*/ } #box7{ background: -webkit-linear-gradient(90deg,gray,yellow);/*Chrome/Safari*/ background: -moz-linear-gradient(90deg,gray,yellow);/*FireFox*/ background: -o-linear-gradient(90deg,gray,yellow);/*Opera*/ background: linear-gradient(90deg,gray,yellow);/*标准的语法(必须放在最后)*/ } #box8{ background: -webkit-linear-gradient(180deg,gray,yellow);/*Chrome/Safari*/ background: -moz-linear-gradient(180deg,gray,yellow);/*FireFox*/ background: -o-linear-gradient(180deg,gray,yellow);/*Opera*/ background: linear-gradient(180deg,gray,yellow);/*标准的语法(必须放在最后)*/ } #box9{ background: -webkit-linear-gradient(-90deg,gray,yellow);/*Chrome/Safari*/ background: -moz-linear-gradient(-90deg,gray,yellow);/*FireFox*/ background: -o-linear-gradient(-90deg,gray,yellow);/*Opera*/ background: linear-gradient(-90deg,gray,yellow);/*标准的语法(必须放在最后)*/ } /*4.多种颜色渐变*/ #box10{ background: -webkit-linear-gradient(red,green,blue);/*Chrome/Safari*/ background: -moz-linear-gradient(red,green,blue);/*FireFox*/ background: -o-linear-gradient(red,green,blue);/*Opera*/ background: linear-gradient(red,green,blue);/*标准的语法(必须放在最后)*/ } #box11{ background: -webkit-linear-gradient(red,orange,yellow,green,blue,indigo,violet);/*Chrome/Safari*/ background: -moz-linear-gradient(red,orange,yellow,green,blue,indigo,violet);/*FireFox*/ background: -o-linear-gradient(red,orange,yellow,green,blue,indigo,violet);/*Opera*/ background: linear-gradient(red,orange,yellow,green,blue,indigo,violet);/*标准的语法(必须放在最后)*/ } /*5.不同比例的多种颜色渐变*/ /*百分比设置的是渐变的起点/位置,即从什么地方开始进行渐变*/ #box12{ /*红色从20%的位置开始,一直渐变到60%的位置;黄色从60%的位置开始,一直渐变到80%的位置;蓝色从80%的位置开始,一直渐变到结束为止*/ background: -webkit-linear-gradient(red 20%,yellow 60%,blue 80%);/*Chrome/Safari*/ background: -moz-linear-gradient(red 20%,yellow 60%,blue 80%);/*FireFox*/ background: -o-linear-gradient(red 20%,yellow 60%,blue 80%);/*Opera*/ background: linear-gradient(red 20%,yellow 60%,blue 80%);/*标准的语法(必须放在最后)*/ } /*6.附带透明度的渐变*/ #box13{ /*[红+绿=黄]*/ background: -webkit-linear-gradient(left,rgba(255,0,0,0),rgba(255,255,0,1));/*Chrome/Safari*/ background: -moz-linear-gradient(left,rgba(255,0,0,0),rgba(255,255,0,1));/*FireFox*/ background: -o-linear-gradient(left,rgba(255,0,0,0),rgba(255,255,0,1));/*Opera*/ background: linear-gradient(left,rgba(255,0,0,0),rgba(255,255,0,1));/*标准的语法(必须放在最后)*/ } #box14{ /*[红+蓝=紫]*/ background: -webkit-linear-gradient(to left,rgba(255,0,0,0),rgba(255,0,255,1));/*Chrome/Safari*/ background: -moz-linear-gradient(to left,rgba(255,0,0,0),rgba(255,0,255,1));/*FireFox*/ background: -o-linear-gradient(to left,rgba(255,0,0,0),rgba(255,0,255,1));/*Opera*/ background: linear-gradient(to left,rgba(255,0,0,0),rgba(255,0,255,1));/*标准的语法(必须放在最后)*/ } #box15{ /*绿+蓝=青色*/ background: -webkit-linear-gradient(to bottom,rgba(255,0,0,0),rgba(0,255,255,1));/*Chrome/Safari*/ background: -moz-linear-gradient(to bottom,rgba(255,0,0,0),rgba(0,255,255,1));/*FireFox*/ background: -o-linear-gradient(to bottom,rgba(255,0,0,0),rgba(0,255,255,1));/*Opera*/ background: linear-gradient(to bottom,rgba(255,0,0,0),rgba(0,255,255,1));/*标准的语法(必须放在最后)*/ } /*7.重复的线性渐变(必须给颜色一定的比例分配)*/ #box16{ background: -webkit-repeating-linear-gradient(red,green 20%,yellow 30%);/*Chrome/Safari*/ background: -moz-repeating-linear-gradient(red,green 20%,yellow 30%);/*FireFox*/ background: -o-repeating-linear-gradient(red,green 20%,yellow 30%);/*Opera*/ background: -linear-gradient(red,green 20%,yellow 30%);/*标准的语法(必须放在最后)*/ } /*二、径向渐变 语法:background:radial-gradient(center,shape size,start-color, ..., last-color); */ /*1. 颜色结点均匀分布(默认情况下)*/ #box17{ width:200px; height:150px; background: -webkit-radial-gradient(red,pink,green,yellow);/*Chrome/Safari*/ background: -moz-radial-gradient(red,pink,green,yellow);/*Firefox*/ background: -o-radial-gradient(red,pink,green,yellow);/*Opera*/ background: radial-gradient(red,pink,green,yellow);/*标准的语法(必须放在最后)*/ } /*2.颜色结点不均匀分布*/ #box18{ width:200px; height:150px; background: -webkit-radial-gradient(red 10%,pink 40%,green 50%,yellow 70%);/*Chrome/Safari*/ background: -moz-radial-gradient(red 10%,pink 40%,green 50%,yellow 70%);/*Firefox*/ background: -o-radial-gradient(red 10%,pink 40%,green 50%,yellow 70%);/*Opera*/ background: radial-gradient(red 10%,pink 40%,green 50%,yellow 70%);/*标准的语法(必须放在最后)*/ } /*3.设置形状,默认为ellipse(椭圆形),还可以设置为circle(圆形)*/ #box19{ width:200px; height:150px; background: -webkit-radial-gradient(ellipse,red,yellow,green);/*Chrome/yellowafari*/ background: -moz-radial-gradient(ellipse,red,yellow,green);/*Firefox*/ background: -o-radial-gradient(ellipse,red,yellow,green);/*Opera*/ background: radial-gradient(ellipse,red,yellow,green);/*标准的语法(必须放在最后)*/ } #box20{ width:200px; height:150px; background: -webkit-radial-gradient(circle,red,yellow,green);/*Chrome/yellowafari*/ background: -moz-radial-gradient(circle,red,yellow,green);/*Firefox*/ background: -o-radial-gradient(circle,red,yellow,green);/*Opera*/ background: radial-gradient(circle,red,yellow,green);/*标准的语法(必须放在最后)*/ } /*4.重复的径向渐变*/ #box21{ width:200px; height:150px; background: -webkit-repeating-radial-gradient(circle,orange,yellow 5%,green 10%);/*Chrome/yellowafari*/ background: -moz-repeating-radial-gradient(circle,orange,yellow 5%green) 10%;/*Firefox*/ background: -o-repeating-radial-gradient(circle,orange,yellow 5%,green 10%);/*Opera*/ background: repeating-radial-gradient(circle,orange,yellow 5%,green 10%);/*标准的语法(必须放在最后)*/ } </style> </head> <body> <p><strong>注意:</strong> Internet Explorer 9 及之前的版本不支持渐变。</p> <div id="wrap"> <p>从上到下渐变(红->黄)</p> <div id="box1"></div> </div> <div id="wrap"> <p>从左到右渐变(绿->蓝)</p> <div id="box2"></div> </div> <div id="wrap"> <p>从右到左渐变(绿->蓝)</p> <div id="box3"></div> </div> <div id="wrap"> <p>从左下角到右上角渐变<br/>(橙->紫)</p> <div id="box4"></div> </div> <div id="wrap"> <p>从右下角到左上角渐变<br/>(黑->黄)</p> <div id="box5"></div> </div> <div id="wrap"> <p>角度渐变:0°(灰->黄)<br/>0°将创建一个从下到上的渐变</p> <div id="box6"></div> </div> <div id="wrap"> <p>角度渐变:90°(灰->黄)<br/>90°将创建一个从左到右的渐变</p> <div id="box7"></div> </div> <div id="wrap"> <p>角度渐变:180°(灰->黄)<br/>180°将创建一个从上到下的渐变</p> <div id="box8"></div> </div> <div id="wrap"> <p>角度渐变:-90°(灰->黄)<br/>-90°将创建一个从右到左的渐变</p> <div id="box9"></div> </div> <div id="wrap"> <p>多种颜色渐变:<br/>(红->绿->蓝)</p> <div id="box10"></div> </div> <div id="wrap"> <p>多种颜色渐变:<br/>(红->橙->黄->绿->蓝->靛->紫)</p> <div id="box11"></div> </div> <div id="wrap"> <p>不同比例的多种颜色渐变:<br/>(红20%->黄60%->蓝80%)</p> <div id="box12"></div> </div> <div id="wrap"> <p>附带透明度的颜色渐变:<br/>(红->黄)[红+绿=黄]</p> <div id="box13"></div> </div> <div id="wrap"> <p>附带透明度的颜色渐变:<br/>(红->紫)[红+蓝=紫]</p> <div id="box14"></div> </div> <div id="wrap"> <p>附带透明度的颜色渐变:<br/>(红->青)[绿+蓝=青]</p> <div id="box15"></div> </div> <div id="wrap"> <p>重复的线性渐变:<br/>(红->绿->黄)</p> <div id="box16"></div> </div> <div id="wrap"> <p>径向渐变:颜色均匀分布<br/>(红->粉->绿->黄)</p> <div id="box17"></div> </div> <div id="wrap"> <p>径向渐变:颜色不均匀分布<br/>(红10%->粉40%->绿50%->70%黄)</p> <div id="box18"></div> </div> <div id="wrap"> <p>径向渐变:设置形状为椭圆(默认为椭圆形)<br/>(红->黄->绿)</p> <div id="box19"></div> </div> <div id="wrap"> <p>径向渐变:设置形状为圆形(默认为椭圆形)<br/>(红->黄->绿)</p> <div id="box20"></div> </div> <div id="wrap"> <p>径向渐变:重复的径向渐变<br/>(橙->黄->绿)</p> <div id="box21"></div> </div> </body> </html> 

效果展示:(PC端浏览器点击图片即可放大查看)

效果展示

小讯
上一篇 2025-01-29 13:08
下一篇 2025-01-14 07:28

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/105173.html