如何使用CSS3中gradient属性做出背景渐变效果

2020-07-23 14:14:20
本文章将分享有关CSS3背景渐变的效果,有一定的参考价值,希望对大家有所帮助

背景渐变是一个很好的的特效,那么如何去利用CSS3去做这个效果呢,今天将为大家分享CSS3渐变,CSS3渐变是向图像模块中添加的新类型的图像。CSS3渐变允许在两个或多个指定颜色之间显示平滑过渡。

浏览器支持两种类型的渐变:

linear, 用linear-gradient()函数定义,

radial, 用radial-gradient()函数定义.

使用过程中注意浏览器的兼容问题

Safari , Chrome :-webkit-linear-gradient

Firefox :-moz-linear-gradient

IE :-ms-linear-gradient

Opera:-o-linear-gradient

本篇文章将以Chrome 浏览器为例进行讲解

线性渐变

要创建线性渐变,将起点和方向设置为角度,还要定义颜色停止必须指定至少两个停止颜色。

从顶部到底部的线性渐变

div{  /* 倒退 */width:200px;height:200px;background-color: #1a82f7;background-repeat: repeat-x;background: -webkit-linear-gradient(top, #2F2727, #1a82f7); }

Image 1.jpg

从左到右的渐变过程

 div{  /* 倒退 */width:200px;height:200px;background-color: #1a82f7;background-repeat: repeat-x;background: -webkit-linear-gradient(left, #2F2727, #1a82f7);}

Image 2.jpg

线性渐变(偶数停顿)

div{ background: -webkit-linear-gradient(left, #2F2727, #1a82f7, #2F2727, #1a82f7, #2F2727);}

Image 3.jpg

径向渐变

径向渐变由它的中心定义,必须至少定义两种颜色结点,还可以指定渐变的中心、形状(圆形或椭圆形)、大小,默认情况下,渐变的中心是 center(表示在中心点),渐变的形状是 ellipse(表示椭圆形),渐变的大小是 farthest-corner(表示到最远的角落)。

居中径向渐变

div{width:500px;height:100px;background-color: #2F2727;background-position: center center;background-repeat: no-repeat; background: -webkit-radial-gradient(circle, #1a82f7, #2F2727);}

Image 5.jpg

自定义径向渐变

closest-side: 最近端, closest-corner:最近角

div{width:500px;height:100px;background-color: #2F2727;background-position: 80% 20%;//自己自定义位置background-repeat: no-repeat; background: -webkit-radial-gradient(80% 20%, closest-corner, #1a82f7, #2F2727);}

Image 6.jpg

总结:以上就是本篇文章的所有内容了,希望通过本篇文章对大家学习渐变有所帮助。