最近在工作中写样式的时候碰到如图这样的背景模糊效果,思来想去没有合适的方法。后查询相关资料后得出解决方案

方法:background-color + backdrop-filter 来配合实现

backdrop-filter css属性可以让你为一个元素后面区域添加图形效果(如模糊或颜色偏移)。因为它适用于元素背后的所有元素,为了看到效果,必须使元素或其背景至少部分透明。

例:

<div class="container">
  <div class="box">
    <p>backdrop-filter: blur(10px)</p>
  </div>
</div>
html,
        body {
            height: 100%;
            width: 100%;
        }

        body {
            background-image: url(https://picsum.photos/id/1080/6858/4574), linear- 
                              gradient(rgb(219, 166, 166), rgb(0, 0, 172));
            background-position: center center;
            background-repeat: no-repeat;
            background-size: cover;
        }

        .box {
            /* 重点设置下面两个属性 */
            background-color: rgba(255, 255, 255, 0.3);
            backdrop-filter: blur(10px);
            -webkit-backdrop-filter: blur(5px);
            border-radius: 5px;
            font-family: sans-serif;
            text-align: center;
            line-height: 1;
            max-width: 50%;
            max-height: 50%;
            padding: 20px 40px;
            color: #fff;
        }

        .container {
            align-items: center;
            display: flex;
            justify-content: center;
            height: 100%;
            width: 100%;
        }

效果图:

 可自行设置想要的背景色和blue数值来达到想要的模糊效果~

参考:backdrop-filter 属性介绍

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐