轩辕李的博客 轩辕李的博客
首页
  • Java
  • Spring
  • 其他语言
  • 工具
  • JavaScript
  • TypeScript
  • Node.js
  • Vue.js
  • 前端工程化
  • 浏览器与Web API
  • 分布式
  • 代码质量管理
  • 基础
  • 操作系统
  • 计算机网络
  • 编程范式
  • 安全
  • 中间件
  • 心得
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

轩辕李

勇猛精进,星辰大海
首页
  • Java
  • Spring
  • 其他语言
  • 工具
  • JavaScript
  • TypeScript
  • Node.js
  • Vue.js
  • 前端工程化
  • 浏览器与Web API
  • 分布式
  • 代码质量管理
  • 基础
  • 操作系统
  • 计算机网络
  • 编程范式
  • 安全
  • 中间件
  • 心得
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • JavaScript

  • TypeScript

  • Node.js

  • Vue.js

  • 工程化

  • 浏览器与Web API

    • HTML基础-语义化标签与文档结构
    • HTML基础-文本与排版标签
    • HTML基础-列表与表格
    • HTML表单-Input类型详解
    • HTML表单-表单元素与验证
    • HTML交互-多媒体元素
    • HTML工程化-模板与组件化
    • HTML工程化-性能优化
    • CSS基础-选择器与优先级
    • CSS基础-盒模型与布局基础
    • CSS基础-单位与颜色系统
    • CSS基础-文本与字体
    • CSS基础-背景、列表与表格样式
    • CSS布局-Flexbox完全指南
    • CSS布局-Grid网格布局
    • CSS布局-响应式设计实践
    • CSS进阶-动画与过渡
    • CSS进阶-渐变与阴影效果
      • 一、CSS渐变(Gradient)
        • 1.1 什么是CSS渐变
        • 1.2 线性渐变(Linear Gradient)
        • 基本语法
        • 简单线性渐变
        • 指定渐变方向
        • 多颜色渐变
        • 颜色位置控制
        • 重复线性渐变
        • 1.3 径向渐变(Radial Gradient)
        • 基本语法
        • 基本径向渐变
        • 设置渐变位置
        • 渐变大小关键字
        • 重复径向渐变
        • 1.4 锥形渐变(Conic Gradient)
        • 基本语法
        • 基本锥形渐变
        • 创建饼图效果
        • 重复锥形渐变
        • 1.5 渐变实战案例
        • 案例1:渐变按钮
        • 案例2:渐变卡片
        • 案例3:渐变文字
      • 二、CSS阴影(Shadow)
        • 2.1 盒阴影(box-shadow)
        • 基本语法
        • 基本阴影效果
        • 多重阴影
        • 内阴影(inset)
        • 新拟态风格(Neumorphism)
        • 2.2 文字阴影(text-shadow)
        • 基本语法
        • 基本文字阴影
        • 多重文字阴影
        • 描边文字效果
        • 2.3 阴影实战案例
        • 案例1:卡片悬停效果
        • 案例2:浮动按钮
        • 案例3:图片阴影效果
      • 三、渐变与阴影组合应用
        • 3.1 玻璃拟态(Glassmorphism)
        • 3.2 渐变阴影
        • 3.3 动态光影效果
      • 四、性能优化与最佳实践
        • 4.1 性能考虑
        • 渐变性能优化
        • 阴影性能优化
        • 4.2 浏览器兼容性
        • 渐变兼容性
        • 阴影兼容性
        • 4.3 最佳实践
        • 1. 渐变最佳实践
        • 2. 阴影最佳实践
        • 3. 组合使用
        • 4.4 调试技巧
        • 使用开发者工具
      • 五、总结
    • CSS进阶-Transform与3D变换
    • CSS进阶-滤镜与混合模式
    • 现代CSS-CSS预处理器对比
    • 现代CSS-CSS-in-JS方案
    • 现代CSS-原子化CSS与Tailwind
    • CSS工程化-架构与规范
    • CSS工程化-性能优化
    • CSS工程化-PostCSS实战指南
    • Web API基础-DOM操作完全指南
    • Web API基础-事件处理与委托
    • Web API基础-BOM与浏览器环境
    • Web API存储-客户端存储方案
    • Web API网络-HTTP请求详解
    • Web API网络-实时通信方案
    • Web API交互-用户体验增强
    • HTML&CSS历代版本新特性
  • 前端
  • 浏览器与Web API
轩辕李
2019-07-24
目录

CSS进阶-渐变与阴影效果

渐变与阴影是CSS中强大的视觉效果工具,能够为网页添加丰富的层次感和立体感。合理运用渐变和阴影,可以让界面更加精致、美观,提升用户视觉体验。

本文将深入介绍CSS的渐变(linear-gradient、radial-gradient、conic-gradient)和阴影(box-shadow、text-shadow)特性,帮助你掌握各种渐变模式和阴影效果的实现技巧。

# 一、CSS渐变(Gradient)

# 1.1 什么是CSS渐变

CSS渐变允许在两个或多个颜色之间显示平稳的过渡效果。渐变可以用于任何使用图像的地方,如背景、边框等。

CSS提供三种类型的渐变:

  • 线性渐变(Linear Gradient):沿着一条直线方向的颜色过渡
  • 径向渐变(Radial Gradient):从中心点向外辐射的颜色过渡
  • 锥形渐变(Conic Gradient):围绕中心点旋转的颜色过渡

# 1.2 线性渐变(Linear Gradient)

# 基本语法

background: linear-gradient(direction, color-stop1, color-stop2, ...);

参数说明:

  • direction:渐变方向,可以是角度(45deg)或关键字(to right)
  • color-stop:颜色节点,可以指定颜色和位置(red 20%)

# 简单线性渐变

最简单的线性渐变从上到下:

<html>
  <div class="gradient-box-a7f3b2">基础渐变</div>
</html>
<style>
.gradient-box-a7f3b2 {
  width: 300px;
  height: 150px;
  background: linear-gradient(#ff6b6b, #4ecdc4);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 18px;
  font-weight: bold;
  border-radius: 8px;
}
</style>

# 指定渐变方向

可以使用角度或关键字指定方向:

/* 使用角度 */
background: linear-gradient(45deg, red, blue);

/* 使用关键字 */
background: linear-gradient(to right, red, blue);
background: linear-gradient(to bottom right, red, blue);
<html>
  <div class="gradient-directions-a7f3b2">
    <div class="box-a7f3b2 to-right-a7f3b2">to right</div>
    <div class="box-a7f3b2 to-bottom-a7f3b2">to bottom</div>
    <div class="box-a7f3b2 deg45-a7f3b2">45deg</div>
    <div class="box-a7f3b2 to-bottom-right-a7f3b2">to bottom right</div>
  </div>
</html>
<style>
.gradient-directions-a7f3b2 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
}
.box-a7f3b2 {
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  border-radius: 8px;
}
.to-right-a7f3b2 {
  background: linear-gradient(to right, #ff6b6b, #4ecdc4);
}
.to-bottom-a7f3b2 {
  background: linear-gradient(to bottom, #ff6b6b, #4ecdc4);
}
.deg45-a7f3b2 {
  background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
}
.to-bottom-right-a7f3b2 {
  background: linear-gradient(to bottom right, #ff6b6b, #4ecdc4);
}
</style>

# 多颜色渐变

可以添加多个颜色节点:

<html>
  <div class="multi-color-a7f3b2">彩虹渐变</div>
</html>
<style>
.multi-color-a7f3b2 {
  width: 100%;
  height: 150px;
  background: linear-gradient(
    to right,
    red,
    orange,
    yellow,
    green,
    blue,
    indigo,
    violet
  );
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 20px;
  font-weight: bold;
  border-radius: 8px;
  text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}
</style>

# 颜色位置控制

可以精确控制每个颜色的位置:

background: linear-gradient(
  to right,
  red 0%,
  yellow 50%,
  blue 100%
);
<html>
  <div class="color-position-demo-a7f3b2">
    <div class="box-a7f3b2 default-a7f3b2">默认分布</div>
    <div class="box-a7f3b2 custom-a7f3b2">自定义位置</div>
  </div>
</html>
<style>
.color-position-demo-a7f3b2 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
}
.color-position-demo-a7f3b2 .box-a7f3b2 {
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  border-radius: 8px;
}
.default-a7f3b2 {
  background: linear-gradient(to right, red, yellow, blue);
}
.custom-a7f3b2 {
  background: linear-gradient(to right, red 0%, yellow 20%, blue 100%);
}
</style>

# 重复线性渐变

使用 repeating-linear-gradient 创建重复渐变图案:

<html>
  <div class="repeating-demo-a7f3b2">
    <div class="box-a7f3b2 stripes-a7f3b2">条纹效果</div>
    <div class="box-a7f3b2 diagonal-a7f3b2">斜条纹</div>
  </div>
</html>
<style>
.repeating-demo-a7f3b2 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
}
.repeating-demo-a7f3b2 .box-a7f3b2 {
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  border-radius: 8px;
}
.stripes-a7f3b2 {
  background: repeating-linear-gradient(
    to right,
    #ff6b6b 0px,
    #ff6b6b 20px,
    #4ecdc4 20px,
    #4ecdc4 40px
  );
}
.diagonal-a7f3b2 {
  background: repeating-linear-gradient(
    45deg,
    #f39c12 0px,
    #f39c12 10px,
    #e74c3c 10px,
    #e74c3c 20px
  );
}
</style>

# 1.3 径向渐变(Radial Gradient)

# 基本语法

background: radial-gradient(shape size at position, color-stop1, color-stop2, ...);

参数说明:

  • shape:渐变形状,circle(圆形)或 ellipse(椭圆形,默认)
  • size:渐变大小,如 closest-side、farthest-corner 等
  • position:渐变中心位置,如 center、top left 等
  • color-stop:颜色节点

# 基本径向渐变

<html>
  <div class="radial-demo-a7f3b2">
    <div class="box-a7f3b2 basic-a7f3b2">基础径向</div>
    <div class="box-a7f3b2 circle-a7f3b2">圆形渐变</div>
  </div>
</html>
<style>
.radial-demo-a7f3b2 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
}
.radial-demo-a7f3b2 .box-a7f3b2 {
  height: 150px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  border-radius: 8px;
}
.basic-a7f3b2 {
  background: radial-gradient(#ff6b6b, #4ecdc4);
}
.circle-a7f3b2 {
  background: radial-gradient(circle, #f39c12, #e74c3c);
}
</style>

# 设置渐变位置

<html>
  <div class="radial-position-a7f3b2">
    <div class="box-a7f3b2 center-a7f3b2">center</div>
    <div class="box-a7f3b2 top-left-a7f3b2">top left</div>
    <div class="box-a7f3b2 bottom-right-a7f3b2">bottom right</div>
    <div class="box-a7f3b2 custom-a7f3b2">30% 70%</div>
  </div>
</html>
<style>
.radial-position-a7f3b2 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
}
.radial-position-a7f3b2 .box-a7f3b2 {
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  border-radius: 8px;
}
.center-a7f3b2 {
  background: radial-gradient(circle at center, #3498db, #2c3e50);
}
.top-left-a7f3b2 {
  background: radial-gradient(circle at top left, #e74c3c, #c0392b);
}
.bottom-right-a7f3b2 {
  background: radial-gradient(circle at bottom right, #2ecc71, #27ae60);
}
.custom-a7f3b2 {
  background: radial-gradient(circle at 30% 70%, #9b59b6, #8e44ad);
}
</style>

# 渐变大小关键字

<html>
  <div class="radial-size-a7f3b2">
    <div class="box-a7f3b2 closest-side-a7f3b2">closest-side</div>
    <div class="box-a7f3b2 farthest-side-a7f3b2">farthest-side</div>
    <div class="box-a7f3b2 closest-corner-a7f3b2">closest-corner</div>
    <div class="box-a7f3b2 farthest-corner-a7f3b2">farthest-corner</div>
  </div>
</html>
<style>
.radial-size-a7f3b2 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
}
.radial-size-a7f3b2 .box-a7f3b2 {
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  border-radius: 8px;
  font-size: 14px;
}
.closest-side-a7f3b2 {
  background: radial-gradient(closest-side at 30% 30%, #e74c3c, #2c3e50);
}
.farthest-side-a7f3b2 {
  background: radial-gradient(farthest-side at 30% 30%, #3498db, #2c3e50);
}
.closest-corner-a7f3b2 {
  background: radial-gradient(closest-corner at 30% 30%, #2ecc71, #2c3e50);
}
.farthest-corner-a7f3b2 {
  background: radial-gradient(farthest-corner at 30% 30%, #f39c12, #2c3e50);
}
</style>

# 重复径向渐变

<html>
  <div class="repeating-radial-a7f3b2">同心圆效果</div>
</html>
<style>
.repeating-radial-a7f3b2 {
  width: 100%;
  height: 200px;
  background: repeating-radial-gradient(
    circle,
    #3498db 0px,
    #3498db 20px,
    #2ecc71 20px,
    #2ecc71 40px
  );
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 20px;
  font-weight: bold;
  border-radius: 8px;
}
</style>

# 1.4 锥形渐变(Conic Gradient)

锥形渐变围绕中心点旋转,颜色沿圆周方向过渡。

# 基本语法

background: conic-gradient(from angle at position, color-stop1, color-stop2, ...);

# 基本锥形渐变

<html>
  <div class="conic-demo-a7f3b2">
    <div class="box-a7f3b2 basic-a7f3b2">基础锥形</div>
    <div class="box-a7f3b2 from-90-a7f3b2">from 90deg</div>
  </div>
</html>
<style>
.conic-demo-a7f3b2 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
}
.conic-demo-a7f3b2 .box-a7f3b2 {
  height: 150px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  border-radius: 8px;
}
.basic-a7f3b2 {
  background: conic-gradient(#ff6b6b, #4ecdc4, #f39c12, #ff6b6b);
}
.from-90-a7f3b2 {
  background: conic-gradient(from 90deg, #e74c3c, #3498db, #2ecc71, #e74c3c);
}
</style>

# 创建饼图效果

<html>
  <div class="pie-chart-a7f3b2">饼图</div>
</html>
<style>
.pie-chart-a7f3b2 {
  width: 200px;
  height: 200px;
  margin: 0 auto;
  background: conic-gradient(
    #e74c3c 0deg 120deg,
    #3498db 120deg 240deg,
    #2ecc71 240deg 360deg
  );
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  font-size: 18px;
}
</style>

# 重复锥形渐变

<html>
  <div class="repeating-conic-a7f3b2">旋转条纹</div>
</html>
<style>
.repeating-conic-a7f3b2 {
  width: 200px;
  height: 200px;
  margin: 0 auto;
  background: repeating-conic-gradient(
    from 0deg,
    #ff6b6b 0deg 30deg,
    #4ecdc4 30deg 60deg
  );
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  font-size: 18px;
}
</style>

# 1.5 渐变实战案例

# 案例1:渐变按钮

<html>
  <div class="gradient-buttons-a7f3b2">
    <button class="btn-a7f3b2 btn-primary-a7f3b2">主要按钮</button>
    <button class="btn-a7f3b2 btn-success-a7f3b2">成功按钮</button>
    <button class="btn-a7f3b2 btn-danger-a7f3b2">危险按钮</button>
  </div>
</html>
<style>
.gradient-buttons-a7f3b2 {
  display: flex;
  gap: 15px;
  flex-wrap: wrap;
}
.btn-a7f3b2 {
  padding: 12px 24px;
  border: none;
  border-radius: 8px;
  color: white;
  font-weight: bold;
  font-size: 16px;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
}
.btn-a7f3b2:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
.btn-a7f3b2:active {
  transform: translateY(0);
}
.btn-primary-a7f3b2 {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.btn-success-a7f3b2 {
  background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
}
.btn-danger-a7f3b2 {
  background: linear-gradient(135deg, #eb3349 0%, #f45c43 100%);
}
</style>

# 案例2:渐变卡片

<html>
  <div class="gradient-card-a7f3b2">
    <div class="card-content-a7f3b2">
      <h3>渐变卡片</h3>
      <p>这是一个使用渐变背景的精美卡片</p>
    </div>
  </div>
</html>
<style>
.gradient-card-a7f3b2 {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
  padding: 2px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
  border-radius: 12px;
}
.card-content-a7f3b2 {
  background: white;
  padding: 30px;
  border-radius: 10px;
}
.card-content-a7f3b2 h3 {
  margin: 0 0 10px 0;
  color: #333;
}
.card-content-a7f3b2 p {
  margin: 0;
  color: #666;
}
</style>

# 案例3:渐变文字

<html>
  <h1 class="gradient-text-a7f3b2">渐变文字效果</h1>
</html>
<style>
.gradient-text-a7f3b2 {
  font-size: 48px;
  font-weight: bold;
  text-align: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  margin: 20px 0;
}
</style>

# 二、CSS阴影(Shadow)

# 2.1 盒阴影(box-shadow)

box-shadow 用于给元素添加阴影效果。

# 基本语法

box-shadow: offset-x offset-y blur-radius spread-radius color inset;

参数说明:

  • offset-x:水平偏移量(必需)
  • offset-y:垂直偏移量(必需)
  • blur-radius:模糊半径(可选,默认0)
  • spread-radius:扩散半径(可选,默认0)
  • color:阴影颜色(可选)
  • inset:内阴影(可选)

# 基本阴影效果

<html>
  <div class="shadow-demo-a7f3b2">
    <div class="box-a7f3b2 basic-shadow-a7f3b2">基础阴影</div>
    <div class="box-a7f3b2 blur-shadow-a7f3b2">模糊阴影</div>
    <div class="box-a7f3b2 spread-shadow-a7f3b2">扩散阴影</div>
    <div class="box-a7f3b2 colored-shadow-a7f3b2">彩色阴影</div>
  </div>
</html>
<style>
.shadow-demo-a7f3b2 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 30px;
  padding: 20px;
}
.shadow-demo-a7f3b2 .box-a7f3b2 {
  height: 120px;
  background: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  border-radius: 8px;
}
.basic-shadow-a7f3b2 {
  box-shadow: 5px 5px 0 0 rgba(0,0,0,0.2);
}
.blur-shadow-a7f3b2 {
  box-shadow: 0 4px 15px rgba(0,0,0,0.2);
}
.spread-shadow-a7f3b2 {
  box-shadow: 0 0 10px 5px rgba(0,0,0,0.15);
}
.colored-shadow-a7f3b2 {
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.5);
}
</style>

# 多重阴影

可以为一个元素添加多个阴影,用逗号分隔:

<html>
  <div class="multi-shadow-demo-a7f3b2">
    <div class="box-a7f3b2 layered-a7f3b2">层叠阴影</div>
    <div class="box-a7f3b2 neon-a7f3b2">霓虹效果</div>
  </div>
</html>
<style>
.multi-shadow-demo-a7f3b2 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 40px;
  padding: 30px;
}
.multi-shadow-demo-a7f3b2 .box-a7f3b2 {
  height: 120px;
  background: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  border-radius: 8px;
}
.layered-a7f3b2 {
  box-shadow: 
    0 2px 4px rgba(0,0,0,0.1),
    0 4px 8px rgba(0,0,0,0.1),
    0 8px 16px rgba(0,0,0,0.1),
    0 16px 32px rgba(0,0,0,0.1);
}
.neon-a7f3b2 {
  background: #2c3e50;
  color: #fff;
  box-shadow: 
    0 0 10px #667eea,
    0 0 20px #667eea,
    0 0 30px #667eea,
    0 0 40px #667eea;
}
</style>

# 内阴影(inset)

使用 inset 关键字创建内阴影:

<html>
  <div class="inset-shadow-demo-a7f3b2">
    <div class="box-a7f3b2 inset-basic-a7f3b2">内阴影</div>
    <div class="box-a7f3b2 inset-pressed-a7f3b2">按压效果</div>
  </div>
</html>
<style>
.inset-shadow-demo-a7f3b2 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 30px;
  padding: 20px;
}
.inset-shadow-demo-a7f3b2 .box-a7f3b2 {
  height: 120px;
  background: #ecf0f1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  border-radius: 8px;
}
.inset-basic-a7f3b2 {
  box-shadow: inset 0 4px 10px rgba(0,0,0,0.2);
}
.inset-pressed-a7f3b2 {
  box-shadow: 
    inset 2px 2px 5px rgba(0,0,0,0.2),
    inset -2px -2px 5px rgba(255,255,255,0.5);
}
</style>

# 新拟态风格(Neumorphism)

新拟态是一种流行的设计风格,结合了浅色阴影和深色阴影:

<html>
  <div class="neumorphism-demo-a7f3b2">
    <div class="neu-card-a7f3b2">
      <div class="neu-button-a7f3b2">按钮</div>
    </div>
  </div>
</html>
<style>
/* 确保容器全屏显示,突出新拟态效果 */
body {
  margin: 0;
  padding: 0;
}

.neumorphism-demo-a7f3b2 {
  background: #e0e5ec;
  padding: 60px 40px;
  border-radius: 16px;
  min-height: 200px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.neu-card-a7f3b2 {
  background: #e0e5ec;
  padding: 40px;
  border-radius: 24px;
  box-shadow:
    12px 12px 24px rgba(163, 177, 198, 0.8),  /* 深色外阴影,增强深度感 */
    -12px -12px 24px rgba(255, 255, 255, 0.7); /* 浅色外阴影,增强凸起感 */
  transition: all 0.3s ease;
}

.neu-button-a7f3b2 {
  background: #e0e5ec;
  padding: 20px 40px;
  border-radius: 12px;
  text-align: center;
  font-weight: bold;
  font-size: 16px;
  color: #666;
  box-shadow:
    8px 8px 16px rgba(163, 177, 198, 0.7),   /* 按钮外阴影 */
    -8px -8px 16px rgba(255, 255, 255, 0.6);  /* 按钮内高光 */
  cursor: pointer;
  transition: all 0.2s ease;
  border: none;  /* 移除默认边框 */
  outline: none; /* 移除默认outline */
}

.neu-button-a7f3b2:hover {
  box-shadow:
    6px 6px 12px rgba(163, 177, 198, 0.6),
    -6px -6px 12px rgba(255, 255, 255, 0.5);
  transform: translateY(-2px); /* 悬停时轻微上浮 */
}

.neu-button-a7f3b2:active {
  box-shadow:
    inset 6px 6px 12px rgba(163, 177, 198, 0.7),  /* 点击时内阴影 */
    inset -6px -6px 12px rgba(255, 255, 255, 0.6); /* 点击时内高光 */
  transform: translateY(0);
}
</style>

# 2.2 文字阴影(text-shadow)

text-shadow 用于给文字添加阴影效果。

# 基本语法

text-shadow: offset-x offset-y blur-radius color;

参数说明:

  • offset-x:水平偏移量(必需)
  • offset-y:垂直偏移量(必需)
  • blur-radius:模糊半径(可选)
  • color:阴影颜色(可选)

# 基本文字阴影

<html>
  <div class="text-shadow-demo-a7f3b2">
    <h2 class="basic-text-a7f3b2">基础文字阴影</h2>
    <h2 class="blur-text-a7f3b2">模糊文字阴影</h2>
    <h2 class="colored-text-a7f3b2">彩色文字阴影</h2>
  </div>
</html>
<style>
.text-shadow-demo-a7f3b2 {
  text-align: center;
  padding: 20px;
}
.text-shadow-demo-a7f3b2 h2 {
  margin: 20px 0;
  font-size: 32px;
  font-weight: bold;
}
.basic-text-a7f3b2 {
  color: #2c3e50;
  text-shadow: 2px 2px 0 rgba(0,0,0,0.2);
}
.blur-text-a7f3b2 {
  color: #3498db;
  text-shadow: 0 2px 10px rgba(52, 152, 219, 0.5);
}
.colored-text-a7f3b2 {
  color: #e74c3c;
  text-shadow: 3px 3px 5px rgba(231, 76, 60, 0.5);
}
</style>

# 多重文字阴影

<html>
  <div class="multi-text-shadow-a7f3b2">
    <h2 class="glow-text-a7f3b2">发光文字</h2>
    <h2 class="3d-text-a7f3b2">3D文字</h2>
    <h2 class="neon-text-a7f3b2">霓虹文字</h2>
  </div>
</html>
<style>
.multi-text-shadow-a7f3b2 {
  background: #2c3e50;
  padding: 30px;
  text-align: center;
  border-radius: 12px;
}
.multi-text-shadow-a7f3b2 h2 {
  margin: 25px 0;
  font-size: 36px;
  font-weight: bold;
}
.glow-text-a7f3b2 {
  color: #fff;
  text-shadow: 
    0 0 10px #fff,
    0 0 20px #fff,
    0 0 30px #3498db,
    0 0 40px #3498db;
}
.3d-text-a7f3b2 {
  color: #e74c3c;
  text-shadow: 
    1px 1px 0 #c0392b,
    2px 2px 0 #c0392b,
    3px 3px 0 #c0392b,
    4px 4px 0 #c0392b,
    5px 5px 10px rgba(0,0,0,0.5);
}
.neon-text-a7f3b2 {
  color: #fff;
  text-shadow: 
    0 0 5px #fff,
    0 0 10px #fff,
    0 0 15px #2ecc71,
    0 0 20px #2ecc71,
    0 0 25px #2ecc71,
    0 0 30px #2ecc71;
  animation: neon-flicker-a7f3b2 1.5s infinite alternate;
}
@keyframes neon-flicker-a7f3b2 {
  0%, 19%, 21%, 23%, 25%, 54%, 56%, 100% {
    text-shadow: 
      0 0 5px #fff,
      0 0 10px #fff,
      0 0 15px #2ecc71,
      0 0 20px #2ecc71,
      0 0 25px #2ecc71,
      0 0 30px #2ecc71;
  }
  20%, 24%, 55% {
    text-shadow: none;
  }
}
</style>

# 描边文字效果

使用多重阴影模拟文字描边:

<html>
  <div class="stroke-text-demo-a7f3b2">
    <h2 class="stroke-text-a7f3b2">描边文字</h2>
  </div>
</html>
<style>
.stroke-text-demo-a7f3b2 {
  text-align: center;
  padding: 30px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
}
.stroke-text-a7f3b2 {
  font-size: 48px;
  font-weight: bold;
  color: #fff;
  text-shadow: 
    -2px -2px 0 #2c3e50,
    2px -2px 0 #2c3e50,
    -2px 2px 0 #2c3e50,
    2px 2px 0 #2c3e50,
    0 0 20px rgba(255,255,255,0.5);
}
</style>

# 2.3 阴影实战案例

# 案例1:卡片悬停效果

<html>
  <div class="hover-cards-a7f3b2">
    <div class="card-a7f3b2">
      <h3>卡片标题</h3>
      <p>鼠标悬停查看阴影效果</p>
    </div>
    <div class="card-a7f3b2">
      <h3>卡片标题</h3>
      <p>鼠标悬停查看阴影效果</p>
    </div>
    <div class="card-a7f3b2">
      <h3>卡片标题</h3>
      <p>鼠标悬停查看阴影效果</p>
    </div>
  </div>
</html>
<style>
.hover-cards-a7f3b2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
}
.card-a7f3b2 {
  background: white;
  padding: 25px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  transition: all 0.3s ease;
  cursor: pointer;
}
.card-a7f3b2:hover {
  transform: translateY(-8px);
  box-shadow: 0 12px 24px rgba(0,0,0,0.15);
}
.card-a7f3b2 h3 {
  margin: 0 0 10px 0;
  color: #2c3e50;
  font-size: 18px;
}
.card-a7f3b2 p {
  margin: 0;
  color: #7f8c8d;
  font-size: 14px;
}
</style>

# 案例2:浮动按钮

<html>
  <div class="fab-demo-a7f3b2">
    <button class="fab-a7f3b2">+</button>
  </div>
</html>
<style>
.fab-demo-a7f3b2 {
  text-align: center;
  padding: 40px;
}
.fab-a7f3b2 {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: none;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  font-size: 32px;
  font-weight: bold;
  cursor: pointer;
  box-shadow: 
    0 4px 8px rgba(102, 126, 234, 0.3),
    0 8px 16px rgba(102, 126, 234, 0.2);
  transition: all 0.3s ease;
}
.fab-a7f3b2:hover {
  transform: scale(1.1);
  box-shadow: 
    0 6px 12px rgba(102, 126, 234, 0.4),
    0 12px 24px rgba(102, 126, 234, 0.3);
}
.fab-a7f3b2:active {
  transform: scale(0.95);
  box-shadow: 
    0 2px 4px rgba(102, 126, 234, 0.3),
    0 4px 8px rgba(102, 126, 234, 0.2);
}
</style>

# 案例3:图片阴影效果

<html>
  <div class="image-shadow-demo-a7f3b2">
    <div class="img-wrapper-a7f3b2 shadow-1-a7f3b2">
      <div class="placeholder-a7f3b2">图片1</div>
    </div>
    <div class="img-wrapper-a7f3b2 shadow-2-a7f3b2">
      <div class="placeholder-a7f3b2">图片2</div>
    </div>
    <div class="img-wrapper-a7f3b2 shadow-3-a7f3b2">
      <div class="placeholder-a7f3b2">图片3</div>
    </div>
  </div>
</html>
<style>
.image-shadow-demo-a7f3b2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 30px;
  padding: 20px;
}
.img-wrapper-a7f3b2 {
  border-radius: 12px;
  overflow: hidden;
  transition: transform 0.3s ease;
}
.img-wrapper-a7f3b2:hover {
  transform: scale(1.05);
}
.placeholder-a7f3b2 {
  width: 100%;
  height: 150px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
}
.shadow-1-a7f3b2 {
  box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}
.shadow-2-a7f3b2 {
  box-shadow: 
    0 2px 4px rgba(0,0,0,0.1),
    0 8px 16px rgba(0,0,0,0.1);
}
.shadow-3-a7f3b2 {
  box-shadow: 0 20px 40px rgba(102, 126, 234, 0.4);
}
</style>

# 三、渐变与阴影组合应用

# 3.1 玻璃拟态(Glassmorphism)

玻璃拟态是一种流行的设计趋势,结合了半透明背景、模糊效果和微妙阴影:

<html>
  <div class="glass-demo-a7f3b2">
    <div class="glass-card-a7f3b2">
      <h3>玻璃拟态卡片</h3>
      <p>这是一个现代化的玻璃效果卡片</p>
      <button class="glass-btn-a7f3b2">了解更多</button>
    </div>
  </div>
</html>
<style>
.glass-demo-a7f3b2 {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 50px;
  border-radius: 12px;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 300px;
}
.glass-card-a7f3b2 {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 30px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 
    0 8px 32px rgba(0, 0, 0, 0.1),
    inset 0 1px 0 rgba(255, 255, 255, 0.2);
  max-width: 400px;
}
.glass-card-a7f3b2 h3 {
  margin: 0 0 15px 0;
  color: white;
  font-size: 24px;
}
.glass-card-a7f3b2 p {
  margin: 0 0 20px 0;
  color: rgba(255, 255, 255, 0.9);
  line-height: 1.6;
}
.glass-btn-a7f3b2 {
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  padding: 10px 20px;
  border-radius: 8px;
  color: white;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
}
.glass-btn-a7f3b2:hover {
  background: rgba(255, 255, 255, 0.3);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
</style>

# 3.2 渐变阴影

为阴影添加渐变色:

<html>
  <div class="gradient-shadow-demo-a7f3b2">
    <div class="box-a7f3b2">渐变阴影</div>
  </div>
</html>
<style>
.gradient-shadow-demo-a7f3b2 {
  text-align: center;
  padding: 80px 40px; /* 增加内边距避免阴影被裁剪 */
  overflow: visible; /* 确保阴影不被容器裁剪 */
}
.gradient-shadow-demo-a7f3b2 .box-a7f3b2 {
  display: inline-block;
  background: white;
  padding: 40px 60px; /* 增加内边距使元素更大 */
  border-radius: 16px; /* 增大圆角 */
  font-weight: bold;
  font-size: 24px; /* 增大字体 */
  position: relative;
  z-index: 1; /* 确保主元素在伪元素上方 */
  margin: 20px; /* 增加外边距避免阴影被裁剪 */
}
.gradient-shadow-demo-a7f3b2 .box-a7f3b2::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 120%; /* 增大宽度使阴影更明显 */
  height: 120%; /* 增大高度使阴影更明显 */
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); /* 明确的渐变范围 */
  border-radius: 20px; /* 增大圆角与主元素匹配 */
  z-index: -1; /* 确保伪元素在主元素下方 */
  filter: blur(30px); /* 增加模糊度增强阴影效果 */
  opacity: 0.8; /* 提高透明度使阴影更明显 */
  transition: all 0.3s ease; /* 增加过渡效果 */
}
/* 可选:添加悬停效果增强交互 */
.gradient-shadow-demo-a7f3b2 .box-a7f3b2:hover::before {
  filter: blur(40px); /* 悬停时增加模糊度 */
  opacity: 0.9; /* 悬停时提高透明度 */
}
</style>

# 3.3 动态光影效果

<html>
  <div class="dynamic-light-demo-a7f3b2">
    <div class="light-box-a7f3b2">鼠标移入查看效果</div>
  </div>
</html>
<style>
.dynamic-light-demo-a7f3b2 {
  text-align: center;
  padding: 60px 20px;
  background: #2c3e50;
  border-radius: 12px;
}
.light-box-a7f3b2 {
  display: inline-block;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 30px 50px;
  border-radius: 12px;
  color: white;
  font-weight: bold;
  font-size: 18px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}
.light-box-a7f3b2:hover {
  transform: translateY(-5px);
  box-shadow: 
    0 0 20px rgba(102, 126, 234, 0.6),
    0 0 40px rgba(118, 75, 162, 0.4),
    0 10px 30px rgba(0, 0, 0, 0.3);
}
</style>

# 四、性能优化与最佳实践

# 4.1 性能考虑

# 渐变性能优化

  1. 避免复杂渐变:过多的颜色节点会影响渲染性能
  2. 使用硬件加速:渐变背景会触发GPU加速
  3. 考虑使用图片:对于复杂的渐变图案,使用优化过的图片可能更高效
/* 避免 */
background: linear-gradient(
  to right,
  color1 0%, color2 5%, color3 10%, /* ... 很多颜色 */
);

/* 推荐:简化颜色节点 */
background: linear-gradient(to right, color1, color2, color3);

# 阴影性能优化

  1. 减少模糊半径:大的模糊半径会增加渲染负担
  2. 避免过多阴影:多重阴影会显著影响性能
  3. 使用 will-change:对于动画阴影,提前告知浏览器
/* 性能较差 */
.element {
  box-shadow: 0 0 100px 50px rgba(0,0,0,0.5);
}

/* 性能较好 */
.element {
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

/* 动画优化 */
.animated {
  will-change: box-shadow;
  transition: box-shadow 0.3s;
}

# 4.2 浏览器兼容性

# 渐变兼容性

现代浏览器都很好地支持CSS渐变,但需要注意:

/* 添加浏览器前缀以支持旧版本 */
.element {
  background: -webkit-linear-gradient(left, red, blue);
  background: -moz-linear-gradient(left, red, blue);
  background: -o-linear-gradient(left, red, blue);
  background: linear-gradient(to right, red, blue);
}

兼容性说明:

  • linear-gradient:所有现代浏览器
  • radial-gradient:所有现代浏览器
  • conic-gradient:Chrome 69+, Safari 12.1+(需要关注兼容性)

# 阴影兼容性

box-shadow 和 text-shadow 在现代浏览器中支持良好:

/* 旧版本浏览器需要前缀 */
.element {
  -webkit-box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  -moz-box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

# 4.3 最佳实践

# 1. 渐变最佳实践

/* ✅ 使用合理的颜色数量 */
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

/* ✅ 使用透明度实现平滑过渡 */
background: linear-gradient(
  to bottom,
  rgba(0,0,0,0) 0%,
  rgba(0,0,0,0.8) 100%
);

/* ✅ 渐变叠加创造复杂效果 */
background: 
  linear-gradient(135deg, transparent 0%, rgba(255,255,255,0.1) 100%),
  linear-gradient(to right, #667eea, #764ba2);

/* ❌ 避免过多颜色节点 */
background: linear-gradient(
  to right,
  color1, color2, color3, color4, color5, color6, color7, color8
);

# 2. 阴影最佳实践

/* ✅ 使用多层阴影创建深度 */
box-shadow: 
  0 2px 4px rgba(0,0,0,0.05),
  0 4px 8px rgba(0,0,0,0.05),
  0 8px 16px rgba(0,0,0,0.05);

/* ✅ 阴影颜色与背景协调 */
.blue-element {
  background: #3498db;
  box-shadow: 0 4px 12px rgba(52, 152, 219, 0.3);
}

/* ✅ 悬停状态增加阴影深度 */
.card {
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  transition: box-shadow 0.3s;
}
.card:hover {
  box-shadow: 0 8px 24px rgba(0,0,0,0.15);
}

/* ❌ 避免过大的模糊半径 */
box-shadow: 0 0 200px 100px rgba(0,0,0,0.5);

# 3. 组合使用

/* ✅ 渐变 + 阴影创造精美效果 */
.button {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  box-shadow: 
    0 4px 15px rgba(102, 126, 234, 0.4),
    inset 0 1px 0 rgba(255, 255, 255, 0.2);
  transition: all 0.3s;
}
.button:hover {
  transform: translateY(-2px);
  box-shadow: 
    0 6px 20px rgba(102, 126, 234, 0.5),
    inset 0 1px 0 rgba(255, 255, 255, 0.2);
}

# 4.4 调试技巧

# 使用开发者工具

  1. Chrome DevTools:

    • 选中元素查看计算后的样式
    • 实时调整渐变参数
    • 查看阴影的层叠效果
  2. 渐变生成器:

    • CSS Gradient (opens new window)
    • Gradient Hunt (opens new window)
    • uiGradients (opens new window)
  3. 阴影生成器:

    • Box Shadow Generator (opens new window)
    • Neumorphism.io (opens new window)

# 五、总结

渐变与阴影是CSS中强大的视觉效果工具:

渐变要点:

  • 掌握三种渐变类型:linear-gradient、radial-gradient、conic-gradient
  • 合理使用颜色节点和位置控制
  • 注意性能影响,避免过于复杂的渐变
  • 使用渐变生成器提高开发效率

阴影要点:

  • box-shadow 用于元素阴影,text-shadow 用于文字阴影
  • 多重阴影可以创造丰富的视觉效果
  • 注意模糊半径对性能的影响
  • 合理使用内外阴影创造立体效果

组合应用:

  • 玻璃拟态、新拟态等现代设计风格
  • 渐变与阴影结合创造精美效果
  • 动态交互增强用户体验

通过本文的学习,你应该能够灵活运用渐变和阴影创造出精美的视觉效果,为你的网页设计增添更多可能性。

祝你变得更强!

编辑 (opens new window)
#CSS
上次更新: 2025/11/20
CSS进阶-动画与过渡
CSS进阶-Transform与3D变换

← CSS进阶-动画与过渡 CSS进阶-Transform与3D变换→

最近更新
01
AI编程时代的一些心得
09-11
02
Claude Code与Codex的协同工作
09-01
03
Claude Code 最佳实践(个人版)
08-01
更多文章>
Theme by Vdoing | Copyright © 2018-2025 京ICP备2021021832号-2 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式