CSS
CSS渐变完全指南2026 - 从线性到网格渐变的一切
linear-gradient、radial-gradient、conic-gradient的全部内容。通过实战示例学习CSS渐变的完全指南。
Toolypet Team
Development Team
CSS渐变完全指南
渐变是现代网页设计中最常用的CSS效果之一。Instagram、Stripe、Spotify...都用渐变来表达品牌特性。
在本指南中,我们将通过代码示例学习CSS渐变的所有类型和实战应用。
渐变基础理解
渐变类型
| 类型 | 函数 | 描述 |
|---|---|---|
| 线性 | linear-gradient() | 直线方向过渡 |
| 径向 | radial-gradient() | 从中心向外 |
| 锥形 | conic-gradient() | 围绕中心旋转 |
| 重复 | repeating-*-gradient() | 图案重复 |
基本语法
.element {
background: linear-gradient(direction, color1, color2, ...);
}
Linear Gradient: 线性渐变
方向设置
/* 关键字方向 */
.to-right { background: linear-gradient(to right, #6366f1, #8b5cf6); }
.to-bottom { background: linear-gradient(to bottom, #6366f1, #8b5cf6); }
.to-top-right { background: linear-gradient(to top right, #6366f1, #8b5cf6); }
/* 角度方向 */
.angle-45 { background: linear-gradient(45deg, #6366f1, #8b5cf6); }
.angle-90 { background: linear-gradient(90deg, #6366f1, #8b5cf6); }
.angle-180 { background: linear-gradient(180deg, #6366f1, #8b5cf6); }
方向关键字总结
| 关键字 | 角度 | 方向 |
|---|---|---|
to top | 0deg | ↑ |
to right | 90deg | → |
to bottom | 180deg | ↓ |
to left | 270deg | ← |
to top right | 45deg | ↗ |
色标(Color Stops)
/* 均匀分布(自动) */
.auto {
background: linear-gradient(#ff0000, #00ff00, #0000ff);
/* 0%, 50%, 100% */
}
/* 明确位置 */
.explicit {
background: linear-gradient(
#ff0000 0%,
#ff0000 30%, /* 0-30%: 保持红色 */
#00ff00 30%, /* 30%处急剧变化 */
#00ff00 70%, /* 30-70%: 保持绿色 */
#0000ff 70% /* 70%处急剧变化 */
);
}
/* 硬边界(急剧过渡) */
.hard-stop {
background: linear-gradient(
#6366f1 50%,
#8b5cf6 50%
);
}
实战示例: 流行渐变
/* Instagram风格 */
.instagram {
background: linear-gradient(
45deg,
#f09433 0%,
#e6683c 25%,
#dc2743 50%,
#cc2366 75%,
#bc1888 100%
);
}
/* Stripe风格 */
.stripe {
background: linear-gradient(
135deg,
#667eea 0%,
#764ba2 100%
);
}
/* 玻璃拟态背景 */
.glassmorphism {
background: linear-gradient(
135deg,
rgba(255, 255, 255, 0.1),
rgba(255, 255, 255, 0.05)
);
backdrop-filter: blur(10px);
}
Radial Gradient: 径向渐变
基本语法
.radial {
background: radial-gradient(shape size at position, color1, color2, ...);
}
形状(Shape)
/* 圆形 */
.circle {
background: radial-gradient(circle, #6366f1, #8b5cf6);
}
/* 椭圆形(默认) */
.ellipse {
background: radial-gradient(ellipse, #6366f1, #8b5cf6);
}
尺寸(Size)
| 关键字 | 描述 |
|---|---|
closest-side | 到最近边 |
farthest-side | 到最远边 |
closest-corner | 到最近角 |
farthest-corner | 到最远角(默认) |
.size-demo {
background: radial-gradient(circle closest-side at 30% 30%, #6366f1, #8b5cf6);
}
位置(Position)
/* 关键字位置 */
.top-left {
background: radial-gradient(circle at top left, #6366f1, #8b5cf6);
}
/* 百分比位置 */
.percent {
background: radial-gradient(circle at 30% 70%, #6366f1, #8b5cf6);
}
/* 像素位置 */
.pixel {
background: radial-gradient(circle at 100px 50px, #6366f1, #8b5cf6);
}
实战示例: 聚光灯效果
.spotlight {
background:
radial-gradient(
circle at var(--mouse-x, 50%) var(--mouse-y, 50%),
rgba(99, 102, 241, 0.3) 0%,
transparent 50%
),
#1a1a2e;
}
// 跟随鼠标的聚光灯
document.addEventListener('mousemove', (e) => {
const x = (e.clientX / window.innerWidth) * 100;
const y = (e.clientY / window.innerHeight) * 100;
document.documentElement.style.setProperty('--mouse-x', `${x}%`);
document.documentElement.style.setProperty('--mouse-y', `${y}%`);
});
Conic Gradient: 锥形渐变
基本语法
.conic {
background: conic-gradient(from angle at position, color1, color2, ...);
}
基本示例
/* 色轮 */
.color-wheel {
background: conic-gradient(
red, yellow, lime, aqua, blue, magenta, red
);
border-radius: 50%;
}
/* 饼图 */
.pie-chart {
background: conic-gradient(
#6366f1 0deg 90deg, /* 25% */
#8b5cf6 90deg 180deg, /* 25% */
#a78bfa 180deg 270deg, /* 25% */
#c4b5fd 270deg 360deg /* 25% */
);
border-radius: 50%;
}
起始角度和位置
/* 改变起始角度 */
.from-angle {
background: conic-gradient(
from 45deg,
#6366f1, #8b5cf6
);
}
/* 改变中心位置 */
.at-position {
background: conic-gradient(
at 30% 70%,
#6366f1, #8b5cf6
);
}
实战示例: 圆环图
.donut-chart {
background: conic-gradient(
#6366f1 0deg 120deg,
#8b5cf6 120deg 240deg,
#a78bfa 240deg 360deg
);
border-radius: 50%;
position: relative;
}
.donut-chart::after {
content: '';
position: absolute;
inset: 25%;
background: white;
border-radius: 50%;
}
Repeating Gradients: 重复图案
重复线性
/* 条纹图案 */
.stripes {
background: repeating-linear-gradient(
45deg,
#6366f1,
#6366f1 10px,
#8b5cf6 10px,
#8b5cf6 20px
);
}
/* 警告条带 */
.warning-tape {
background: repeating-linear-gradient(
45deg,
#fbbf24,
#fbbf24 20px,
#1f2937 20px,
#1f2937 40px
);
}
重复径向
/* 波纹图案 */
.ripple {
background: repeating-radial-gradient(
circle at center,
#6366f1 0px,
#6366f1 5px,
transparent 5px,
transparent 20px
);
}
重复锥形
/* 扇形图案 */
.fan {
background: repeating-conic-gradient(
#6366f1 0deg 10deg,
#8b5cf6 10deg 20deg
);
}
多层渐变
叠加层
.layered {
background:
linear-gradient(45deg, rgba(99, 102, 241, 0.5), transparent 50%),
linear-gradient(-45deg, rgba(139, 92, 246, 0.5), transparent 50%),
linear-gradient(to bottom, #1a1a2e, #16213e);
}
渐变 + 图片
.overlay {
background:
linear-gradient(
to bottom,
rgba(0, 0, 0, 0.1),
rgba(0, 0, 0, 0.8)
),
url('/image.jpg') center/cover;
}
实战示例: 网格渐变效果
.mesh-gradient {
background:
radial-gradient(at 40% 20%, #6366f1 0px, transparent 50%),
radial-gradient(at 80% 0%, #8b5cf6 0px, transparent 50%),
radial-gradient(at 0% 50%, #a78bfa 0px, transparent 50%),
radial-gradient(at 80% 50%, #c4b5fd 0px, transparent 50%),
radial-gradient(at 0% 100%, #e9d5ff 0px, transparent 50%),
radial-gradient(at 80% 100%, #fae8ff 0px, transparent 50%),
#1e1b4b;
}
动画渐变
位置动画
.animated-gradient {
background: linear-gradient(
270deg,
#6366f1,
#8b5cf6,
#a78bfa,
#c4b5fd
);
background-size: 400% 400%;
animation: gradient-shift 15s ease infinite;
}
@keyframes gradient-shift {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
色相旋转动画
.hue-rotate {
background: linear-gradient(45deg, #6366f1, #8b5cf6);
animation: hue-rotate 5s linear infinite;
}
@keyframes hue-rotate {
to { filter: hue-rotate(360deg); }
}
使用@property的平滑颜色过渡
@property --gradient-angle {
syntax: '<angle>';
initial-value: 0deg;
inherits: false;
}
.smooth-rotate {
--gradient-angle: 0deg;
background: conic-gradient(
from var(--gradient-angle),
#6366f1, #8b5cf6, #6366f1
);
animation: rotate-gradient 3s linear infinite;
}
@keyframes rotate-gradient {
to { --gradient-angle: 360deg; }
}
渐变文字
基本实现
.gradient-text {
background: linear-gradient(90deg, #6366f1, #8b5cf6);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
}
动画文字
.animated-text {
background: linear-gradient(
90deg,
#6366f1,
#8b5cf6,
#a78bfa,
#6366f1
);
background-size: 300% 100%;
-webkit-background-clip: text;
background-clip: text;
color: transparent;
animation: text-shine 3s linear infinite;
}
@keyframes text-shine {
to { background-position: 300% 0; }
}
渐变边框
clip-path方式
.gradient-border {
position: relative;
background: white;
padding: 2rem;
}
.gradient-border::before {
content: '';
position: absolute;
inset: 0;
padding: 3px;
background: linear-gradient(45deg, #6366f1, #8b5cf6);
-webkit-mask:
linear-gradient(#fff 0 0) content-box,
linear-gradient(#fff 0 0);
mask:
linear-gradient(#fff 0 0) content-box,
linear-gradient(#fff 0 0);
-webkit-mask-composite: xor;
mask-composite: exclude;
border-radius: inherit;
}
border-image方式
.gradient-border-simple {
border: 3px solid;
border-image: linear-gradient(45deg, #6366f1, #8b5cf6) 1;
}
性能优化
利用GPU加速
/* 用transform做动画 */
.optimized {
background: linear-gradient(45deg, #6366f1, #8b5cf6);
transform: translateZ(0); /* 创建GPU层 */
}
复杂渐变的替代方案
/* 考虑用图片代替多层渐变 */
.complex {
background: url('/gradient.webp') center/cover;
}
FAQ
Q1: 选择渐变颜色很难。有推荐的组合吗?
A: 在Gradient Generator查看热门渐变预设。另外,从相近的色系开始(紫-蓝、橙-粉)看起来更自然。
Q2: 渐变在IE中能用吗?
A: IE11支持linear-gradient和radial-gradient,但不支持conic-gradient。截至2026年,IE支持基本已终止。
Q3: 渐变会影响打包大小吗?
A: CSS渐变比图片轻得多。即使复杂的渐变也只用几行CSS表示,对打包大小影响很小。
Q4: 如何设置渐变透明度?
A: 使用rgba()或hsla()。
background: linear-gradient(rgba(99, 102, 241, 0.5), rgba(139, 92, 246, 0.5));
Q5: 渐变动画卡顿。
A: 用@property动画CSS变量比background-position动画更流畅。或者使用filter: hue-rotate()。
总结
CSS渐变要点:
- Linear: 方向 + 色标
- Radial: 形状 + 尺寸 + 位置
- Conic: 旋转角度 + 位置
- 多层: 逗号分隔,从上到下叠加
- 动画:
background-position或@property
相关工具
| 工具 | 用途 |
|---|---|
| Gradient Generator | 生成和预览渐变 |
| Box-Shadow Generator | 生成阴影效果 |
| Filter Editor | CSS滤镜效果 |
CSS渐变Gradient设计网页设计CSS3
关于作者
Toolypet Team
Development Team
The Toolypet Team creates free, privacy-focused web tools for developers and designers. All tools run entirely in your browser with no data sent to servers.
Web DevelopmentCSS ToolsDeveloper ToolsSEOSecurity