CSS实现背景图片高斯模糊效果

效果演示

未添加高斯模糊的效果:

代码:

body {
    background: url(./images/bg.jpg);
    background-size: cover;
}

添加了高斯模糊的效果:

代码:

body {
    /* 省略部分代码 */
}
body::before {
    content: "";
    position: absolute; /* 一定要用绝对定位 */
    width: 100%;
    height: 100%;
    backdrop-filter: blur(30px); /* 模糊半径 */
}

这里是通过 ::before 伪元素实现,原理就是给 body 添加了一个拥有高斯模糊的遮罩层。

当然也适用于其他元素,举个例子:

代码:

.img-show {
    /* 省略部分代码 */
}
.img-show::before {
    content: "";
    position: absolute; /* 一定要用绝对定位 */
    width: 100%;
    height: 100%;
    border-radius: 15px; /* 这里记得给遮罩层加一个圆角!!! */
    backdrop-filter: blur(30px); /* 模糊半径 */
}

源代码

<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>高斯模糊效果</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            background: url(./images/bg.jpg);
            background-size: cover;
        }
        body::before {
            content: "";
            position: absolute; /* 一定要用绝对定位 */
            width: 100%;
            height: 100%;
            backdrop-filter: blur(30px); /* 模糊半径 */
        }
        .img-show {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 750px;
            height: 450px;
            border: 2px solid #bebebe;
            border-radius: 15px;
            box-shadow: 8px 8px 20px rgba(0, 0, 0, .3), -8px -8px 20px rgba(0, 0, 0, .3);
            background: url(./images/show.jpg);
            background-size: cover;
        }
    </style>
</head>
<body>
    <div class="img-show"></div>
</body>
</html>

  • 9
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@cooltea

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值