html - 将阴影与背景滤镜一起使用 : blur

标签 html css shadow blur

我最近一直在尝试使用 backdrop-filter,通常用它来模糊元素后面的任何内容(这是动态的,所以我不能只使用像 this 这样的东西)。但是,我还需要对所述元素应用阴影,所以我简单地添加了 box-shadow: /* something, not inset */

不幸的是,模糊效果被扩展到了阴影覆盖的所有区域(这似乎合乎逻辑,因为它被称为背景滤镜)。您可以在下面看到它的演示(请注意,您将需要一个支持 backdrop-filter 的浏览器,以防这还不明显)。

#background {
  position: absolute;
  
  width: 600px;
  height: 300px;
  z-index: -1;
  
  background-image: url('https://picsum.photos/600/300/');
  background-repeat: no-repeat;
}

#blurryandshadowy {
  display: inline-block;

  margin: 50px;
  padding: 50px;
  
  background: rgba(255, 255, 255, 0.25);
  backdrop-filter: blur(15px);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.25);
}
<div id="background"></div>
<div id="blurryandshadowy">
  . . .
  <br>. . .
</div>

有没有办法应用与背景滤镜不冲突的阴影(冲突甚至是动词吗?)?如果有必要,我还可以使用 JavaScript(这就是为什么它出现在这篇文章的标签中)——当然,仅使用 CSS 的答案会更好。

编辑:在 2020 年正常工作🙃

最佳答案

您可能可以使用伪元素拥有两个不同的层。一层用于滤镜,另一层用于阴影:

#background {
  position: absolute;
  width: 600px;
  height: 300px;
  z-index: -1;
  background-image: url('https://lorempixel.com/600/300/');
  background-repeat: no-repeat;
}

#blurryandshadowy {
  display: inline-block;
  margin: 50px;
  padding: 50px;
  position: relative;
  z-index: 0;
}

#blurryandshadowy:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.25);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  z-index: 1;
}

#blurryandshadowy:after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.25);
}
<div id="background"></div>
<div id="blurryandshadowy">
  . . .
  <br>. . .
</div>

关于html - 将阴影与背景滤镜一起使用 : blur,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52140378/

相关文章:

html - iOS 视网膜图像大小限制?

php - 如何在php中检索文本框数据

css - 样式化的 unicode 箭头未显示为 Google Fonts 网络字体

CSS:是否可以在无背景 PNG 图像的轮廓上添加阴影?

html - 如何在页脚和页面底部之间放置空格?

flutter - 从 Flutter 中的 Scaffold AppBar 中删除阴影?

javascript - AngularJS:如何在选择另一个跨度时取消选择一个跨度

html - TD :before and vertical-align:middle

css - 如何使具有多个背景的元素没有纯色与透明 png 混淆?

css - 如何在我的 css 中设置默认的伪样式?