CSS渐变

本文详细介绍了CSS中的渐变技术,包括线性渐变、径向渐变、重复渐变和渐变文字的使用方法及效果展示。从均匀渐变到不均匀渐变,再到角度渐变和透明度渐变,全面覆盖了渐变的各种应用场景。

CSS渐变

线性渐变、径向渐变、重复渐变、渐变文字

一、线性渐变(linear-gradient)

1. 均匀渐变:(默认top→bottom)

标准语法

background: linear-gradient(yellow,orange,red,green,cyan,blue,plum) ;

效果图
在这里插入图片描述

2. 不均匀渐变

标砖语法
background: linear-gradient(yellow 10%,orange 30%,red 50%,green 80%,cyan,blue,plum);
效果图
在这里插入图片描述

颜色后加百分比,类似PS中调色块,标注位置。
百分比:是指色标位置的百分比,0%表示起点,100%表示终点。(若有需要,也可以使用范围之外的值。)

3. 带方向的渐变:(默认top→bottom)

标准语法:(方向前加to,e.g. to left)

background: linear-gradient(to left,yellow,orange,red,green,cyan,blue,plum);

效果图
在这里插入图片描述

:
兼容语法设置的是开始方向:(e.g. left:表示left→right)

background: -webkit-linear-gradient(left,yellow,orange,red,green,cyan,blue,plum);

效果图
在这里插入图片描述

4. 角度渐变(起始位置:正下方,顺时针旋转)

标准语法

//角度为正数:
background: linear-gradient(45deg,yellow,orange,red,green,cyan,blue,plum);

效果图
在这里插入图片描述

//角度为负数:
background: linear-gradient(-45deg,yellow,orange,red,green,cyan,blue,plum);

效果图
在这里插入图片描述

  • 兼容语法
//角度为正数:
background: -webkit-linear-gradient(45deg,yellow,orange,red,green,cyan,blue,plum);

效果图
在这里插入图片描述

//角度为负数:
background: -webkit-linear-gradient(-45deg,yellow,orange,red,green,cyan,blue,plum);

效果图
在这里插入图片描述

  • 起始位置在正下方,顺时针旋转(标准语法)
    0deg:bottom→top;
    45deg:left-bottom→right-top;
    90deg:left→right;
    135deg:left-top→right-bottom;
    180deg:top→bottom

5. 重复线性渐变:

标准语法

background: repeating-linear-gradient(yellow 25%,orange 50%);

效果图
在这里插入图片描述

6. 带透明度渐变:

标准语法

background: linear-gradient(rgba(154, 255, 228, 0.4), rgba(255, 165, 0, 0.73));

效果图
在这里插入图片描述

二、径向渐变

:随元素的宽高值呈现正圆或椭圆效果,可设置为正圆

1. 均匀渐变

标准语法

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

效果图
在这里插入图片描述

2. 不均匀渐变

标准语法

background: radial-gradient(yellow 20%,red,blue 50%,green);

效果图
在这里插入图片描述

3. 成正圆渐变:

标准语法

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

效果图
在这里插入图片描述

4. 定义圆的中心点的位置:

标准语法

background: radial-gradient(circle at 10% 20%,yellow,red,blue,green);

效果图
在这里插入图片描述

  • 注:兼容语法:删掉“at”关键字即可。
background: -webkit-radial-gradient(10% 30%,yellow,red,blue,green);

效果图
在这里插入图片描述

5. 定义圆的大小:

css中取决于中心点距离边或者角的位置

  • 点到边
    closest-side(最近的边);farthest-side(最远的边)
  • 点到角
    closest-corner(最近的角);farthest-corner(最远的角)

标准语法

//最近的边:
background: radial-gradient(circle closest-side at 10% 30%,yellow,red,blue,green);

效果图
在这里插入图片描述

//最远的边:
background: radial-gradient(circle farthest-side at 10% 30%,yellow,red,blue,green);

效果图
在这里插入图片描述

//最近的角:
background: radial-gradient(circle closest-corner at 10% 30%,yellow,red,blue,green);

效果图
在这里插入图片描述

//最远的角:
background: radial-gradient(circle farthest-corner at 10% 30%,yellow,red,blue,green);

效果图
在这里插入图片描述

6. 重复径向渐变:

标准语法

background: repeating-radial-gradient(yellow 10%,red,blue,green 60%);

效果图
在这里插入图片描述

三、渐变文字

1.文本填充:

语法【注意兼容性】

 background: repeating-radial-gradient(yellow 10%,red,blue,green 60%);
 -webkit-text-fill-color: transparent;
  -webkit-background-clip: text;

效果图

在这里插入图片描述

2. 文本描边(注意是居中描边)

语法

background: repeating-radial-gradient(yellow 10%,red,blue,green 60%);
-webkit-text-fill-color: transparent;
-webkit-background-clip: text;
-webkit-text-stroke: 1px #333;

效果图
在这里插入图片描述

3. 文本外发光【即:阴影】

text-shadow: 0 1px 4px blue;

效果图
在这里插入图片描述

:多层叠加,可制作霓虹灯效果(一般20层)

CSS渐变是一种在两个或多个颜色之间平滑过渡的图像效果,可作为背景使用,减少对图片的依赖,提高页面性能[^2]。以下是同类型CSS渐变的使用方法和示例: ### 线性渐变(Linear Gradient) 基本语法中涉及方向参数,方向参数可以是关键词方向(如 to left、to right等)或角度方向(如 45deg 等)。 示例代码: ```css /* 简单的水平线性渐变 */ .linear-gradient-example1 { background: linear-gradient(to right, red, yellow); } /* 带透明度的线性渐变 */ .linear-gradient-example2 { background: linear-gradient(to bottom, rgba(255, 0, 0, 0.5), rgba(0, 0, 255, 0.5)); } ``` ### 径向渐变(Radial Gradient) 基本语法包含形状和大小(形状如 circle、ellipse;大小有 closest-side、farthest-corner 等)以及位置。 示例代码: ```css /* 简单的圆形径向渐变 */ .radial-gradient-example1 { background: radial-gradient(circle, red, yellow); } /* 椭圆形径向渐变 */ .radial-gradient-example2 { background: radial-gradient(ellipse, red, yellow); } /* 带有偏移位置的径向渐变 */ .radial-gradient-example3 { background: radial-gradient(at 20% 50%, red, yellow); } ``` ### 锥形渐变(Conic Gradient) 示例代码: ```css .conic-gradient-example { background: conic-gradient(from 90deg, red, yellow, blue); } ``` ### 重复渐变 包含重复线性渐变、重复径向渐变和重复锥形渐变。 示例代码: ```css /* 重复线性渐变 */ .repeating-linear-gradient-example { background: repeating-linear-gradient(45deg, red, red 10px, yellow 10px, yellow 20px); } /* 重复径向渐变 */ .repeating-radial-gradient-example { background: repeating-radial-gradient(circle, red, red 10px, yellow 10px, yellow 20px); } ``` ### 组合渐变示例 ```css .combined-gradient-example { background: linear-gradient(to right, rgba(255, 126, 95, 0.8), rgba(254, 180, 123, 0.8)); } ```
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值