gpt4 book ai didi

html - 图像被 css :before pseudo-element unless opacity is different than 1 隐藏

转载 作者:太空宇宙 更新时间:2023-11-04 15:28:12 26 4
gpt4 key购买 nike

我尝试使用 css 伪元素为图像设置背景颜色:

<div class="my">
<img src="http://cdn.impressivewebs.com/123rf-jan.jpg"/>
</div>

CSS:

img {
height: 100px;
width: 100px;
}

.my:before {
content: "";
display: block;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgb(0,255,255);
}

div {
position: absolute;
width: 200px;
height: 200px;
background-color: red;
}

我设法让它工作的唯一方法是设置 opacity:0.99; 或任何其他不为 1 的值。我尝试使用 z-index 但没有成功。这在 Chrome 和 Firefox 中都会发生。

opacity 设置为 1 或将其保留为默认值会导致伪元素隐藏图像。这是 fiddle :http://jsfiddle.net/zfYnu/

最佳答案

您还需要定位图像:

img {
position: relative;
height: 100px;
width: 100px;
}

Updated fiddle

opacity 设置为小于 1 会导致图像创建自己的堆叠上下文。这会导致图像将自身堆叠在您已定位的 :before 伪元素之上。如果您将 opacity 保留为默认值 1,则不会发生这种情况,这会导致 :before 伪元素改为堆叠在图像顶部。

参见 CSS Color level 3 module有关 opacitysection 9.9 of CSS2.1 的信息有关堆栈上下文的详细信息。

关于html - 图像被 css :before pseudo-element unless opacity is different than 1 隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17300183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com