IE7浏览器z-index问题的解决方法

Html代码

<div id="container">
	<div id="box1">This box should be on top</div>
</div>
<div id="box2">This box should not be on top; IE however seems to
	create a new stacking context for positioned elements, even when the
	computed z-index of that element is 'auto'.
</div>

Css代码

body { margin: 0; padding: 0; }  
#container { position: relative;}  
#box1 { position: absolute; top: 100px; left: 510px; width: 200px; height: 200px; background-color: yellow;z-index:20; }  
#box2 { position: absolute; top: 50px; left: 460px; width: 200px; height: 200px; background-color: lime; z-index: 10;}  

效果:box1被box2覆盖。
原因:其实这是IE浏览器的一个BUG——在IE浏览器中,定位元素会产生一个新的stacking context,并且从z-index的值为0开始。所以我们需要在这个元素的父元素上设置一个更高的z-index值。在上述的box1中的父元素container设置一个更大的z-index就能解决这个问题。

修改后的css代码

body { margin: 0; padding: 0; }  
#container { position: relative; z-index:30;}  
#box1 { position: absolute; top: 100px; left: 510px; width: 200px; height: 200px; background-color: yellow; }  
/* box1有没有z-index都无所谓了,但必须同position(relative或absolute)使用 */
#box2 { position: absolute; top: 50px; left: 460px; width: 200px; height: 200px; background-color: lime; z-index: 20; }  
效果:box1把box2覆盖。

要想覆盖住父级的同级 ,父级的z-index就必须别的大。







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值