CSS渐变属性的特效

boyanx2周前技术教程5

页面中如果有两种或多种指定颜色之间的平滑过渡的渐变效果,会使得我们的视觉效果瞬间提升几个档次,在CSS3中有提供的多个渐变方式属性就能让我们轻松实现这样的渐变效果。

目前CSS渐变属性有六个,分别为:linear-gradient(线性渐变),repeating-linear-gradient(重复线性渐变),radial-gradient(径向渐变),repeating-radial-gradient(重复径向渐变),conic-gradient(锥形渐变),repeating-conic-gradient(重复锥形渐变); CSS渐变属性作用是从一种颜色平滑渐变到另一种颜色的图像,那么background-imageborder-image属性都可以用渐变作为图片内容。 下面,我们就分别来看看这几个属性的效果

linear-gradient和repeating-linear-gradient

线性渐变以直线的方式,可向左、向右、向上、向下、对角方向延伸,使用频率很高。要创建线性渐变,需要指定两种及以上的颜值和方向,如果未指定方向,默认为上到下渐变。
使用语法:

1
background-image: linear-gradient(direction, ColorStop1, ColorStop2, ...,ColorStopN);
  1. direction的取值有: to right(向右)to bottom(向下)to bottom right(向右下角)、180deg(向下)
  2. ColorStop为指定渐变颜色和渐变位置,颜色代码可以是十六进制颜色代码,RGB颜色代码。位置可以是百分比也可以是像素
1
2
3
4
5
6
7
8
  <div class="bg">
  </div>

  .bg{
  width: 200px;
	height: 200px;
	background-image: linear-gradient( rgb(123, 255, 0),rgb(119, 0, 255));
}

默认从上到下

1
2
3
4
5
.bg{
  width: 200px;
	height: 200px;
	background-image: linear-gradient(to bottom right, rgb(123, 255, 0),rgb(119, 0, 255));
}

指定方向从左上角到右下角

1
2
3
4
5
.bg{
  width: 200px;
	height: 200px;
	background-image: linear-gradient(to bottom right, rgb(123, 255, 0),rgb(119, 0, 255),rgb(255, 0, 43));
}

指定方向从左上角到右下角,设置多种渐变颜色

1
2
3
4
5
.bg{
  width: 200px;
	height: 200px;
	background-image: linear-gradient(to bottom right, rgb(123, 255, 0) 0,rgb(251, 255, 0) 15%,rgb(119, 0, 255) 35% 80%,rgb(255, 0, 43));
}

指定方向从左上角到右下角,设置多种渐变颜色及颜色作用位置

repeating-linear-gradient用得可能比较少,它是基于linear-gradient进行重复平铺操作

1
2
3
4
5
.bg{
  width: 200px;
	height: 200px;
	background-image: repeating-linear-gradient(to bottom, rgb(123, 255, 0) 0 ,rgb(251, 255, 0)10%,rgb(255, 0, 43)15%);
}

前面都是渐变背景,我们再来看看渐变边框是什么效果

1
2
3
4
5
6
7
.bg{
  width: 200px;
	height: 200px;
  border-width:10px;
  border-style:solid;
  border-image:linear-gradient(to bottom right, rgb(123, 255, 0) 0,rgb(251, 255, 0) 15%,rgb(119, 0, 255) 35% 80%,rgb(255, 0, 43)) 1 10;
}

radial-gradient

径向渐变以由中心点由或者椭圆向外扩散,使用语法

1
background-image: radial-gradient(shape size at position, ColorStop, ..., ColorStopN);
  1. shape 圆类型,就两种:ellipse(椭圆)和circle (圆),默认ellipse
  2. size 渐变大小,分别有farthest-corner(从圆心到圆最远的角为半径),farthest-side(从圆心到圆最远的边为半径),closest-corner(从圆心到圆最近的角为半径),closest-side(从圆心到圆最近的边为半径),Size,默认是farthest-corner
  3. position 位置:left,right,top,bottom,center或者数值比分比,默认是center
  4. ColorStop,渐变颜色和渐变位置
    radial-gradient的用法和linear-gradient的用法相似
1
2
3
4
5
.bg{
  width: 200px;
	height: 200px;
  background-image: radial-gradient( rgb(123, 255, 0) 0,rgb(251, 255, 0) 15%,rgb(119, 0, 255) 35% 80%,rgb(255, 0, 43));
}
1
2
3
4
5
6
7
.bg{
  width: 200px;
	height: 200px;
  border-width:10px;
  border-style:solid;
  border-image:radial-gradient(rgb(123, 255, 0) 0,rgb(251, 255, 0) 15%,rgb(119, 0, 255) 35% 80%,rgb(255, 0, 43)) 1 10;
}

conic-gradient

一般情况下,用conic-gradient的场景比较少,但我们也可以基本了解一下。其基本语法:

1
background-image: conic-gradient(from angle at position,ColorStop, ...,ColorStopN);
  1. from angle 起点角度,默认0deg
  2. position 位置:left,right,top,bottom,center或者数值比分比,默认是center
  3. ColorStop,渐变颜色和渐变位置
1
2
3
4
5
.bg{
  width: 200px;
	height: 200px;
  background-image: conic-gradient( rgb(123, 255, 0) 0,rgb(251, 255, 0) 15%,rgb(119, 0, 255) 35% 80%,rgb(255, 0, 43));
}
1
2
3
4
5
.bg{
  width: 200px;
	height: 200px;
  background-image: conic-gradient(from 90deg at left, rgb(123, 255, 0) 0,rgb(251, 255, 0) 15%,rgb(119, 0, 255) 35% 80%,rgb(255, 0, 43));
}
1
2
3
4
5
6
7
.bg{
  width: 200px;
	height: 200px;
  border-width:10px;
  border-style:solid;
  border-image:conic-gradient(from 90deg at left, rgb(123, 255, 0) 0,rgb(251, 255, 0) 15%,rgb(119, 0, 255) 35% 80%,rgb(255, 0, 43)) 1 10;
}
标签: css文字特效

相关文章

Web开发中10个有用的免费CSS代码

在本文中主要展示了在Web开发中一些免费但是非常有用的代码,开发人员可以下载它们来简化工作流程。在这个集合中的所有代码都是经过精挑细选的,对于开发人员来说非常有用。在开发一个网站时,这些代码将节省大量...

悠然晨光!一道 CSS 面试题,解锁页面美化新技能

清晨的阳光温柔地洒进房间,空气中弥漫着淡淡的咖啡香,这样宁静的时刻,最适合静下心来充实自己。前端的朋友们,别让堆积如山的面试题压得喘不过气,每天清晨和上午,花上几分钟,跟着我拆解一道 CSS 高频面试...

特效字体设计大解析

作者:Jansoon来源:http://www.zcool.com.cn/work/ZMTQxMjgxMTY=.html----------------------------------------...

30种CSS3动画特效按钮

不论是设计还前端开发,设计和制作个特效按钮,对于他们来说,是习以为常的事情,下面是一组使用CSS3制作的炫酷鼠标滑过按钮动画特效。这30种鼠标滑过按钮的动画特效分别使用CSS3 transition和...

Qwen上新AI前端工程师!一句话搞定HTML/CSS/JS,秒变React大神

梦晨 发自 凹非寺量子位 | 公众号 QbitAIQwen上新“AI前端工程师”Web Dev,一句话开发网页应用。三大件HTML,CSS,JavaScript一个工具全包了,定睛一看用的还是Reac...

CSS3 遮罩

在网页设计中,我们经常需要实现一些特殊的视觉效果来增强用户体验。CSS3 遮罩(mask)允许我们通过控制元素的可见区域来创建各种精美的视觉效果。本文将带你全面了解 CSS3 遮罩的功能和应用。什么是...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。