IE7/8可以在position:absolute元素前面显示position:relative元素吗?

发布于 2024-09-18 04:25:20 字数 1796 浏览 4 评论 0原文

我在标题区域有一个基于绝对定位的布局和一个基于 CSS 的菜单。在 Firefox 中一切正常,但在 Internet Explorer 7 和 8 中,菜单下拉菜单被内容区域覆盖。在这种情况下,IE 根本不支持 z-index 属性。

我读到了有关堆叠上下文和 z-index bug 的信息,但仍然无法理解去工作。有没有办法修复它而不删除 #content 上的 position:absolute ?不过,我可以更改导航区域使用的定位方法。

这是简化的代码:

<!DOCTYPE html>
<html>
<head>
<style>
#nav {margin:0; padding: 0; position: absolute; left: 0; top: 0; right: 0; height: 2em; background: #eee}
#nav > li {float: left; list-style: none; position: relative; }
#nav > li > a {display: block; position: relative; margin: 0; background-color: #eee; padding: 5px; border: 1px solid #eee; text-decoration: none; color: #444;}
#nav > li:hover > a { background-color: #ddd; }

#nav li ul { z-index: 1; display: none; position: absolute; padding: 0; margin: 0; list-style: none; border: 1px solid black; background: #eee; width: 100px; }
#nav li:hover ul {display: inline;}
#nav li ul li {list-style: none; margin: 0; padding: 0; height: 5em; }
#nav li ul li a {display: block; padding: 5px; margin: 0; color: #444; text-decoration: none; white-space: nowrap;}

#content { position: absolute; top: 3em; left: 0; right: 0; bottom: 0; background: #ccc; }
</style>
</head>
<body>

<ul id="nav">
    <li><a href="#">A</a><ul><li>...</li></ul></li>
    <li><a href="#">Group B</a><ul><li>...</li></ul></li>
    <li><a href="#">The third group</a><ul><li>...</li></ul></li>
</ul>

<div id="content"></div>

</body>
</html>

I have a layout based on absolute positioning and a CSS-based menu in the header area. Everything works fine in Firefox, but in Internet Explorer 7 and 8, the menu dropdowns are covered by the content area. IE simply does not honour the z-index property in this case.

I read about stacking contexts and the z-index bug, but still could not get it to work. Is there some way to fix it without dropping the position:absolute on #content? I can change the positioning method used for the navigation area, though.

Here's the simplified code:

<!DOCTYPE html>
<html>
<head>
<style>
#nav {margin:0; padding: 0; position: absolute; left: 0; top: 0; right: 0; height: 2em; background: #eee}
#nav > li {float: left; list-style: none; position: relative; }
#nav > li > a {display: block; position: relative; margin: 0; background-color: #eee; padding: 5px; border: 1px solid #eee; text-decoration: none; color: #444;}
#nav > li:hover > a { background-color: #ddd; }

#nav li ul { z-index: 1; display: none; position: absolute; padding: 0; margin: 0; list-style: none; border: 1px solid black; background: #eee; width: 100px; }
#nav li:hover ul {display: inline;}
#nav li ul li {list-style: none; margin: 0; padding: 0; height: 5em; }
#nav li ul li a {display: block; padding: 5px; margin: 0; color: #444; text-decoration: none; white-space: nowrap;}

#content { position: absolute; top: 3em; left: 0; right: 0; bottom: 0; background: #ccc; }
</style>
</head>
<body>

<ul id="nav">
    <li><a href="#">A</a><ul><li>...</li></ul></li>
    <li><a href="#">Group B</a><ul><li>...</li></ul></li>
    <li><a href="#">The third group</a><ul><li>...</li></ul></li>
</ul>

<div id="content"></div>

</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

抚笙 2024-09-25 04:25:20

好吧,没关系。在 #nav#content(分别为 2 和 1)上放置显式 z-index 就可以了。请注意,上述代码中 #nav li ul 上的 z-index 不是必需的。

Okay, never mind. Putting an explicit z-index on #nav and #content (2 and 1, respectively) did the trick. Note that the z-index on #nav li ul in the code above is not required.

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