我已经隔离了 IE7 的 z-index
错误的一个小测试用例,但不知道如何修复它。
我整天都在玩 z-index
。
IE7 中的 z-index
有什么问题?
测试 CSS:
input {
border: 1px solid #000;
}
div {
border: 1px solid #00f;
}
ul {
border: 1px solid #f00;
background-color: #f00;
list-style-type: none;
margin: 0;
padding-left: 0;
z-index: 1000;
}
li {
color: #fff;
list-style-type: none;
padding-left: 0;
margin-left: 0;
}
span.envelope {
position: relative;
}
span.envelope ul {
position: absolute;
top: 20px;
left: 0;
width: 150px;
}
测试 HTML:
<form>
<label>Input #1:</label>
<span id="envelope-1" class="envelope">
<input name="my-input-1" id="my-input-1" />
<ul>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
</ul>
</span>
<br><br>
<label>Input #2:</label>
<span id="envelope-2" class="envelope">
<input name="my-input-2" id="my-input-2" />
</span>
</form>
最佳答案
Z-index 不是绝对测量值。 z-index: 1000 的元素可以位于 z-index: 1 的元素后面 - 只要相应元素属于不同的堆叠上下文。
当您指定 z-index 时,您是在相对于同一堆叠上下文中的其他元素指定它,尽管 CSS spec's paragraph on Z-index说一个新的堆叠上下文只为定位的内容创建,具有非自动的 z-index(意味着你的整个文档应该是一个单一的堆叠上下文),你确实构造了一个定位跨度:不幸的是,IE7 将没有 z-index 的定位内容解释为新的堆叠上下文。
简而言之,尝试添加这个 CSS:
#envelope-1 {position:relative; z-index:1;}
或者重新设计文档,这样你的 span 就不再有 position:relative 了:
<html>
<head>
<title>Z-Index IE7 Test</title>
<style type="text/css">
ul {
background-color: #f00;
z-index: 1000;
position: absolute;
width: 150px;
}
</style>
</head>
<body>
<div>
<label>Input #1:</label> <input><br>
<ul><li>item<li>item<li>item<li>item</ul>
</div>
<div>
<label>Input #2:</label> <input>
</div>
</body>
</html>
参见 http://www.brenelz.com/blog/2009/02/03/squish-the-internet-explorer-z-index-bug/对于此错误的类似示例。为父元素(在您的示例中为 envelope-1)提供更高 z-index 的原因是因为 envelope-1 的所有子元素(包括菜单)将与 envelope-1 的所有兄弟元素(特别是 envelope-2)重叠。
尽管 z-index 允许您明确定义事物的重叠方式,even without z-index the layering order is well defined .最后,IE6 有一个额外的错误,导致选择框和 iframe float 在其他所有内容之上。
关于css - IE7 Z-Index 分层问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1287439/