← CSS word-break 属性 SASS CSS预处理器 →

CSS 背景模糊与滤镜

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

CSS 背景模糊与滤镜效果详细教程

概述

CSS 背景模糊效果是现代网页设计中常用的视觉技术,能够创建出专业且吸引人的用户界面。本教程将详细介绍两种实现背景模糊的方法:filter 属性和 backdrop-filter 属性,并通过丰富示例展示它们的应用场景和效果差异。

基础概念

filter 属性

filter 属性用于对元素本身应用图形效果,如模糊、亮度调整和颜色转换等。

基本语法:

.element {
  filter: <filter-function> [<filter-function>]* | none;
}

backdrop-filter 属性

backdrop-filter 属性用于对元素背后的区域应用图形效果,创建"毛玻璃"效果。

基本语法:

.element {
  backdrop-filter: <filter-function> [<filter-function>]* | none;
}

实现背景模糊的两种方法

方法一:使用 filter 属性

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>代码号 - filter属性实现背景模糊</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: 'Arial', sans-serif;
        }
        
        .hero-section {
            position: relative;
            height: 100vh;
            overflow: hidden;
        }
        
        .hero-section::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-image: url('https://www.ebingou.cn/biancheng/images/1.jpg');
            background-size: cover;
            background-position: center;
            filter: blur(8px);
            z-index: -1;
        }
        
        .content {
            position: relative;
            z-index: 1;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            height: 100%;
            color: white;
            text-align: center;
            padding: 20px;
        }
        
        .content h1 {
            font-size: 2.5rem;
            margin-bottom: 20px;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
        }
        
        .content p {
            font-size: 1.2rem;
            max-width: 600px;
            line-height: 1.6;
            text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
        }
        
        .cta-button {
            margin-top: 30px;
            padding: 12px 30px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 1.1rem;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        
        .cta-button:hover {
            background-color: #2980b9;
        }
    </style>
</head>
<body>
    <section class="hero-section">
        <div class="content">
            <h1>欢迎来到代码号编程学习平台</h1>
            <p>在这里,您可以学习最新的前端开发技术,包括CSS滤镜效果、JavaScript框架和响应式设计等专业知识。</p>
            <button class="cta-button">开始学习</button>
        </div>
    </section>
</body>
</html>

方法二:使用 backdrop-filter 属性

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>代码号 - backdrop-filter属性实现毛玻璃效果</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: 'Arial', sans-serif;
            background-image: url('https://www.ebingou.cn/biancheng/images/2.jpg');
            background-size: cover;
            background-position: center;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        .card {
            width: 80%;
            max-width: 600px;
            padding: 40px;
            background-color: rgba(255, 255, 255, 0.2);
            backdrop-filter: blur(10px);
            border-radius: 15px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
        }
        
        .card h2 {
            color: #2c3e50;
            margin-bottom: 20px;
            font-size: 2rem;
        }
        
        .card p {
            color: #34495e;
            line-height: 1.6;
            font-size: 1.1rem;
        }
        
        .features {
            display: flex;
            justify-content: space-between;
            margin-top: 30px;
        }
        
        .feature {
            text-align: center;
            padding: 15px;
            background-color: rgba(255, 255, 255, 0.3);
            border-radius: 8px;
            flex: 1;
            margin: 0 10px;
        }
        
        .feature h3 {
            color: #2c3e50;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <div class="card">
        <h2>CSS backdrop-filter 属性</h2>
        <p>backdrop-filter 属性可以让你为一个元素后面的区域添加图形效果(如模糊或颜色偏移)。因为它适用于元素背后的所有元素,为了看到效果,必须使元素或其背景至少部分透明。</p>
        
        <div class="features">
            <div class="feature">
                <h3>模糊效果</h3>
                <p>使用blur()函数创建毛玻璃效果</p>
            </div>
            <div class="feature">
                <h3>亮度调整</h3>
                <p>使用brightness()调整背景亮度</p>
            </div>
            <div class="feature">
                <h3>对比度</h3>
                <p>使用contrast()增强背景对比度</p>
            </div>
        </div>
    </div>
</body>
</html>

CSS 滤镜函数详解

1. blur() - 模糊效果

.blur-effect {
  filter: blur(5px); /* 对元素本身应用模糊 */
}

.backdrop-blur {
  backdrop-filter: blur(10px); /* 对元素背后区域应用模糊 */
}

2. brightness() - 亮度调整

.brightness-effect {
  filter: brightness(150%); /* 增加亮度 */
}

.low-brightness {
  filter: brightness(50%); /* 降低亮度 */
}

3. contrast() - 对比度调整

.contrast-effect {
  filter: contrast(200%); /* 增加对比度 */
}

.low-contrast {
  filter: contrast(50%); /* 降低对比度 */
}

4. grayscale() - 灰度效果

.grayscale-effect {
  filter: grayscale(100%); /* 灰度 */
}

.partial-grayscale {
  filter: grayscale(50%); /* 部分灰度 */
}

5. sepia() - 褐色滤镜

.sepia-effect {
  filter: sepia(100%); /* 褐色滤镜 */
}

.partial-sepia {
  filter: sepia(50%); /* 部分褐色滤镜 */
}

6. hue-rotate() - 色相旋转

.hue-rotate-effect {
  filter: hue-rotate(90deg); /* 色相旋转90度 */
}

.full-hue-rotate {
  filter: hue-rotate(360deg); /* 色相旋转 */
}

7. saturate() - 饱和度调整

.saturate-effect {
  filter: saturate(200%); /* 增加饱和度 */
}

.desaturate-effect {
  filter: saturate(50%); /* 降低饱和度 */
}

8. 多滤镜组合使用

.multiple-filters {
  filter: blur(2px) brightness(120%) contrast(110%) saturate(130%);
}

.backdrop-multiple {
  backdrop-filter: blur(8px) brightness(80%) contrast(120%);
}

实际应用示例

示例1:图片画廊模糊效果

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>代码号 - 图片画廊模糊效果</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        
        .gallery {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
            gap: 20px;
            max-width: 1200px;
            margin: 0 auto;
        }
        
        .gallery-item {
            position: relative;
            overflow: hidden;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s, box-shadow 0.3s;
            height: 200px;
        }
        
        .gallery-item:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
        }
        
        .gallery-item img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: filter 0.3s;
        }
        
        .gallery-item:hover img {
            filter: blur(3px) brightness(0.7);
        }
        
        .gallery-caption {
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            background-color: rgba(0, 0, 0, 0.7);
            color: white;
            padding: 15px;
            transform: translateY(100%);
            transition: transform 0.3s;
            backdrop-filter: blur(5px);
        }
        
        .gallery-item:hover .gallery-caption {
            transform: translateY(0);
        }
        
        .gallery-title {
            margin: 0 0 5px 0;
            font-size: 1.2rem;
        }
        
        .gallery-desc {
            margin: 0;
            font-size: 0.9rem;
            opacity: 0.8;
        }
    </style>
</head>
<body>
    <div class="gallery">
        <div class="gallery-item">
            <img src="https://www.ebingou.cn/biancheng/images/3.jpg" alt="编程学习">
            <div class="gallery-caption">
                <h3 class="gallery-title">HTML5基础</h3>
                <p class="gallery-desc">学习现代网页结构</p>
            </div>
        </div>
        
        <div class="gallery-item">
            <img src="https://www.ebingou.cn/biancheng/images/4.jpg" alt="CSS教程">
            <div class="gallery-caption">
                <h3 class="gallery-title">CSS3高效</h3>
                <p class="gallery-desc">掌握样式设计技巧</p>
            </div>
        </div>
        
        <div class="gallery-item">
            <img src="https://www.ebingou.cn/biancheng/images/5.jpg" alt="JavaScript编程">
            <div class="gallery-caption">
                <h3 class="gallery-title">JavaScript进阶</h3>
                <p class="gallery-desc">深入了解交互编程</p>
            </div>
        </div>
        
        <div class="gallery-item">
            <img src="https://www.ebingou.cn/biancheng/images/6.jpg" alt="响应式设计">
            <div class="gallery-caption">
                <h3 class="gallery-title">响应式设计</h3>
                <p class="gallery-desc">适配多设备网页开发</p>
            </div>
        </div>
    </div>
</body>
</html>

示例2:登录表单毛玻璃效果

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>代码号 - 毛玻璃登录表单</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: 'Arial', sans-serif;
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            background-image: url('https://www.ebingou.cn/biancheng/images/2.jpg');
            background-size: cover;
            background-position: center;
        }
        
        .login-container {
            width: 100%;
            max-width: 400px;
            padding: 30px;
            background-color: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(12px);
            border-radius: 15px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
            border: 1px solid rgba(255, 255, 255, 0.2);
        }
        
        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .login-header h2 {
            color: #2c3e50;
            margin-bottom: 10px;
        }
        
        .login-header p {
            color: #7f8c8d;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #2c3e50;
            font-weight: bold;
        }
        
        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid rgba(255, 255, 255, 0.3);
            border-radius: 6px;
            background-color: rgba(255, 255, 255, 0.1);
            color: #2c3e50;
            font-size: 16px;
            backdrop-filter: blur(5px);
        }
        
        .form-group input:focus {
            outline: none;
            border-color: #3498db;
            background-color: rgba(255, 255, 255, 0.2);
        }
        
        .login-button {
            width: 100%;
            padding: 12px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        
        .login-button:hover {
            background-color: #2980b9;
        }
        
        .login-footer {
            text-align: center;
            margin-top: 20px;
            color: #7f8c8d;
        }
        
        .login-footer a {
            color: #3498db;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-header">
            <h2>欢迎回来</h2>
            <p>请登录您的代码号账户</p>
        </div>
        
        <form>
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" placeholder="请输入用户名">
            </div>
            
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" placeholder="请输入密码">
            </div>
            
            <button type="submit" class="login-button">登录</button>
        </form>
        
        <div class="login-footer">
            <p>还没有账户?<a href="#">立即注册</a></p>
        </div>
    </div>
</body>
</html>

浏览器兼容性考虑

虽然现代浏览器对CSS滤镜效果的支持已经相当不错,但在实际项目中仍需考虑兼容性方案:

.element {
  /* 标准属性 */
  filter: blur(5px);
  /* Webkit前缀 */
  -webkit-filter: blur(5px);
  /* 备用方案 */
  background-color: rgba(0, 0, 0, 0.5);
}

.backdrop-element {
  /* 标准属性 */
  backdrop-filter: blur(10px);
  /* Webkit前缀 */
  -webkit-backdrop-filter: blur(10px);
}

性能优化建议

  1. 谨慎使用:过度使用滤镜效果可能影响页面性能,特别是在低性能设备上

  2. 硬件加速:确保浏览器能够使用GPU加速渲染滤镜效果

  3. 限制范围:只对必要元素应用滤镜效果,避免大面积使用

  4. 备用方案:为不支持滤镜效果的浏览器提供视觉降级方案

本节课程知识要点

  1. 两种模糊技术

    • filter: blur() 用于元素本身的效果

    • backdrop-filter: blur() 用于元素背后区域的毛玻璃效果

  2. 常用滤镜函数

    • 模糊效果:blur()

    • 亮度调整:brightness()

    • 对比度调整:contrast()

    • 灰度效果:grayscale()

    • 饱和度调整:saturate()

    • 色相旋转:hue-rotate()

  3. 应用场景

    • 图片画廊的悬停效果

    • 模态框和对话框的背景模糊

    • 卡片式设计的视觉增强

    • 登录表单的毛玻璃效果

  4. 兼容性处理

    • 使用前缀确保跨浏览器兼容

    • 提供降级方案增强用户体验

  5. 性能考虑

    • 避免过度使用滤镜效果

    • 确保硬件加速优化

← CSS word-break 属性 SASS CSS预处理器 →
分享笔记 (共有 篇笔记)
验证码:
微信公众号