这是关于渐变的第二篇文章径向渐变,第一篇文章是线性渐变,有兴趣的能够看这里css
线性渐变是沿着一条直线渐变,而径向渐变则是沿着椭圆或者圆进行渐变。其标准语法以下:html
radial-gradient(position, shape, size, color-stop);
position,指明径向渐变的椭圆或者圆心的位置,支持的值有:left
、right
、top
、bottom
,也能够指定px或者百分比,默认是图形的正中心。相应的对应关系以下:前端
top left | at 0% 0% |
---|---|
top center | at 0% 50% |
top right | at 0% 100% |
right center | at 100% 50% |
right bottom | at 100% 100% |
bottom center | at 100% 50% |
left bottom | at 100% 0% |
left center | at 0% 100% |
center center | at 50% 50% |
shape,指明径向渐变的形状,能够为circle
或者ellipse
,从字面意思可知,circle
表示圆形,ellipse
表示椭圆形。默认为ellipse
。css3
size,表示渐变的尺寸,即渐变到哪里中止,除了支持百分比以及像素,还支持如下四个关键字,详细以下:segmentfault
关键字 | 描述 |
---|---|
closest-side | 渐变中心距离容器最近的边 做为终止位置。 |
closest-corner | 渐变中心距离容器最近的角 做为终止位置。 |
farthest-side | 渐变中心距离容器最远的边 做为终止位置。 |
farthest-corner(默认) | 渐变中心距离容器最远的角 做为终止位置。 |
size和position能够用at链接,例如30px 40px at center
,表示以元素中心点为圆心,30px为水平半径,40px为垂直半径的椭圆型渐变。微信
color-stop,与线性渐变的用法相同,这里就不在赘述了。ide
<div class="easy"></div>
.easy{ width: 200px; height: 100px; background: radial-gradient(yellow, red); }
以中心(50%, 50%)为起点,到最远角(farthest-corner),从red到green、blue的均匀渐变,效果以下图:wordpress
<div class="easy2"></div>
.easy2{ width: 200px; height: 100px; background: radial-gradient(yellow 5%, red 15%, blue 60%); }
<div class="easy3"></div> <div class="easy4"></div>
.easy3{ display: inline-block; width: 200px; height: 100px; border: 1px solid deeppink; background: radial-gradient(circle, yellow 5%, red 15%, white 40%); } .easy4{ display: inline-block; width: 200px; height: 100px; border: 1px solid deeppink; background: radial-gradient(ellipse, yellow 5%, red 25%, white 40%); }
30px 50px
指明了水平方向的渐变尺寸为30px
,垂直方向上的渐变尺寸为50px
,同时渐变的中心点为center
或者left
。<div class="box box1"></div> <div class="box box2"></div> <div class="box box3"></div> <div class="box box4"></div>
.box{ display: inline-block; width: 200px; height: 100px; } .box1{ background: radial-gradient(30px 50px at center, yellow, red); } .box2{ background: radial-gradient(50px 50px at center, yellow, red); } .box3{ background: radial-gradient(50px 30px at center, yellow, red); } .box4{ background: radial-gradient(20% 50% at left, yellow, red); }
<div class="box box10"></div> <div class="box box11"></div> <div class="box box12"></div> <div class="box box13"></div>
.box{ display: inline-block; width: 200px; height: 100px; } .box10{ background: radial-gradient(closest-side circle at 50% 75%, yellow, red); } .box11{ background: radial-gradient(closest-corner circle at 50% 75%, yellow, red); } .box12{ background: radial-gradient(farthest-side circle at 50% 75%, yellow, red); } .box13{ background: radial-gradient(farthest-corner circle at 50% 75%, yellow, red); }
径向渐变的本质也是背景图,利用背景的可叠加性,能够实现酷炫的效果,好比本例中的相似眼睛的效果图。spa
<div class="easy5"></div>
.easy5{ display: inline-block; width: 200px; height: 100px; background: radial-gradient(100px 50px ellipse, transparent 40px, red 60px, transparent 61px), radial-gradient(10px circle, #000, #000 10px, transparent 11px); }
利用background-size
属性控制背景图的大小以及其自己的重复性质,咱们还能够实现其的一些效果。3d
<div class="easy6"></div>
.easy6{ display: inline-block; width: 100px; height: 200px; background: radial-gradient(5px 10px ellipse, transparent 4px, yellow 5px, red 6px, transparent 7px), radial-gradient(3px circle, red, red 3px, transparent 4px); background-size: 25px; }
<div class="easy7"></div>
.easy7{ width: 200px; height: 100px; background: #cd0000; position: relative; } .easy7:after{ content: ''; position: absolute; height: 10px; left: 0; right: 0; bottom: -10px; background: radial-gradient(20px 15px ellipse at top, #cd0000 10px, transparent 11px); background-size: 20px 15px; }
除了径向渐变radial-gradient
,css3中还支持重复径向渐变reapting-radial-gradient
。
若是想对提升本身的csss水平,推荐《CSS揭秘》,很不错额。
喜欢的话,麻烦点个赞。感谢阅读。
CSS3 radial-gradient径向渐变语法及辅助理解案例10则
碰见了,不妨关注下个人微信公众号「前端Talkking」