相对定位,ie 7中的div堆叠问题

发布于 2024-11-17 18:01:34 字数 894 浏览 1 评论 0原文

这是我的示例代码,它在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑寡妇 2024-11-24 18:01:34

在 div 上设置 z-index 实际上应该创建一个堆叠上下文,而不是简单地引入 div,它会应用于另一个之上......所以虽然我确实认为 IE7 并没有完全理解它是的,(惊讶!)

我认为最好通过设置 z-index 来使 oner div 成为创建堆栈开头的 div,并且你首先想要什么oner 具有比第二个更高的 z-index

<div style="height:500px;width:900px;margin:auto;">
    <div class="oner" style="z-index: 1;">
        <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>

因此绝对定位子项根本不需要具有 z-index,因为这些 div 现在采用它们的“z 层”来自其相对定位的父级 - IE 和堆栈可能会非常混乱!

CSS:

.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; 
}

.onet {
  position:absolute;
  height:500px;
  left:0;
  width:200px;
  background:red; 
}

然而,这确实意味着,如果您有两个以上的 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 the z-index on them, and what you want it for the first oner to have a higher z-index than the second

<div style="height:500px;width:900px;margin:auto;">
    <div class="oner" style="z-index: 1;">
        <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>

with 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:

.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; 
}

.onet {
  position:absolute;
  height:500px;
  left:0;
  width:200px;
  background:red; 
}

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 the oner style inline in the HTML if you have more you might need some more classes to separate them)

帥小哥 2024-11-24 18:01:34

由于两个内部 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文