绝对定位元素上的 Z-Index 问题
我试图让一个绝对定位的元素出现在另一个元素后面。我已经设置了 z 索引,但似乎没有任何效果。我试图放置在背景中的元素却位于最前面。
所讨论的两个元素都位于位置设置为相对的容器内。据我了解,这将创建一组新的堆叠元素,尽管我不确定这是否真的很重要。
HTML:
<div id="wrapper">
<div id="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer augue</p>
<p>massa, scelerisque non viverra sagittis, egestas nec erat. Aliquam vel</p>
</div>
<div id="bg"> </div>
</div>
CSS:
#wrapper {
background: #fdd;
position: relative;
width: 300px;
}
#content{
width: 300px;
z-index: 100;
}
#bg{
background: #ddf;
position: absolute;
bottom: 0;
left: 0;
top: 0;
width: 40%;
z-index:1;
}
您可以在此处查看结果: http://jsfiddle.net/GsG28/
这是尽管我还没有测试过其他浏览器,但 Chrome 和 IE 中都会发生这种情况。我得到的是这样的:
我想要的是文本位于蓝色背景之上。
I'm trying to get an absolutely positioned element to appear behind another element. I've set the z-indices but it doesn't seem to have any effect. The element that I am trying to place in the background is instead at the forefront.
Both elements in question are inside a container with a position set to relative. It was my understanding that this would create a fresh set of stacking elements although I'm not sure if that is even really important.
HTML:
<div id="wrapper">
<div id="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer augue</p>
<p>massa, scelerisque non viverra sagittis, egestas nec erat. Aliquam vel</p>
</div>
<div id="bg"> </div>
</div>
CSS:
#wrapper {
background: #fdd;
position: relative;
width: 300px;
}
#content{
width: 300px;
z-index: 100;
}
#bg{
background: #ddf;
position: absolute;
bottom: 0;
left: 0;
top: 0;
width: 40%;
z-index:1;
}
You can see the result here: http://jsfiddle.net/GsG28/
This is occurring in both Chrome and IE, although I haven't tested other browsers. What I am getting is this:
What I want is for the text to be on top of the blue background.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
#content
需要一个position:relative;
才能应用z-index
。#content
needs aposition: relative;
for thez-index
to apply to it.您的
#content
区域缺少其定位:Your
#content
area is missing its positioning: