相对定位,ie 7中的div堆叠问题
这是我的示例代码,它在 IE7 中没有按预期工作 - 我认为position:relative; 的问题是
.oner {
position:relative;
height:50px;
background:#fff;
border:5px solid #e4e4e4;
height:200px;
margin-top:20px;
}
.onea {
position:absolute;
height:500px;
right:0;
width:200px;
background: #eee;
z-index:999;
}
.onet {
position:absolute;
height:500px;
left:0;
width:200px;
background:red;
z-index:999;
}
IE7 HTML
<div style="height:500px;width:900px;margin:auto;">
<div class="oner">
<div class="onea">IE IE7 this div goes behind the "oner" div below </div>
</div>
<div class="oner">
<div class="onet">My name is Sumit Kumar Ray my email is ..</div>
</div>
</div>
: onea
div 位于以下 oner
div 后面,但在其他浏览器中它会覆盖它
This is my example code which is not working as expected in IE7 - I think position:relative; is the issue for IE7
.oner {
position:relative;
height:50px;
background:#fff;
border:5px solid #e4e4e4;
height:200px;
margin-top:20px;
}
.onea {
position:absolute;
height:500px;
right:0;
width:200px;
background: #eee;
z-index:999;
}
.onet {
position:absolute;
height:500px;
left:0;
width:200px;
background:red;
z-index:999;
}
HTML:
<div style="height:500px;width:900px;margin:auto;">
<div class="oner">
<div class="onea">IE IE7 this div goes behind the "oner" div below </div>
</div>
<div class="oner">
<div class="onet">My name is Sumit Kumar Ray my email is ..</div>
</div>
</div>
What happens is that the onea
div goes behind the following oner
div, but in other browsers it overlays it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 div 上设置
z-index
实际上应该创建一个堆叠上下文,而不是简单地引入 div,它会应用于另一个之上......所以虽然我确实认为 IE7 并没有完全理解它是的,(惊讶!)我认为最好通过设置
z-index
来使oner
div 成为创建堆栈开头的 div,并且你首先想要什么oner
具有比第二个更高的z-index
,因此绝对定位子项根本不需要具有 z-index,因为这些 div 现在采用它们的“z 层”来自其相对定位的父级 - IE 和堆栈可能会非常混乱!
CSS:
然而,这确实意味着,如果您有两个以上的 div,如本示例所示,您需要在所有
oner
div 上设置级别,第一个是最高的。(这就是为什么我将HTML 中内联的oner
样式(如果您有更多样式,您可能需要更多的类来分隔它们)setting a
z-index
on a div is actually supposed to create a stacking context, not simply bring the div, it's applied to, above another.. so while I do think IE7 didn't get it quite right, (surprise!)I think it would be better to make the
oner
divs the ones that create the start of the stack by setting thez-index
on them, and what you want it for the firstoner
to have a higherz-index
than the secondwith this there is no need for the Absolutely Positioned children to have a z-index at all, as those divs now take their "z level" from their relatively positioned parent - IE and the stack can be quite confusing!
CSS:
However it does mean that if you have more than two as in this example you need to set the levels on all the
oner
divs with the first one being the highest.. (that's why I put theoner
style inline in the HTML if you have more you might need some more classes to separate them)由于两个内部 div 的 zindex 均为 999,因此第二个 div 应该覆盖第一个,尽管 zindex 结果在浏览器中可能无法预测。实际上,您应该设置不同的 zindex 值来准确控制深度。
Since both the inner divs have a zindex of 999 the second should overlay the first, although zindex results can be unpredictable across browsers. Really you should set different zindex values to accurately control depth.