gpt4 book ai didi

html - 图像元素的背景图像 z-index 问题

转载 作者:太空宇宙 更新时间:2023-11-04 03:46:57 24 4
gpt4 key购买 nike

我想将背景图像叠加到图像上。密码是here .如何将背景图像放在 img 元素上?

我的 html 是;

<div id="sidebar">
<div class="editor-select">
<img src="http://i.imgur.com/mh3noQH.jpg" alt="img"/>
</div>
</div>

我的CSS是;

  #sidebar{
width: 344px;
background: url("http://i.imgur.com/BQc7nYP.png") no-repeat top right !important;
float: left;
height: 500px;
margin-top: 45px;
padding: 8px;
position: relative;
z-index: 1000;
}

.editor-select{
width: 350px;
height: 360px;
position: relative;
}

.editor-select>img{
width: 320px;
height: 160px;
z-index : -1;
}

最佳答案

你不能。您的 img 元素包含在 #sidebar 元素中,这有效地使 img 元素位于 #sidebar 元素。您不能将父元素置于其子元素之上。

纯 CSS 解决方案

但是,您可以使用 pseudo-element (:before:after),位于 img 元素的顶部(我在所有 4 边都偏移了 6px 以允许图像正确重叠):

.editor-select:after {
content: '';
position: absolute;
top: -6px;
left: -6px;
height: 172px;
width: 332px;
background: url("http://i.imgur.com/BQc7nYP.png") no-repeat top right
}

CodePen Demo .

Demo

关于html - 图像元素的背景图像 z-index 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23974418/

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