使用CSS模糊没有背景图像的元素

时间:2020-03-29 22:21:31

标签: html css blur

background-image标签上使用body,然后 里面有两个div。 我需要每个背景都模糊 div
我现在能做的是将background-image设置为 每个div并使用filter: blur属性对其进行模糊处理。

但是因为我希望整个页面具有相同的背景(每个div都不分开), 我想要的方式是设置background-imagebody标签上,然后模糊div元素。

这就是我尝试过的:

body {
    background-image: url(public/images/billy-huynh-W8KTS-mhFUE-unsplash\ \(1\).jpg);
    background-attachment: fixed;
    background-size: cover;
    display: grid;
    align-items: center;
    justify-content: center;
    height: 100vh;
    width: 100vw;
    position: relative;
}

.parent-div::before {
    content: "";
    display: block;
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    left: 0;
    width: 100%;
    height: 100%;
    filter: blur(5px);

}

.parent-div {
    position: relative;
}

HTML代码:

<body>
<div class=" parent-div">
    <div class=" child-div">
        <h4 class=" text-light">Hello world!!</h4>
    </div>
</div>
<div class=" parent-div">
    <div class=" child-div">
        <h4 class=" text-light">Hello world!!</h4>
    </div>
</div>

事实上,我想模糊div元素而没有背景图像。 有办法吗? 谢谢

1 个答案:

答案 0 :(得分:2)

您正在寻找的是backdrop-filter

body {
  background-image: url(https://i.picsum.photos/id/174/800/800.jpg);
  background-attachment: fixed;
  background-size: cover;
  height: 100vh;
  margin:0;
  position: relative;
}


.parent-div {
  position: relative;
  height:50vh;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
<div class=" parent-div">
  <div class=" child-div">
    <h4 class=" text-light">Hello world!!</h4>
  </div>
</div>
<div class=" parent-div">
  <div class=" child-div">
    <h4 class=" text-light">Hello world!!</h4>
  </div>
</div>