为什么 z-index 不能正确排序元素?
我正在尝试将蓝色笑脸置于黄色笑脸下方。我做错了什么?
#group {
position: relative
}
#sitename {
z-index: 1
}
#listen {
position: absolute;
top: 150px;
z-index: 0
}
<div id="group">
<h1 id="sitename">
<a href="/"><img src="http://i5.photobucket.com/albums/y196/dannydude182/smiley-300x300.png" /></a>
</h1>
<a id="listen" target="_blank" href="/"><img src="http://i925.photobucket.com/albums/ad96/emly_haha/annoyed-smiley.jpg" /></a>
</div>
I'm trying to get the blue smiley underneath the yellow smiley. What am I doing wrong?
#group {
position: relative
}
#sitename {
z-index: 1
}
#listen {
position: absolute;
top: 150px;
z-index: 0
}
<div id="group">
<h1 id="sitename">
<a href="/"><img src="http://i5.photobucket.com/albums/y196/dannydude182/smiley-300x300.png" /></a>
</h1>
<a id="listen" target="_blank" href="/"><img src="http://i925.photobucket.com/albums/ad96/emly_haha/annoyed-smiley.jpg" /></a>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
z-index
规则根据元素是否定位而变化。如果您将两个元素都定位,则它可以工作: http://jsfiddle.net/zPJm2/4/它比我们大多数人刚开始时想象的要复杂一些。非常值得阅读 z-index 上的实际 CSS 规范 了解详细信息,但基本上,有多个堆栈上下文。根据规范:
z-index
rules vary depending on whether the element is positioned or not. If you make both elements positioned, it works: http://jsfiddle.net/zPJm2/4/It's a bit more complicated than most of us think it is when we first start with it. It's well worth reading the actual CSS specification on
z-index
for the details, but basically, there are multiple stacking contexts. From the spec:如果您希望绝对 div z-index 位于任何相对索引后面,请将其设置为小于零。
Set the absolute div z-index to be less than zero if you want it behind any relative ones.
一种可能性是检查元素的默认样式。
我试图在幻灯片放映上重叠具有 Ul 和 LI 元素的菜单下拉菜单。我增加了 ul 上的 z 索引,但没有注意到 li 上的 z 索引为 1(之前编码)以防止重叠。
希望这对某人有帮助
谢谢
法尔汗
One possibility is to check the default styles on elements.
I was trying to overlap a menu dropdown having Ul and LI elements over a slide show. I increased the z index on ul but did not notice a z-index of 1 (coded before) on li preventing the overlapping.
hope this help someone
Thanks
Farhan