この記事は現在の言語では未提供です。zh-CN 版を表示しています。

CSS 渐变完全指南:线性/径向/锥形渐变与高级视觉效果实战

開発ツール(更新: 2026年6月2日)

CSS 渐变的三种类型

类型 语法 视觉效果
线性渐变 linear-gradient() 沿直线方向过渡
径向渐变 radial-gradient() 从中心向外扩散
锥形渐变 conic-gradient() 围绕中心旋转

线性渐变(linear-gradient)

基础语法

/* 方向 + 色标 */
background: linear-gradient(方向, 色标1, 色标2, ...);

/* 示例 */
background: linear-gradient(to right, #667eea, #764ba2);
background: linear-gradient(135deg, #f093fb, #f5576c);
background: linear-gradient(to bottom right, #4facfe, #00f2fe);

方向参数

/* 关键字方向 */
to top          /* 从下到上 */
to right        /* 从左到右 */
to bottom       /* 从上到下(默认) */
to left         /* 从右到左 */
to top right    /* 对角线 */

/* 角度(更精确) */
0deg    /* → to top */
90deg   /* → to right */
180deg  /* → to bottom */
45deg   /* → 右上对角 */
135deg  /* → 右下对角 */

色标控制

/* 等距分布(自动计算位置) */
background: linear-gradient(red, yellow, green);
/* red: 0%, yellow: 50%, green: 100% */

/* 指定位置 */
background: linear-gradient(red 0%, yellow 30%, green 100%);

/* 硬边界(无过渡) */
background: linear-gradient(red 50%, blue 50%);

/* 多段渐变 */
background: linear-gradient(
  red 0%, red 20%,
  yellow 20%, yellow 40%,
  green 40%, green 60%,
  blue 60%, blue 80%,
  purple 80%, purple 100%
);

径向渐变(radial-gradient)

基础语法

background: radial-gradient(形状 大小 at 位置, 色标...);

/* 示例 */
background: radial-gradient(circle, #ff6b6b, #556270);
background: radial-gradient(ellipse at top, #ffd89b, #19547b);
background: radial-gradient(circle 100px at center, #fff, #000);

形状

circle    /* 圆形 */
ellipse   /* 椭圆(默认) */

大小关键字

closest-side     /* 到最近边 */
farthest-side    /* 到最远边 */
closest-corner   /* 到最近角 */
farthest-corner  /* 到最远角(默认) */

位置

at center           /* 居中(默认) */
at top left         /* 左上角 */
at 30% 70%          /* 百分比定位 */
at 100px 200px      /* 像素定位 */

锥形渐变(conic-gradient)

基础语法

background: conic-gradient(from 起始角度 at 位置, 色标...);

/* 示例 */
background: conic-gradient(red, yellow, green, blue, red);
background: conic-gradient(from 45deg, #f00, #0f0, #00f, #f00);

实战:饼图

.pie-chart {
  background: conic-gradient(
    #3b82f6 0% 35%,
    #ef4444 35% 55%,
    #22c55e 55% 80%,
    #f59e0b 80% 100%
  );
  border-radius: 50%;
}

实战:色轮

.color-wheel {
  background: conic-gradient(
    hsl(0, 100%, 50%),
    hsl(60, 100%, 50%),
    hsl(120, 100%, 50%),
    hsl(180, 100%, 50%),
    hsl(240, 100%, 50%),
    hsl(300, 100%, 50%),
    hsl(360, 100%, 50%)
  );
  border-radius: 50%;
}

高级视觉效果

1. 渐变叠加(多层渐变)

.multi-gradient {
  background:
    linear-gradient(217deg, rgba(255,0,0,.8), rgba(255,0,0,0) 70.71%),
    linear-gradient(127deg, rgba(0,255,0,.8), rgba(0,255,0,0) 70.71%),
    linear-gradient(336deg, rgba(0,0,255,.8), rgba(0,0,255,0) 70.71%);
}

2. 网格背景

.grid-bg {
  background:
    linear-gradient(rgba(0,0,0,0.1) 1px, transparent 1px),
    linear-gradient(90deg, rgba(0,0,0,0.1) 1px, transparent 1px);
  background-size: 20px 20px;
}

3. 点阵背景

.dot-bg {
  background: radial-gradient(circle 2px, #999 100%, transparent 100%);
  background-size: 20px 20px;
}

4. 光效(Shine Effect)

.shine-button {
  position: relative;
  overflow: hidden;
  background: #3b82f6;
}

.shine-button::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(
    105deg,
    transparent 40%,
    rgba(255,255,255,0.4) 45%,
    rgba(255,255,255,0.4) 55%,
    transparent 60%
  );
  animation: shine 3s infinite;
}

@keyframes shine {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(100%); }
}

5. 玻璃拟态(Glassmorphism)

.glass-card {
  background: linear-gradient(
    135deg,
    rgba(255, 255, 255, 0.1),
    rgba(255, 255, 255, 0.05)
  );
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 16px;
}

6. 渐变文字

.gradient-text {
  background: linear-gradient(135deg, #667eea, #764ba2);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

7. 渐变边框

.gradient-border {
  position: relative;
  background: #1f2937;
  border-radius: 12px;
}

.gradient-border::before {
  content: '';
  position: absolute;
  inset: -2px;
  background: linear-gradient(135deg, #667eea, #764ba2, #f093fb);
  border-radius: 14px;
  z-index: -1;
}

8. 动态渐变

.animated-gradient {
  background: linear-gradient(
    -45deg,
    #ee7752, #e73c7e, #23a6d5, #23d5ab
  );
  background-size: 400% 400%;
  animation: gradient-shift 8s ease infinite;
}

@keyframes gradient-shift {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

重复渐变

/* 重复线性渐变 → 条纹 */
.repeating-linear {
  background: repeating-linear-gradient(
    45deg,
    #3b82f6,
    #3b82f6 10px,
    #60a5fa 10px,
    #60a5fa 20px
  );
}

/* 重复径向渐变 → 靶心 */
.repeating-radial {
  background: repeating-radial-gradient(
    circle,
    #3b82f6,
    #3b82f6 10px,
    #60a5fa 10px,
    #60a5fa 20px
  );
}

/* 重复锥形渐变 → 雷达 */
.repeating-conic {
  background: repeating-conic-gradient(
    #3b82f6 0deg 30deg,
    #60a5fa 30deg 60deg
  );
}

使用工具库渐变生成器

  1. 打开 CSS 渐变生成器
  2. 选择渐变类型(线性/径向/锥形)
  3. 添加色标,调整位置和颜色
  4. 调整方向/角度
  5. 实时预览效果
  6. 复制生成的 CSS 代码

渐变性能优化

技巧 说明
避免过多色标 3-5 个色标最佳,超过 8 个影响渲染
优先用 hex 而非 rgba #ff000080rgba(255,0,0,0.5) 更短
渐变区域尽量小 大面积渐变增加绘制成本
动画用 background-position 不要动画色标值
will-change: background 对动画渐变添加提示

浏览器兼容性

特性 Chrome Firefox Safari
linear-gradient ✅ 全部 ✅ 全部 ✅ 全部
radial-gradient ✅ 全部 ✅ 全部 ✅ 全部
conic-gradient 69+ 83+ 12.1+
repeating-conic-gradient 69+ 83+ 12.1+
多层渐变 ✅ 全部 ✅ 全部 ✅ 全部
background-clip: text 49+

总结

CSS 渐变是视觉设计的瑞士军刀——从简单的双色过渡到复杂的网格背景、光效、玻璃拟态,渐变都能胜任。掌握线性/径向/锥形三种类型 + 色标控制 + 多层叠加,就能实现绝大多数视觉效果。工具库的 渐变生成器 让你可视化调整参数,一键复制 CSS 代码。

#CSS渐变#渐变生成器#设计工具#UI设计#教程