CSS3 渐变(gradient)可以让你在两个或多个指定的颜色之间显示平稳的过渡。
以前,你必须使用图像来实现这些效果,现在通过使用 CSS3 的渐变(gradients)即可实现。此外,渐变效果的元素在放大时看起来效果更好,因为渐变(gradient)是由浏览器生成的。
CSS3 定义了两种类型的渐变(gradients):
-
线性渐变(Linear Gradients)- 向下/向上/向左/向右/对角方向
-
径向渐变(Radial Gradients)- 由它们的中心定义
注意:IE8 及更早版本的 IE 浏览器不支持该属性。
注意:我们在使用渐变背景设置的使用,需要在 渐变的前面添加 浏览器的内核标识码 -webkit- , -o-, -moz-, -ms-
1.1.1 线性渐变
线性渐变(linear gradients)沿着一根轴线改变颜色,从起点到终点颜色进行顺序渐变。
语法:
background:linear-gradient(direction,color-stop1,color-stop2,……);
1.1.1.1 线性渐变从上到下(默认)
<style>
.box{
width: 350px;
height: 200px;
background-image: -webkit-linear-gradient(to bottom, red,blue);
background-image: -moz-linear-gradient(to bottom, red,blue);
background-image: -o-linear-gradient(to bottom, red,blue);
background-image: -ms-linear-gradient(to bottom, red,blue);
background-image: linear-gradient(to bottom, red,blue);
}
</style>
</head>
<body>
<div class="box box1"></div>
注意:
background-image: linear-gradient(to bottom, red,blue);
/* to bottom 可以省略不写,因为 线性渐变 (默认) 是 从上到下 拉渐变 */
/*简写*/
background-image: linear-gradient(red,blue);
1.1.1.2 线性渐变从下到上
.box2{
background-image: linear-gradient(to top, red,blue);
}
1.1.1.3 线性渐变从左到右
.box3{
background-image: linear-gradient(to right, red,blue);
}
1.1.1.4 线性渐变从右到左
.box4{
background-image: linear-gradient(to left, red,blue);
}
1.1.1.5 线性渐变对角
语法:
background: -webkit-linear-gradient(begin-level begin-vertical,color-stop1,color-stop2,……);
对角--- 向 左上角 拉渐变
小提示:线性渐变对角时关键字的顺序无关紧要,“to left top”与“to top left”相同。
.box5{
background-image: linear-gradient(to left top, red,blue,green);
}
对角--- 向 左下角 拉渐变
background-image: linear-gradient(to left bottom, red,blue,green);
对角--- 向 右上角 拉渐变
background-image: linear-gradient(to right top, red, blue, green);
对角--- 向 右下角 拉渐变
background-image: linear-gradient(to right bottom, red, blue, green);
1.1.1.6 线性渐变-使用角度
语法:
background:linear-gradient(angle,color-stop1,color-stop2,……);
90deg创建一个从左到右的渐变:
.box6{
background-image: linear-gradient(90deg, red,blue);
}
1.1.1.7 线性渐变-色标点区域
根据效果图,补充代码,实现元素的背景颜色在水平方向上发生渐变,其中:
(1)0%-20%处,颜色是红色全透明
(2)20%-50%处,颜色是从白色渐变到红色
(3)50%-90%处,颜色从红色渐变到绿色,其中绿色的透明度是0.7
<style>
.box{
width: 600px;
height: 400px;
margin: 20px auto 0;
border: 1px solid black;
/* 渐变,至少要有2个颜色,才能形成渐变! */
/* 0%-20%处,颜色是红色全透明----表达的意思是:渐变的颜色是红色全透明 */
/* 20%-50%, 颜色从白色渐变到红色*/
/* 50%-90%,颜色从红色渐变到绿色,其中绿色的透明度是0.7 */
background-image: linear-gradient(to right,rgba(255,0,0,0) 20%, rgba(255,0,0,1) 50%, rgba(0,255,0,0.7) 90%);
}
</style>
</head>
<body>
<div class="box"></div>
0%-20%处,颜色是红色全透明----表达的意思是:渐变的颜色是红色全透明
rgba(255,0,0,0) 20%,
20%-50%处,颜色是从白色渐变到红色----表达的意思是:渐变的颜色是红色,颜色没有透明度
rgba(255,0,0,1) 50%
50%-90%处,颜色从红色渐变到绿色,其中绿色的透明度是0.7
rgba(0,255,0,0.7) 90%
预览:
1.1.1.8 线性渐变-重复渐变
重复的线性渐变
语法:
background:repeating-linear-gradient(color1 length|percentage,color2 length|percentage,……);
<style>
.box{
width: 600px;
height: 400px;
margin: 20px auto 0;
border: 1px solid black;
background-image: repeating-linear-gradient(to right, red 0%, blue 50%);
}
</style>
</head>
<body>
<div class="box"></div>
预览:
线性渐变,重复显示。其中蓝色 在整个区域的50%处显示。
如果你把 blue 设置为 blue 30%,表示 蓝色在整个区域的30%处显示。观察 重复显示的 第一片区域。
1.1.2 径向渐变(放射性渐变)
径向渐变由它的中心定义。
为了创建一个径向渐变,你也必须至少定义两种颜色节点。颜色节点即你想要呈现平稳过渡的颜色。同时,你也可以指定渐变的中心、形状(圆形或椭圆形)、大小。默认情况下,渐变的中心是 center(表示在中心点),渐变的形状是 ellipse(表示椭圆形),渐变的大小是 farthest-corner(表示到最远的角落)。
语法:
background-image: radial-gradient(shape size at position, start-color, ..., last-color);
特别注意: shape size at position, 这几个值的设置,都是在 逗号前面的。 每个设置之间用 空格 间隔。
1.1.2.1 at position起点
at position起点:中心点的位置,可以是关键字(水平方向--left,center,right, 垂直方向--top,center,bottom),具体数值或百分比。
如果这个参数省略了,其默认值为“center”。
注意:
渐变的中心是 center(表示在中心点),中心点不同,形成的效果 各不相同。
中心点可以用关键字:
<style>
.box{
width: 600px;
height: 400px;
margin: 20px auto 20px;
border: 1px solid black;
}
.box1{
background-image: radial-gradient(circle at left top, red, blue,pink,green);
}
.box2{
background-image: radial-gradient(circle at left center, red, blue,pink,green);
}
.box3{
background-image: radial-gradient(circle at left bottom, red, blue,pink,green);
}
.box4{
background-image: radial-gradient(circle at center top, red, blue,pink,green);
}
.box5{
background-image: radial-gradient(circle at center center, red, blue,pink,green);
}
.box6{
background-image: radial-gradient(circle at center bottom, red, blue,pink,green);
}
.box7{
background-image: radial-gradient(circle at right top, red, blue,pink,green);
}
.box8{
background-image: radial-gradient(circle at right center, red, blue,pink,green);
}
.box9{
background-image: radial-gradient(circle at right bottom, red, blue,pink,green);
}
</style>
</head>
<body>
<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box3"></div>
<div class="box box4"></div>
<div class="box box5"></div>
<div class="box box6"></div>
<div class="box box7"></div>
<div class="box box8"></div>
<div class="box box9"></div>
</body>
中心点还可以用百分比:
percentage:用百分比指定径向渐变圆心的横坐标或纵坐标。可以为负值。
<style>
.box10{
background-image: radial-gradient(circle at 60% 50%, red, blue,pink,green);
}
</style>
<div class="box box10"></div>
中心点在 水平方向 60%,垂直方向50% 那个点上,预览如下图所示:
中心点还可以用数值:
length:用长度值指定径向渐变圆心的横坐标或纵坐标。可以为负值。
<style>
.box11{
background-image: radial-gradient(circle at 200px 200px, red, blue,pink,green);
}
</style>
<div class="box box11"></div>
1.1.2.2 shape形状
shape形状: ellipse、circle;
shape 参数定义了形状。它可以是值 circle 或 ellipse。其中,circle 表示圆形,ellipse 表示椭圆形。默认值是 ellipse。
<style>
.box{
width: 600px;
height: 400px;
margin: 20px auto 0;
border: 1px solid black;
background-image: radial-gradient(ellipse at center center,red,blue,pink,green);
}
</style>
</head>
<body>
<div class="box"></div>
<style>
.box{
width: 600px;
height: 400px;
margin: 20px auto 0;
border: 1px solid black;
background-image: radial-gradient(circle at center center,red,blue,pink,green);
}
</style>
</head>
<body>
<div class="box"></div>
1.1.2.3 size大小
size:主要用来确定径向渐变的结束形状大小。如果省略了,其默认值为“farthest-corner”。可以给其显式的设置一些关键词,
径向渐变的半径长度:是从中心点 到 你拉渐变 结束的那个点,这之间的长度,就叫径向渐变的半径长度。如下图所示:
不同尺寸大小关键字的使用
size 参数定义了渐变的大小。主要有:
farthest-corner (最远的角落),closest-corner (最近的角),farthest-side (最远的端),closest-side (最近端)。
-
farthest-corner (默认) : 指定径向渐变的半径长度为从圆心到离圆心最远的角。渐变的大小是 farthest-corner (最远的角落)
-
closest-side :指定径向渐变的半径长度为从圆心到离圆心最近的边。 (最近端)
-
closest-corner : 指定径向渐变的半径长度为从圆心到离圆心最近的角。(最近的角)
-
farthest-side :指定径向渐变的半径长度为从圆心到离圆心最远的边。(最远的端)
-
contain(包含) : 使元素放大渐变,直到它被完全包含,而不会被元素的边界切掉任何渐变。
-
cover(覆盖) : 将导致梯度放大,直到它覆盖元件的整个区域。
(白话理解:径向渐变的半径长度 就是 指 你从哪 到 哪 拉渐变。)
<style>
.box{
width: 1200px;
height: 400px;
margin: 20px auto 20px;
border: 1px solid black;
}
.box1{
background-image: radial-gradient(circle farthest-corner at 60% 55%, red, blue,pink,green);
}
.box2{
background-image: radial-gradient(circle closest-corner at 60% 55%, red, blue,pink,green);
}
.box3{
background-image: radial-gradient(circle farthest-side at 60% 55%, red, blue,pink,green);
}
.box4{
background-image: radial-gradient(circle closest-side at 60% 55%, red, blue,pink,green);
}
</style>
</head>
<body>
<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box3"></div>
<div class="box box4"></div>
补充:
Autoprefixer---VScode自动补全CSS3前缀插件
把下面这段内容,添加在 autoprefixer:options 在setting.json中编辑里:
"autoprefixer.options": {
"browsers":[
"ie >= 6",
"firebox >= 8",
"chrome >= 24",
"Opera >= 10",
"last 2 versions",
"> 5%"
]
},
div{
width: 500px;
height:500px;
margin:10px;
background: -webkit-linear-gradient(top,red,white,blue);
background: -moz-linear-gradient(top,red,white,blue);
}
径向渐变
div{
width: 500px;
height:500px;
margin:10px;
background: -webkit-radial-gradient(50% 50%,black,white,blue);
background: -moz-radial-gradient(50% 50%,black,white,blue);
}
重复径向渐变
div{
width:200px;
height:200px;
padding: 20px;
background:-webkit-repeating-radial-gradient(circle contain,#f00,#ff0 10%,#f00 15%)
}