← CSS word-wrap 换行 CSS text-shadow 文本阴影 →

CSS box-shadow 属性

原创 2025-09-06 CSS 已有人查阅

CSS box-shadow 属性详解:实现边框阴影与立体效果

概述

box-shadow 是 CSS 中一个功能强大的属性,它允许开发者为元素添加阴影效果,从而创造出深度感和与背景的分离感。通过合理运用这一属性,可以实现多种视觉效果,包括常见的边框阴影(border shadows)效果。本教程将系统讲解 box-shadow 的基础知识和高级应用技巧。

基础语法与参数解析

box-shadow 属性的基本语法结构如下:

box-shadow: [水平偏移量] [垂直偏移量] [模糊半径] [扩散半径] [颜色];

参数说明

  • 水平偏移量(必需):指定阴影在水平方向的偏移距离。正值使阴影向右偏移,负值使阴影向左偏移

  • 垂直偏移量(必需):指定阴影在垂直方向的偏移距离。正值使阴影向下偏移,负值使阴影向上偏移

  • 模糊半径(可选):控制阴影的模糊程度。值越大,阴影越模糊;值为0时,阴影边缘锐利

  • 扩散半径(可选):调整阴影的尺寸大小。正值扩大阴影范围,负值缩小阴影范围

  • 颜色(可选):定义阴影的颜色值。支持多种颜色表示方式,包括颜色名称、RGB、HEX 或 HSL 值

文本边框效果实现

在设计领域中,为文本添加边框效果类似于为画作添加轮廓线,能够使文字内容更加突出和醒目。这种效果通过创建围绕文本的视觉框架,增强文本的可读性和视觉吸引力。

示例:文本边框效果

.text-element {
  color: #FF7F50;
  text-shadow: 
    -1px 0 #000000, 
    0 1px #000000, 
    1px 0 #000000, 
    0 -1px #000000;
}

边框阴影实现方法

通过为具有实色背景的元素应用 box-shadow 属性,可以创建出边框阴影效果。这种技术使阴影环绕在元素边界周围,形成类似边框的视觉效果。

基础边框阴影示例

.shadow-element {
  box-shadow: 
    rgba(85, 91, 255) 0px 0px 0px 3px, 
    rgba(31, 193, 27) 0px 0px 0px 6px, 
    rgba(255, 217, 19) 0px 0px 0px 9px, 
    rgba(255, 156, 85) 0px 0px 0px 12px, 
    rgba(255, 85, 85) 0px 0px 0px 15px;
}

高级阴影技术应用

1. 多重阴影效果

单个元素可以应用多个阴影效果,通过逗号分隔不同的阴影参数,创造出复杂的视觉层次。

.multi-shadow {
  box-shadow: 
    0 0 10px rgba(0, 0, 0, 0.2),
    0 0 20px rgba(0, 0, 0, 0.1),
    0 0 30px rgba(0, 0, 0, 0.05);
}

2. 内阴影效果

使用 inset 关键字可以创建内阴影效果,使元素呈现嵌入背景的视觉感受。

.inset-shadow {
  box-shadow: inset 2px 2px 5px rgba(0, 0, 0, 0.3);
}

3. 扩散半径创建边框式阴影

通过设置非零的扩散半径值,可以创建出更加明显的边框式阴影效果。

.border-like-shadow {
  box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.3);
}

4. 过渡与动画效果

box-shadow 属性可以与 CSS 过渡和动画功能结合使用,创建动态的阴影效果。

.animated-element {
  width: 100px;
  height: 100px;
  background-color: #FF7F50;
  color: #FFFFFF;
  animation: shadow-animation 5s infinite;
}

@keyframes shadow-animation {
  50% {
    box-shadow: 10px 20px 30px #0000FF;
  }
}

代码号学习编程实践示例

示例1:按钮悬停效果

.codehao-button {
  padding: 12px 24px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  transition: box-shadow 0.3s ease;
}

.codehao-button:hover {
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3), 
              0 6px 12px rgba(0, 0, 0, 0.2);
}

示例2:卡片设计效果

.codehao-card {
  width: 300px;
  padding: 20px;
  background-color: #FFFFFF;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1), 
              0 1px 3px rgba(0, 0, 0, 0.08);
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
}

.codehao-card:hover {
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.12), 
              0 10px 10px rgba(0, 0, 0, 0.08);
}

CSS box-shadow 属性完整演示示例

下面是一个完整的box-shadow属性演示页面,展示了多种阴影效果及其应用场景。

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS box-shadow 属性完整演示 - 代码号编程学习</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            color: #333;
            line-height: 1.6;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        header {
            text-align: center;
            padding: 30px 0;
            margin-bottom: 40px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        
        .logo {
            width: 120px;
            height: 120px;
            margin: 0 auto 20px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: white;
            border-radius: 50%;
            box-shadow: 0 0 0 5px #4a6cf7, 0 0 0 10px #6c87f9, 0 0 20px rgba(0, 0, 0, 0.15);
        }
        
        .logo img {
            max-width: 80%;
        }
        
        h1 {
            color: #2d3748;
            margin-bottom: 15px;
            font-size: 2.5rem;
        }
        
        h2 {
            color: #4a6cf7;
            margin: 30px 0 15px;
            padding-bottom: 10px;
            border-bottom: 2px solid #e2e8f0;
        }
        
        p {
            margin-bottom: 20px;
            font-size: 1.1rem;
        }
        
        .grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 25px;
            margin: 40px 0;
        }
        
        .card {
            background: white;
            border-radius: 12px;
            padding: 25px;
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            display: flex;
            flex-direction: column;
            height: 100%;
        }
        
        .card:hover {
            transform: translateY(-5px);
        }
        
        .card-title {
            font-size: 1.4rem;
            color: #4a6cf7;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
        }
        
        .card-title i {
            margin-right: 10px;
        }
        
        .example {
            height: 200px;
            margin: 15px 0;
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
            font-size: 1.2rem;
            text-align: center;
            padding: 15px;
            background-color: #4a6cf7;
        }
        
        .code {
            background: #2d3748;
            color: #e2e8f0;
            padding: 15px;
            border-radius: 8px;
            font-family: 'Courier New', monospace;
            overflow-x: auto;
            margin-top: auto;
            font-size: 0.9rem;
        }
        
        /* 示例样式 */
        .example-1 {
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }
        
        .example-2 {
            box-shadow: 0 0 0 3px #4a6cf7, 0 0 0 6px #6c87f9, 0 0 0 9px #8fa3fb;
        }
        
        .example-3 {
            box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.3);
        }
        
        .example-4 {
            box-shadow: 5px 5px 0 #4a6cf7, 10px 10px 0 #6c87f9, 15px 15px 0 #8fa3fb;
        }
        
        .example-5 {
            box-shadow: 0 10px 30px rgba(74, 108, 247, 0.5);
        }
        
        .example-6 {
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15), 0 10px 10px rgba(0, 0, 0, 0.1);
        }
        
        .example-image {
            background-image: url('https://www.ebingou.cn/biancheng/images/2.jpg');
            background-size: cover;
            background-position: center;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
        }
        
        .example-image-2 {
            background-image: url('https://www.ebingou.cn/biancheng/images/5.jpg');
            background-size: cover;
            background-position: center;
            box-shadow: inset 0 0 50px rgba(0, 0, 0, 0.7);
        }
        
        .button {
            display: inline-block;
            background: #4a6cf7;
            color: white;
            padding: 12px 25px;
            border-radius: 6px;
            text-decoration: none;
            font-weight: bold;
            margin: 10px 0;
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(74, 108, 247, 0.3);
        }
        
        .button:hover {
            background: #3b5be0;
            box-shadow: 0 6px 12px rgba(74, 108, 247, 0.4);
            transform: translateY(-2px);
        }
        
        footer {
            text-align: center;
            margin-top: 50px;
            padding: 20px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
        }
        
        @media (max-width: 768px) {
            .grid {
                grid-template-columns: 1fr;
            }
            
            h1 {
                font-size: 2rem;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <div class="logo">
                <img src="https://www.ebingou.cn/biancheng/images/logo.png" alt="代码号编程学习logo">
            </div>
            <h1>CSS box-shadow 属性完整演示</h1>
            <p>通过实际示例学习如何使用box-shadow属性创建各种阴影效果</p>
        </header>
        
        <main>
            <h2>box-shadow 基础示例</h2>
            <p>box-shadow属性可以为元素添加一个或多个阴影效果,增强页面的视觉层次感。</p>
            
            <div class="grid">
                <div class="card">
                    <h3 class="card-title"><i class="fas fa-shadow"></i>基本阴影效果</h3>
                    <div class="example example-1">阴影效果:0 5px 15px rgba(0, 0, 0, 0.2)</div>
                    <div class="code">box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);</div>
                </div>
                
                <div class="card">
                    <h3 class="card-title"><i class="fas fa-border-all"></i>多层边框阴影</h3>
                    <div class="example example-2">多层边框阴影效果</div>
                    <div class="code">box-shadow: 0 0 0 3px #4a6cf7, 0 0 0 6px #6c87f9, 0 0 0 9px #8fa3fb;</div>
                </div>
                
                <div class="card">
                    <h3 class="card-title"><i class="fas fa-square-full"></i>内阴影效果</h3>
                    <div class="example example-3">内阴影效果:inset 0 0 20px rgba(0, 0, 0, 0.3)</div>
                    <div class="code">box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.3);</div>
                </div>
                
                <div class="card">
                    <h3 class="card-title"><i class="fas fa-layer-group"></i>分层阴影效果</h3>
                    <div class="example example-4">分层阴影效果</div>
                    <div class="code">box-shadow: 5px 5px 0 #4a6cf7, 10px 10px 0 #6c87f9, 15px 15px 0 #8fa3fb;</div>
                </div>
                
                <div class="card">
                    <h3 class="card-title"><i class="fas fa-paint-brush"></i>彩色阴影效果</h3>
                    <div class="example example-5">彩色阴影:0 10px 30px rgba(74, 108, 247, 0.5)</div>
                    <div class="code">box-shadow: 0 10px 30px rgba(74, 108, 247, 0.5);</div>
                </div>
                
                <div class="card">
                    <h3 class="card-title"><i class="fas fa-expand-alt"></i>扩散阴影效果</h3>
                    <div class="example example-6">扩散阴影:多重阴影组合</div>
                    <div class="code">box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15), 0 10px 10px rgba(0, 0, 0, 0.1);</div>
                </div>
            </div>
            
            <h2>实际应用示例</h2>
            <p>下面展示box-shadow在实际UI设计中的应用场景。</p>
            
            <div class="grid">
                <div class="card">
                    <h3 class="card-title"><i class="fas fa-image"></i>图片阴影效果</h3>
                    <div class="example example-image">外阴影使图片突出</div>
                    <div class="code">box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);</div>
                </div>
                
                <div class="card">
                    <h3 class="card-title"><i class="fas fa-image"></i>图片内阴影效果</h3>
                    <div class="example example-image-2">内阴影创建暗角效果</div>
                    <div class="code">box-shadow: inset 0 0 50px rgba(0, 0, 0, 0.7);</div>
                </div>
                
                <div class="card">
                    <h3 class="card-title"><i class="fas fa-mouse-pointer"></i>交互按钮效果</h3>
                    <div class="example" style="display: flex; justify-content: center; align-items: center;">
                        <a href="https://www.ebingou.cn/biancheng/" class="button">悬停查看效果</a>
                    </div>
                    <div class="code">/* 默认状态 */<br>box-shadow: 0 4px 6px rgba(74, 108, 247, 0.3);<br><br>/* 悬停状态 */<br>box-shadow: 0 6px 12px rgba(74, 108, 247, 0.4);</div>
                </div>
            </div>
            
            <h2>本节课程知识要点</h2>
            <div class="card">
                <ul style="padding-left: 20px; margin: 15px 0;">
                    <li>box-shadow语法:水平偏移 垂直偏移 模糊半径 扩散半径 颜色</li>
                    <li>使用inset关键字创建内阴影效果</li>
                    <li>通过逗号分隔实现多重阴影效果</li>
                    <li>阴影颜色可以使用RGBA实现半透明效果</li>
                    <li>结合transition实现平滑的阴影动画效果</li>
                    <li>合理运用阴影增强用户界面的层次感和交互反馈</li>
                </ul>
            </div>
            
            <div style="text-align: center; margin: 40px 0;">
                <a href="https://www.ebingou.cn/biancheng/" class="button">查看更多编程教程</a>
            </div>
        </main>
        
        <footer>
            <p>© 2025 代码号编程学习 | 提供高质量的编程教程和资源</p>
            <p>访问 <a href="https://www.ebingou.cn/" style="color: #4a6cf7;">代码号</a> 获取更多学习资源</p>
        </footer>
    </div>
</body>
</html>

本节课程知识要点

  1. 理解 box-shadow 参数:掌握水平偏移、垂直偏移、模糊半径、扩散半径和颜色五个基本参数的作用和用法

  2. 多重阴影应用:学会使用逗号分隔符为单个元素添加多个阴影效果

  3. 内阴影技巧:使用 inset 关键字创建元素内部阴影效果

  4. 动态阴影实现:结合 CSS 过渡和动画功能创建交互式阴影效果

  5. 实际应用场景:将阴影效果应用于按钮、卡片等常见UI元素,提升用户体验

总结

CSS 的 box-shadow 属性是实现现代网页设计效果的重要工具之一。通过灵活运用其各种参数和技巧,开发者可以为网页元素添加丰富的视觉效果,从而增强页面的视觉层次感和用户体验。从简单的边框阴影到复杂的多重阴影组合,这一属性提供了广泛的设计可能性,是前端开发者必须掌握的核心技能之一。

更多CSS教程和编程知识,请访问代码号编程教程栏目。

← CSS word-wrap 换行 CSS text-shadow 文本阴影 →
分享笔记 (共有 篇笔记)
验证码:
微信公众号