当前位置: 首页 > news >正文

css3新特性第四章(渐变)

渐变

  • 线性渐变

  • 径向渐变

  • 重复渐变

  • 使用:

    background-image: xx 渐变

    background-image: linear-gradient(red,yellow,green);

  • 公共代码

     .box {width: 300px;height: 200px;border: 1px solid black;float: left;margin-left: 30px;margin-top: 30px;text-align: center;line-height: 200px;font-size: 20px;}
    <body><div class="box box1">线性渐变-默认方式,从上到下</div><div class="box box2">线性渐变-使用关键字</div><div class="box box3">线性渐变-使用角度</div><div class="box box4">线性渐变-使用像素位置 </div><div class="box box5">线性渐变演示效果</div>
    </body>
    

2.1 线性渐变

一条线一条线的变化,看起来是同一水平线;

使用:background-image: linear-gradient(颜色1,颜色2,颜色3)

默认多个颜色渐变

background-image: linear-gradient(red,yellow,green);

在这里插入图片描述

总结:多个颜色之间的渐变, 默认从上到下渐变

使用关键词设置渐变方向

/* 第二种方式 关键词设置方向 */

.box2 {

/* 从下到上 */

​ /* background-image: linear-gradient(to top,red,yellow,green); */

​ /* 到右上角 那么右上角就是 绿色 */

​ background-image: linear-gradient(to right top,red,yellow,green);

}

在这里插入图片描述

使用角度单位设置渐变方向

/* 第三种方式 使用角度 */

.box3 {

background-image: linear-gradient(30deg,red,yellow,green);

}

在这里插入图片描述

使用像素位置设置渐变方向

/* 第四种方式 使用位置 */

.box4 {

background-image: linear-gradient(red 50px,yellow 100px,green 150px);

}

在这里插入图片描述

小设计综合

.box5 {

​ background-image: linear-gradient(375deg,red 50px,yellow 100px,green 150px);

​ font-size: 40px;

​ color: transparent;

​ font-weight: bold;

​ /* */

​ -webkit-background-clip: text;

}

在这里插入图片描述

2.2 径向渐变

多个颜色从圆心散开,最终是一个圆形,从而为径向渐变,有半径

默认多个颜色渐变

  • 默认从圆心四散。(注意:不一定是正圆,要看容器本身宽高比)

/* 第一种方式 默认 从圆心向四周发散 */

.box1 {

​ background-image: radial-gradient(red,yellow,green);

}

在这里插入图片描述

关键字设置渐变位置

/* 第二种方式 关键字 从右上角发散 */

.box2 {

​ background-image: radial-gradient(at right top,red,yellow,green);

}

在这里插入图片描述

使用像素值调整渐变圆的圆心位置

/* 第三种方式 像素 从x轴开始 */

.box3 {

​ background-image: radial-gradient(red 50px,yellow 100px,green 150px);

}

在这里插入图片描述

调整渐变形状为正圆

/* 第四种方式 正圆,本来默认是根据容器的宽高计算,如果是正方形默认就是圆心了 */

.box4 {

​ background-image: radial-gradient(circle,red,yellow,green);

}

在这里插入图片描述

圆心半径

/* 圆心半径 */

.box5 {

​ background-image: radial-gradient(150px,red,yellow,green);

}

在这里插入图片描述

2.3 重复渐变

无论线性渐变,还是径向渐变,在没有发生渐变的位置,继续进行渐变,就为重复渐变。

  • 使用 repeating-linear-gradient 进行重复线性渐变,具体参数同 linear-gradient 。
  • 使用 repeating-radial-gradient 进行重复径向渐变,具体参数同 radial-gradient 。

我们可以利用渐变,做出很多有意思的效果:例如:横格纸、立体球等等。

重复径向线性渐变

  • 使用方法
    • repeating-linear-gradient:单位
  .box {width: 300px;height: 200px;border: 1px solid black;float: left;margin-left: 30px;margin-top: 30px;text-align: center;line-height: 200px;font-size: 20px;}
<body><div class="box box1">重复渐变-线性渐变</div></body>

/* 第四种方式 使用位置 */

.box1 {

​ background-image: repeating-linear-gradient(red 50px,yellow 100px,green 150px);

}

在这里插入图片描述

重复径向渐变

​ .box {
​ width: 300px;
​ height: 200px;
​ border: 1px solid black;
​ float: left;
​ margin-left: 30px;
​ margin-top: 30px;
​ text-align: center;
​ line-height: 200px;
​ font-size: 20px;
​ }

​ /* 第四种方式 使用位置 */
​ .box1 {
​ background-image: repeating-linear-gradient(red 50px,yellow 100px,green 150px);
​ }

​ .box2 {
​ background-image: repeating-radial-gradient(circle,red 50px,yellow 100px,green 150px);
​ }

重复渐变-线性渐变
重复渐变-镜像渐变

在这里插入图片描述

应用小案例

在这里插入图片描述

  • 代码

    <style>.box {width: 900px;height: 700px;border: 1px solid black;margin: 20px auto;padding: 15px;}.box1 {background-image: repeating-linear-gradient(transparent 0,transparent 34px,gray 35px);background-clip: content-box;}.ball {width: 150px;height: 150px;border-radius: 50%;position: relative;}.ball-1,.ball-2 {background-image: repeating-radial-gradient(white 0,#333 100px);}.ball-3,.ball-4 {background-image: repeating-radial-gradient(at 80px 80px ,white,#333);}.ball-1 {position: absolute;top: 10px;left: 20px;}.ball-2 {position: absolute;top: 10px;right: 20px;}.ball-3 {position: absolute;bottom: 10px;left: 20px;}.ball-4 {position: absolute;bottom: 10px;right: 20px;}</style></head>
    <body><div class="box box1"></div><div class="float-ball"><div class="ball ball-1"></div><div class="ball ball-2"></div><div class="ball ball-3"></div><div class="ball ball-4"></div></div>
    </body>
    

相关文章:

  • 【条形码识别改名工具】如何批量识别图片条形码,并以条码内容批量重命名,基于WPF和Zxing的开发总结
  • 【iOS】alloc init new底层原理
  • 嵌入式---零点漂移(Zero Drift)
  • 网络设备基础运维全攻略:华为/思科核心操作与巡检指南
  • IDEA多环节实现优雅配置
  • IDEA在Git提交时添加.ignore忽略文件,解决为什么Git中有时候使用.gitignore也无法忽略一些文件
  • 国际数据加密算法(IDEA)详解
  • 按字符串长度升序,长度相同则按字典序
  • 【Linux系统】Linux基础指令(详解Linux命令行常用指令,每一个指令都有示例演示)
  • 30天开发操作系统 第26天 -- 为窗口移动提速
  • 实现AWS Data Pipeline安全地请求企业内部API返回数据
  • 2026《数据结构》考研复习笔记四(第一章)
  • 蓝桥杯 二进制问题 刷题笔记
  • Linux操作系统简介:从开源内核到技术生态
  • BeautifulSoup 库的使用——python爬虫
  • AWS EC2完全指南:如何快速搭建高性能云服务器?
  • maven的安装与配置、IDEA集成maven
  • BEVDet: High-Performance Multi-Camera 3D Object Detection in Bird-Eye-View
  • 实操基于MCP驱动的 Agentic RAG:智能调度向量召回或者网络检索
  • 23、.NET和C#有什么区别?
  • 央媒聚焦人形机器人:为何发展突然加速?中国制造有何优势?
  • 特朗普:乌克兰问题谈判短期内若无进展美将不再斡旋
  • 杭州一地铁口建筑被吐槽像棺材,官方回应:暂无拆除计划
  • 九部门:将符合条件的家政从业人员纳入公租房等保障范围
  • “雪豹瘫痪”“漂流小孩哥大闹幼儿园”都是谣言!10起典型案例公布
  • 鲁比奥称美国已向各方提出了“持久和平的框架”