一、CSS3渐变简介
CSS3渐变是什么?渐变是两种或多种颜色之间的平滑过渡。CSS3 渐变可以让你在两个或多个指定的颜色之间显示平稳的过渡。
CSS3 定义了两种类型的渐变:
- 线性渐变(Linear Gradients)- 向下/向上/向左/向右/对角方向
- 径向渐变(Radial Gradients)- 由它们的中心定义
二、什么是色标
在创建渐变的过程中,可以指定多个中间颜色值,这个值称为色标。每个色标包含一种颜色和一个位置,浏览器从每个色标的颜色淡出到下一个,以创建平滑的渐变。
三、CSS3线性渐变
在线性渐变过程中,颜色沿着一条直线过度:从左侧到右侧,从右侧到左侧,从顶部到底部,从底部到顶部或沿任意轴。
CSS3制作渐变效果,首先指定一个渐变的方向、起始颜色、结束颜色。具有这三个参数就可以制作一个简单、普通的渐变效果。
如果制作一个复杂的渐变效果,就需要在同一个渐变方向增添多个色标。具备这些渐变参数(至少三个),各浏览器就会绘制与渐变线垂直的颜色结来填充整个容器。
为了创建一个线性渐变,你必须至少定义两种颜色结点。颜色结点即你想要呈现平稳过渡的颜色。同时,你也可以设置一个起点和一个方向(或一个角度)。
CSS3线性渐变语法及参数
线性渐变 - 从上到下(默认情况下)
语法:background: linear-gradient(direction, color-stop1, color-stop2, ...);
示例1:预定义方向
示例图:
示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS3线性渐变</title>
<style>
/*线性渐变 - 从上到下(默认情况下)*/
.to-top {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background: -webkit-linear-gradient(red, blue);
/* Safari 5.1 - 6.0 */
background: -o-linear-gradient(red, blue);
/* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(red, blue);
/* Firefox 3.6 - 15 */
background: linear-gradient(red, blue);
/* 标准的语法(必须放在最后) */
}
/*线性渐变 - 从左到右*/
.to-left {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background: -webkit-linear-gradient(right, red, blue);
/* Safari 5.1 - 6.0 */
background: -o-linear-gradient(right, red, blue);
/* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(right, red, blue);
/* Firefox 3.6 - 15 */
background: linear-gradient(to right, red, blue);
/* 标准的语法(必须放在最后) */
}
/*线性渐变 - 对角*/
.to-bottom-right {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background: -webkit-linear-gradient(left top, red, blue);
/* Safari 5.1 - 6.0 */
background: -o-linear-gradient(bottom right, red, blue);
/* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(bottom right, red, blue);
/* Firefox 3.6 - 15 */
background: linear-gradient(to bottom right, red, blue);
/* 标准的语法(必须放在最后) */
}
</style>
</head>
<body>
<!-- 线性渐变 - 从上到下(默认情况下) -->
<div class="to-top">从上到下(默认情况下)</div>
<!-- 线性渐变 - 从左到右 -->
<div class="to-left">从左到右</div>
<!-- 线性渐变 - 对角 -->
<div class="to-bottom-right">对角</div>
</body>
</html>
示例2:使用角度
如果你想要在渐变的方向上做更多的控制,你可以定义一个角度,而不用预定义方向(to bottom、to top、to right、to left、to bottom right,等等)。
角度是指水平线和渐变线之间的角度,逆时针方向计算。换句话说,0deg 将创建一个从下到上的渐变,90deg 将创建一个从左到右的渐变。
语法:background: linear-gradient(angle, color-stop1, color-stop2);
示例图:
示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS3线性渐变</title>
<style>
/*线性渐变 - 0deg*/
.deg0 {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background: -webkit-linear-gradient(0deg, red, blue);
/* Safari 5.1 - 6.0 */
background: -o-linear-gradient(0deg, red, blue);
/* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(0deg, red, blue);
/* Firefox 3.6 - 15 */
background: linear-gradient(0deg, red, blue);
/* 标准的语法(必须放在最后) */
}
/*线性渐变 - 90deg*/
.deg90 {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background: -webkit-linear-gradient(90deg, red, blue);
/* Safari 5.1 - 6.0 */
background: -o-linear-gradient(90deg, red, blue);
/* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(90deg, red, blue);
/* Firefox 3.6 - 15 */
background: linear-gradient(90deg, red, blue);
/* 标准的语法(必须放在最后) */
}
/*线性渐变 - 180deg*/
.deg180 {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background: -webkit-linear-gradient(180deg, red, blue);
/* Safari 5.1 - 6.0 */
background: -o-linear-gradient(180deg, red, blue);
/* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(180deg, red, blue);
/* Firefox 3.6 - 15 */
background: linear-gradient(180deg, red, blue);
/* 标准的语法(必须放在最后) */
}
/*线性渐变 - -90deg*/
.deg-90 {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background: -webkit-linear-gradient(-90deg, red, blue);
/* Safari 5.1 - 6.0 */
background: -o-linear-gradient(-90deg, red, blue);
/* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(-90deg, red, blue);
/* Firefox 3.6 - 15 */
background: linear-gradient(-90deg, red, blue);
/* 标准的语法(必须放在最后) */
}
</style>
</head>
<body>
<!-- 线性渐变 - 0deg -->
<div class="deg0">0 deg</div>
<!-- 线性渐变 - 90deg -->
<div class="deg90">90 deg</div>
<!-- 线性渐变 - 180deg -->
<div class="deg180">180 deg</div>
<!-- 线性渐变 - -90 -->
<div class="deg-90">-90 deg</div>
</body>
</html>
示例3:使用多个颜色结点
示例图1:
示例代码1:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS3线性渐变</title>
<style>
/*线性渐变 - 使用多颜色结点*/
.grad {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background: -webkit-linear-gradient(left, red, orange, yellow, green, blue, indigo, violet);
/* Safari 5.1 - 6.0 */
background: -o-linear-gradient(left, red, orange, yellow, green, blue, indigo, violet);
/* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(left, red, orange, yellow, green, blue, indigo, violet);
/* Firefox 3.6 - 15 */
background: linear-gradient(to right, red, orange, yellow, green, blue, indigo, violet);
/* 标准的语法(必须放在最后) */
}
</style>
</head>
<body>
<!--线性渐变 - 使用多颜色结点 -->
<div class="grad">使用多颜色结点</div>
</body>
</html>
示例图2:
示例代码2:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS3线性渐变</title>
<style>
/*线性渐变 - 3 个颜色结点(均匀分布)*/
.grad {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background: -webkit-linear-gradient(red, green, blue);
/* Safari 5.1 - 6.0 */
background: -o-linear-gradient(red, green, blue);
/* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(red, green, blue);
/* Firefox 3.6 - 15 */
background: linear-gradient(red, green, blue);
/* 标准的语法(必须放在最后) */
}
/*线性渐变 - 3 个颜色结点(不均匀分布)*/
.grad2 {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background: -webkit-linear-gradient(red 10%, green 85%, blue 90%);
/* Safari 5.1 - 6.0 */
background: -o-linear-gradient(red 10%, green 85%, blue 90%);
/* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(red 10%, green 85%, blue 90%);
/* Firefox 3.6 - 15 */
background: linear-gradient(red 10%, green 85%, blue 90%);
/* 标准的语法(必须放在最后) */
}
</style>
</head>
<body>
<!--线性渐变 - 3 个颜色结点(均匀分布) -->
<div class="grad">3 个颜色结点(均匀分布)</div>
<!--线性渐变 - 3 个颜色结点(不均匀分布) -->
<div class="grad2">3 个颜色结点(不均匀分布)</div>
</body>
</html>
示例4:使用透明度(transparent)
CSS3 渐变也支持透明度(transparent),可用于创建减弱变淡的效果。
为了添加透明度,我们使用 rgba() 函数来定义颜色结点。rgba() 函数中的最后一个参数可以是从 0 到 1 的值,它定义了颜色的透明度:0 表示完全透明,1 表示完全不透明。
示例图:
示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS3线性渐变</title>
<style>
/*线性渐变 - 透明度*/
.grad {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background: -webkit-linear-gradient(left, rgba(255, 0, 0, 0), rgba(255, 0, 0, 1));
/* Safari 5.1 - 6.0 */
background: -o-linear-gradient(right, rgba(255, 0, 0, 0), rgba(255, 0, 0, 1));
/* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(right, rgba(255, 0, 0, 0), rgba(255, 0, 0, 1));
/* Firefox 3.6 - 15 */
background: linear-gradient(to right, rgba(255, 0, 0, 0), rgba(255, 0, 0, 1));
/* 标准的语法(必须放在最后) */
}
</style>
</head>
<body>
<!--线性渐变 - 透明度 -->
<div class="grad">线性渐变 - 透明度</div>
</body>
</html>
四、CSS3径向渐变
CSS3径向渐变是圆形或椭圆形渐变。颜色不再沿着一条直线轴变化,而是从一个起点朝所有方向混合。径向渐变由它的中心定义。
为了创建一个径向渐变,你也必须至少定义两种颜色结点。颜色结点即你想要呈现平稳过渡的颜色。同时,你也可以指定渐变的中心、形状(圆形或椭圆形)、大小。默认情况下,渐变的中心是 center(表示在中心点),渐变的形状是 ellipse(表示椭圆形),渐变的大小是 farthest-corner(表示到最远的角落)。
CSS3径向渐变的语法及参数
语法:background: radial-gradient(center, shape size, start-color, ..., last-color);
渐变的中心位置取值:
- <length>:用长度值指定径向渐变圆心的横坐标或纵坐标,可以为负值
- <percentage>:用百分比指定径向渐变圆心的横坐标或纵坐标,可以为负值
- top:设置顶部为径向渐变圆心的纵坐标值
- right:设置右边为径向渐变圆心的横坐标值
- bottom:设置底部为径向渐变圆心的纵坐标值
- left:设置左边为径向渐变圆心的横坐标值
- center:设置中间为径向渐变圆心的横坐标值或纵坐标值(默认值)
定义径向渐变的形状
<shape>主要用来定义渐变的形状,主要包括两个值circle和ellipse。
- circle:指定圆形的径向渐变
- ellipse:指定椭圆形的径向渐变
指定径向渐变的形状大小
<size>用来确定径向渐变的结束形状大小。
- closest-side:指定径向渐变的半径长度为从圆心到离圆心最近的边
- closest-corder:指定径向渐变的半径长度为从圆心到离圆心最近的角
- farthest-side:指定径向渐变的半径长度为从圆心到离圆心最远的边
- farthest-corner:指定径向渐变的半径长度为从圆心到离圆心最远的角
示例图1:
示例代码1:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS3线性渐变</title>
<style>
/*径向渐变 - 颜色结点均匀分布(默认情况下)*/
.grad {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background: -webkit-radial-gradient(red, green, blue);
/* Safari 5.1 - 6.0 */
background: -o-radial-gradient(red, green, blue);
/* Opera 11.6 - 12.0 */
background: -moz-radial-gradient(red, green, blue);
/* Firefox 3.6 - 15 */
background: radial-gradient(red, green, blue);
/* 标准的语法(必须放在最后) */
}
/*径向渐变 - 颜色结点不均匀分布*/
.grad2 {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background: -webkit-radial-gradient(red 5%, green 15%, blue 60%);
/* Safari 5.1 - 6.0 */
background: -o-radial-gradient(red 5%, green 15%, blue 60%);
/* Opera 11.6 - 12.0 */
background: -moz-radial-gradient(red 5%, green 15%, blue 60%);
/* Firefox 3.6 - 15 */
background: radial-gradient(red 5%, green 15%, blue 60%);
/* 标准的语法(必须放在最后) */
}
</style>
</head>
<body>
<!--径向渐变 - 颜色结点均匀分布(默认情况下)-->
<div class="grad">颜色结点均匀分布</div>
<!--径向渐变 - 颜色结点不均匀分布-->
<div class="grad2">颜色结点不均匀分布</div>
</body>
</html>
示例图2:
示例代码2:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS3线性渐变</title>
<style>
/*径向渐变 - 椭圆形 Ellipse(默认)*/
.grad {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background: -webkit-radial-gradient(red, yellow, green);
/* Safari 5.1 - 6.0 */
background: -o-radial-gradient(red, yellow, green);
/* Opera 11.6 - 12.0 */
background: -moz-radial-gradient(red, yellow, green);
/* Firefox 3.6 - 15 */
background: radial-gradient(red, yellow, green);
/* 标准的语法(必须放在最后) */
}
/*径向渐变 - 圆形 Circle*/
.grad2 {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background: -webkit-radial-gradient(circle, red, yellow, green);
/* Safari 5.1 - 6.0 */
background: -o-radial-gradient(circle, red, yellow, green);
/* Opera 11.6 - 12.0 */
background: -moz-radial-gradient(circle, red, yellow, green);
/* Firefox 3.6 - 15 */
background: radial-gradient(circle, red, yellow, green);
/* 标准的语法(必须放在最后) */
}
</style>
</head>
<body>
<!--径向渐变 - 椭圆形 Ellipse(默认)-->
<div class="grad">椭圆形 Ellipse</div>
<!--径向渐变 - 圆形 Circle-->
<div class="grad2">圆形 Circle</div>
</body>
</html>
示例图3:
示例代码3:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS3线性渐变</title>
<style>
/*径向渐变 - closest-side*/
.grad {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background: -webkit-radial-gradient(60% 55%, closest-side, blue, green, yellow, black);
/* Safari 5.1 - 6.0 */
background: -o-radial-gradient(60% 55%, closest-side, blue, green, yellow, black);
/* Opera 11.6 - 12.0 */
background: -moz-radial-gradient(60% 55%, closest-side, blue, green, yellow, black);
/* Firefox 3.6 - 15 */
background: radial-gradient(60% 55%, closest-side, blue, green, yellow, black);
/* 标准的语法(必须放在最后) */
}
/*径向渐变 - farthest-side*/
.grad2 {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background: -webkit-radial-gradient(60% 55%, farthest-side, blue, green, yellow, black);
/* Safari 5.1 - 6.0 */
background: -o-radial-gradient(60% 55%, farthest-side, blue, green, yellow, black);
/* Opera 11.6 - 12.0 */
background: -moz-radial-gradient(60% 55%, farthest-side, blue, green, yellow, black);
/* Firefox 3.6 - 15 */
background: radial-gradient(60% 55%, farthest-side, blue, green, yellow, black);
/* 标准的语法(必须放在最后) */
}
/*径向渐变 - closest-corner*/
.grad3 {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background: -webkit-radial-gradient(60% 55%, closest-corner, blue, green, yellow, black);
/* Safari 5.1 - 6.0 */
background: -o-radial-gradient(60% 55%, closest-corner, blue, green, yellow, black);
/* Opera 11.6 - 12.0 */
background: -moz-radial-gradient(60% 55%, closest-corner, blue, green, yellow, black);
/* Firefox 3.6 - 15 */
background: radial-gradient(60% 55%, closest-corner, blue, green, yellow, black);
/* 标准的语法(必须放在最后) */
}
/*径向渐变 - farthest-corner*/
.grad4 {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background: -webkit-radial-gradient(60% 55%, farthest-corner, blue, green, yellow, black);
/* Safari 5.1 - 6.0 */
background: -o-radial-gradient(60% 55%, farthest-corner, blue, green, yellow, black);
/* Opera 11.6 - 12.0 */
background: -moz-radial-gradient(60% 55%, farthest-corner, blue, green, yellow, black);
/* Firefox 3.6 - 15 */
background: radial-gradient(60% 55%, farthest-corner, blue, green, yellow, black);
/* 标准的语法(必须放在最后) */
}
</style>
</head>
<body>
<!--径向渐变 - closest-side-->
<div class="grad">closest-side</div>
<!--径向渐变 - closest-corner-->
<div class="grad3">closest-corner</div>
<!--径向渐变 - farthest-side-->
<div class="grad2">farthest-side</div>
<!--径向渐变 - farthest-corner-->
<div class="grad4">farthest-corner</div>
</body>
</html>
五、CSS3重复渐变
CSS3通过repeating-linear-gradient和repeating-radial-gradient可以直接实现重复的渐变效果。
示例效果图:
示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS3线性渐变</title>
<style>
/*重复线性渐变*/
.grad {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background-image:-webkit-repeating-linear-gradient(red,orange 40px,orange 80px);
background-image:repeating-linear-gradient(red,orange 40px,orange 80px);
}
/*重复径向渐变*/
.grad2 {
height: 150px;
width: 300px;
margin: 10px auto;
font-size: 20px;
color: #fff;
line-height: 150px;
text-align: center;
background-image:-webkit-repeating-radial-gradient(red,green 40px,orange 80px);
background-image:repeating-radial-gradient(red,green 40px,orange 80px);
}
</style>
</head>
<body>
<!--重复线性渐变-->
<div class="grad">重复线性渐变</div>
<!--重复径向渐变-->
<div class="grad2">重复径向渐变</div>
</body>
</html>
示例效果:笔记本效果
示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS3线性渐变</title>
<style>
html,body{
margin:0;
padding:0;
height: 100%;
}
.container{
height: 100%;
background:-webkit-repeating-linear-gradient(to top,#f9f9f9,#f9f9f9 29px,#ccc 30px);
background: repeating-linear-gradient(to top,#f9f9f9,#f9f9f9 29px,#ccc 30px);
background-size:100% 30px;
position: relative;
}
.container:before{
content: "";
display: inline-block;
height: 100%;
width:4px;
border-left: double 4px #fca1a1;
position:absolute;
left:30px;
}
</style>
</head>
<body>
<div class="container"></div>
</body>
</html>
六、综合案例
使用CSS3渐变制作纹理效果。
示例图:
示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS3渐变纹理效果</title>
<style>
.patterns{
width:200px;
height: 200px;
float: left;
margin:10px;
box-shadow: 0 1px 8px #666;
}
.pt1{
background-size:50px 50px;
background-color: #0ae;
background-image: linear-gradient(rgba(255,255,255,.2) 50%,transparent 50%,transparent);
}
.pt2{
background-size:50px 50px;
background-color: #f90;
background-image: linear-gradient(rgba(255,255,255,.2) 50%,transparent 50%,transparent);
}
.pt3{
background-color:#ddeeff;
background-image: radial-gradient(closest-side,transparent 98%,rgba(0,0,0,0.3) 99%),
radial-gradient(closest-side,transparent 98%,rgba(0,0,0,0.3) 99%);
background-position: 0 0px,40px 40px;
background-size:80px 80px;
}
</style>
</head>
<body>
<div class="patterns pt1"></div>
<div class="patterns pt2"></div>
<div class="patterns pt3"></div>
</body>
</html>