backdrop-filter 与 filter 区别

117 阅读1分钟

image.png

结论

backdrop-filter 会将后面的背景模糊,但是里面的子元素依然正常展示。

filter 会让其元素本身以及子元素都模糊。

backdrop-filter 属性可以为该元素后面区域添加图形效果(如模糊或颜色偏移)。因为它适用于元素背后的所有元素。MDN文档

示例代码:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .element-with-background1 {
        background-image: url('your-image.webp');/* 你的图片 */
      }
      .child1 {
        backdrop-filter: blur(10px); 
      }

      .element-with-background2 {
        background-image: url('your-image.webp');/* 你的图片 */
        filter: blur(10px);
      }
      .container {
        display: flex;
        padding: 20px;
      }

      .element-with-background1, .element-with-background2 {
        background-size: contain;
        background-repeat: no-repeat;
        width: 300px;
        height: 200px;
        color: red;
      }

      .child1, .child2 {
        width: 200px;
        height: 100px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="element-with-background1">
        <div class="child1">
          <span>backdrop-filter: blur(10px)</span>
        </div>
      </div>
      <div class="element-with-background2">
        <div class="child2">
          <span>filter: blur(10px)</span>
        </div>
    </div>
  </body>
</html>