CSS3 渐变(gradients)
可以让你在两个或多个指定的颜色之间显示平稳的过渡。
线性渐变(Linear Gradients)- 向下/向上/向左/向右/对角方向
径向渐变(Radial Gradients)- 由它们的中心定义
线性渐变:
如果你想要在渐变的方向上做更多的控制,你可以定义一个角度,而不用预定义方向(to bottom、to top、to right、to left、to bottom right,等等)。
语法:
background-image: linear-gradient(direction, color-stop1, color-stop2, ...);
从上到下 线性渐变
<div class="box"></div>
.box {
height: 200px;
background-image: linear-gradient(#e66465, #9198e5);
}
从左到右 线性渐变
<div class="box"></div>
.box {
height: 200px;
background-image: linear-gradient(to right, red, yellow);
}
重复渐变
<div class="box"></div>
.box {
height: 200px;
background-image: repeating-linear-gradient(red, yellow 10%, green 20%);
}
径向渐变:
渐变的中心是 center(表示在中心点),渐变的形状是 ellipse(表示椭圆形),渐变的大小是 farthest-corner(表示到最远的角落)。
语法:
background-image: radial-gradient(shape size at position, start-color, ..., last-color);
注:circle 表示圆形,ellipse 表示椭圆形。默认值是 ellipse。
如:
<div class="box2"></div>
.box2 {
width: 200px;
background-image: radial-gradient(circle, red, yellow, green);
}
可以使用百分比渐变:
<div class="box2"></div>
.box2 {
width: 200px;
background-image: radial-gradient(red 5%, yellow 15%, green 60%);
}
不同尺寸的大小 closest-side、farthest-side、closest-corner、farthest-corner
<div class="box2"></div>
.box2 {
width: 200px;
background-image: radial-gradient(closest-side at 60% 55%, red, yellow, black);
}
重复的径向渐变:
<div class="box2"></div>
.box2 {
width: 200px;
background-image: repeating-radial-gradient(red, yellow 10%, green 15%);
}