css3渐变属性-Gradients
CSS3 渐变(gradients)可以让你在两个或多个指定的颜色之间显示平稳的过渡。
CSS3 定义了两种类型的渐变(gradients):
- 线性渐变(Linear Gradients)- 向下/向上/向左/向右/对角方向
- 径向渐变(Radial Gradients)- 由它们的中心定义
一、linear-gradient(线性渐变)📌
为了创建一个线性渐变,你必须至少定义两种颜色节点。颜色节点即你想要呈现平稳过渡的颜色。同时,你也可以设置一个起点和一个方向(或一个角度)
语法:
background-image: linear-gradient(direction, color-stop1, color-stop2, ...);
direction表示渐变方向,color-stop1、color-stop2表示渐变颜色
1.默认情况下,线性渐变是从上到下,也就是可以不用设置direction参数
div{
height: 200px;
background-image: linear-gradient(red, blue);
}
从顶部开始的线性渐变。起点是红色,慢慢过渡到蓝色:
2.设置direction参数,改变线性渐变方向
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
margin: 20px;
display: inline-block;
width: 150px;
height: 150px;
background-color: red;
text-align: center;
line-height: 150px;
}
.box1{
/* 默认从上到下,二者效果一致 */
/* background-image: linear-gradient(red, blue); */
background-image: linear-gradient(to bottom, red, blue);
}
.box2{
/* 向上渐变 */
background-image: linear-gradient(to top, red, blue);
}
.box3{
/* 向右渐变 */
background-image: linear-gradient(to right, red, blue);
}
.box4{
/* 向左渐变 */
background-image: linear-gradient(to left, red, blue);
}
.box5{
/* 对角线渐变 */
background-image: linear-gradient(to left bottom, red, blue);
}
.box6{
/* 对角线渐变 */
background-image: linear-gradient(to right top, red, blue);
}
</style>
</head>
<body>
<div class="box1">默认(从上到下)</div>
<div class="box2">to top(向上)</div>
<div class="box3">to right(向右)</div>
<div class="box4">to left(向左)</div>
<div class="box5">左下角渐变</div>
<div class="box6">右上角渐变</div>
</body>
</html>
当然,除了通过上述关键字来指定渐变方向,还可以用角度来指定渐变方向(角度)==>
.box1{
background-image: linear-gradient(0deg, red, blue);
}
.box2{
background-image: linear-gradient(45deg, red, blue);
}
.box3{
background-image: linear-gradient(-45deg, red, blue);
}
.box4{
background-image: linear-gradient(90deg, red, blue);
}
.box5{
background-image: linear-gradient(180deg, red, blue);
}
3.使用多个颜色渐变
.box1{
background-image: linear-gradient(red, green, blue);
}
.box2{
background-image: linear-gradient(red, orange, yellow, green, blue, indigo, violet)
}
.box3{
background-image: linear-gradient(red 10%, green 85%, blue 90%);
}
注意: 当指定百分比时,颜色是不均匀分布。
4.使用透明度渐变
CSS3 渐变也支持透明度(transparent),可用于创建减弱变淡的效果。
background-image: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0,1));
5.重复线性渐变
将linear-gradient前面加上repeating(repeating-linear-gradient)即可实现 重复 线性渐变
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
margin: 20px;
display: inline-block;
width: 150px;
height: 150px;
text-align: center;
line-height: 150px;
}
.box1{
background-image: repeating-linear-gradient(to right ,red, blue 10%, green 20%);
}
.box2{
background-image: repeating-linear-gradient(45deg,red,blue 7%,green 10%);
}
.box3{
background-image: repeating-linear-gradient(190deg,red,blue 10%,green 10%);
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>
二、radial-gradient(径向渐变)📍
为了创建一个径向渐变,你也必须至少定义两种颜色节点。颜色节点即你想要呈现平稳过渡的颜色。同时,你也可以指定渐变的中心、形状(圆形或椭圆形)、大小。
默认情况下,渐变的中心是 center(表示在中心点),渐变的形状是 ellipse(表示椭圆形),渐变的大小是 farthest-corner(表示到最远的角落)。
语法:
background-image: radial-gradient(shape size at position, start-color, ..., last-color);
1.默认情况下,径向渐变从内向外分布
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
margin: 20px;
display: inline-block;
width: 150px;
height: 150px;
text-align: center;
line-height: 150px;
}
.box1{
/* 均匀分布 */
background-image: radial-gradient(red, blue);
}
.box2{
/* 不均匀分布 */
background-image: radial-gradient(red 5%, blue 18%, orange 50%);
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>
2.设置形状(shape)
shape参数定义了形状。它可以是值 circle 或 ellipse。其中,circle 表示圆形,ellipse 表示椭圆形。默认值是 ellipse。
.box1{
/* 椭圆形 Ellipse(默认) */
background-image: radial-gradient(red, blue, orange);
}
.box2{
/* 圆形 Circle */
background-image: radial-gradient(circle, red 5%, blue 18%, orange 50%);
}
3.不同尺寸大小关键字的使用(size)
size 参数定义了渐变的大小。它可以是以下四个值:
- closest-side:指定半径的长度,圆心到最近的边渐变过程
- farthest-side:指定半径的长度,圆心到最远的边渐变过程
- closest-corner:指定半径的长度,圆心到最近的叫角渐变过程
- farthest-corner:指定半径的长度,圆心到最远的叫角渐变过程
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
margin: 20px;
display: inline-block;
width: 200px;
height: 150px;
text-align: center;
line-height: 150px;
}
.box1{
/* 圆心到最近的边渐变过程 50px 50px表示圆心位置 */
background-image:radial-gradient(closest-side at 50px 50px,red, black);
}
.box2{
/* 圆心到最远的边渐变过程 */
background-image:radial-gradient(farthest-side at 50px 100px,red, black);
}
.box3{
/* 圆心到最近的角渐变过程 */
background-image:radial-gradient(closest-corner at 100px 50px,red, black);
}
.box4{
/* 默认 */
background-image:radial-gradient(farthest-corner at 100px 100px,red, black);
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
</body>
</html>
4. 重复径向渐变
repeating-radial-gradient() 函数用于重复径向渐变
.box1{
background-image:repeating-radial-gradient(red 0px,red 15px,blue 15px,blue 30px);
}
.box2{
background-image:repeating-radial-gradient(red 2%,black 15%,blue 20%);
}
至此,以上就是关于css3渐变属性Gradients~~~