z-index IE7,IE8,IE9不起作用?

时间:2011-08-26 12:46:48

标签: html css internet-explorer input z-index

z-index CSS属性在Internet Explorer 7,8和9上不起作用。如何解决此问题?

HTML:

<div>
    <input type="text">
    <span>label</span>
</div>


CSS:

body{background:#ddd;}
div{
    position:relative;
    width:250px;
    height:30px;
    line-height:30px;
    background:#fff;
    border:1px solid #666;
    z-index:0;
}
input{
    background:none;
    border:0;
    position:absolute;
    top:0px;
    left:0px;
    width:242px;
    height:22px;
    padding:4px;
    z-index:2;
    *line-height:30px; /* ie7 needs this */
    line-height /*\**/: 30px\9; /* ie8 needs this */
}
span{
    position:absolute;
    top:0px;
    left:4px;
    z-index:1;
}
input:focus + span{
    filter: alpha(opacity=50);
    -khtml-opacity: 0.50;
    -moz-opacity: 0.50;
    opacity: 0.50;
}

如果单击标签,则输入不会聚焦

你可以看到这里发生了什么:http://jsfiddle.net/YKFuZ/2/

1 个答案:

答案 0 :(得分:2)

我已经更新了你的代码 - http://jsfiddle.net/YKFuZ/6/

IE无法正确呈现z-index。因此,将div的位置设置为绝对值。 IE不支持:focus伪选择器。每当涉及到IE时,我都会坚持使用javascript。

编辑:输入的父级设置为相对,而输入元素本身设置为绝对。在这种情况下,Z-index将无法正确呈现。