html - 添加过滤器 : grayscale to img elements hides overlaid elements

标签 html css

由于一些有趣的小样式指南更改,所有图像都必须是黑白的。我认为全局样式可以解决这个问题:

img {
    filter: grayscale(100%)
}

然而,它就像一个魅力:位于图像顶部的元素突然消失。例如:位于横幅图像顶部的文本消失,位于图像顶部的 ::before 元素消失。

这是预期的行为还是有什么不祥之兆?

编辑:

HTML 没有什么特别的:

<div class="wrapper">
    <div class="header-text">
        <h2>My Header Text</h2>
    </div>
    <img src="myimage.png" />
</div>

.header-text 是绝对定位的。

最佳答案

过滤器会影响堆叠顺序,可以通过为 header-text div 提供 z-index 值来调整堆叠顺序。

这在规范中有记录。

Filter Property

A computed value of other than none results in the creation of a stacking context [CSS21] the same way that CSS opacity does.

.wrapper {
  position: relative;
}

.header-text {
  position: absolute;
  color: white;
  z-index: 1;
}

img {
  filter: grayscale(100%);
}
<div class="wrapper">
  <div class="header-text">
    <h2>My Header Text</h2>
  </div>
  <img src="http://www.placebacon.net/400/300?image=1" />
</div>

关于html - 添加过滤器 : grayscale to img elements hides overlaid elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42906653/

相关文章:

html - Flexslider 淡入淡出问题

javascript - 使用浏览器滚动条滚动固定的 div 内容

html - 如何在消息聊天框中显示姓名、当前时间

css - 如何将div中的元素居中对齐

html - 占位符文本溢出 :ellipsis in IE10 not working

html - 为什么此 html 表定义无效?

html - css:如何在不增加整个文本行高的情况下增加一个字符的字体大小?

javascript - 使用 Jquery 设置单选按钮值(多于一个单词/包含空格)

javascript - 为什么分页链接没有显示在我的网站上?

html - 将代码复制到下拉菜单按钮以创建第二个下拉菜单按钮时遇到问题