IE7/8可以在position:absolute元素前面显示position:relative元素吗?
我在标题区域有一个基于绝对定位的布局和一个基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,没关系。在
#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 thez-index
on#nav li ul
in the code above is not required.