当前位置: 首页 > 图文教程 > 网页制作 > CSS样式表 > margin 负值引起的层级(z-index)问题

CSS样式表
CSS3教程(10):CSS3 HSL声明设置颜色
CSS3教程:新增加的结构伪类
网页布局绝对定位(position)轻松简单
掌握盒模型轻松DIV CSS网页布局
实例方式学CSS text-align怎么用
网页重构时在IE6中遇到png兼容性
CSS3教程:边框属性border的极致应用
CSS网页布局使用表格可以吗?
最新版本的CSS选择器浏览器支持情况
有序列表和段落设计华丽的网页列表数字
CSS教程:通过实例学习和理解CSS盒模型
CSS网页布局的核心内容:CSS盒模型
CSS3伪类选择器:nth-child()
CSS教程:inline-block在各浏览器的显示
CSS实现网页分栏布局的方法:绝对定位和浮动
CSS实例:用CSS制作网页像素画
CSS教程:三列固定网页布局实例
无hack无js全兼容text-overflow-ellipsis效果
CSS解决方案:IE6中遇到png兼容性
CSS学习之类目之间竖线的练习实例

CSS样式表 中的 margin 负值引起的层级(z-index)问题


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 306 ::
收藏到网摘: n/a

先来看这么一段代码:

代码如下:

<div style="height:100px;width:200px; border: solid 1px black; ">
<div style="background-color:Red;margin-top: -5px " mce_style="background-color:Red;margin-top: -5px ">
<a href="http://www.ruanchen.com/" mce_href="http://www.ruanchen.com/">软晨学习网</a></div>
</div>

IE6和IE7下,内层的容器被外层覆盖,如图所示:

在IE8和ff下,外层的容器被内层覆盖,如图所示:

真是疯狂啊,如果要达到IE8的外层的容器被内层覆盖的效果,ie7可以通过触发内层的layout解决,,但是IE6却不行,只能在内层使用position:relative来解决问题,当然position:relative也解决ie7的问题,因为position:relative本身就能触发layout。
看代码:

代码如下:

<div style="height:100px;width:200px; border: solid 1px black; ">
<div style="background-color:Red;margin-top: -5px;position:relative" mce_style="background-color:Red;margin-top: -5px;position:relative">
<a href="http://www.ruanchen.com/" mce_href="http://www.ruanchen.com/">软晨学习网</a> </div>
</div>

当然要IE8、FF达到IE6、IE7的效果就只要在外层加overflow:hidden 就可以了,看代码

代码如下:

<div style="height:100px;width:200px; border: solid 1px black; overflow:hidden ">
<div style="background-color:Red;margin-top: -5px;" mce_style="background-color:Red;margin-top: -5px;">
<a href="http://www.ruanchen.com/" mce_href="http://www.ruanchen.com/">软晨学习网</a> </div>
</div>