FF浏览器嵌套div中margin-top转移问题的解决办法

来源:互联网 发布:sql server 2005 编辑:程序博客网 时间:2024/06/06 12:57

转载的。原文地址http://blog.csdn.net/xiaohongcheng/article/details/6874200 

嵌套div中margin-top转移问题的解决办法

在这两个浏览器中,有两个嵌套关系的div,如果外层div的父元素padding值为0,那么内层div的margin-top或者margin-bottom的值会“转移”给外层div。

以上边缘为例:
<div style="background-color:green; color:white;" >绿色</div>
<div style="background-color: black;height:300px; padding-top:0px;">

<div style="background-color: black;height:300px;">
<!--解决方法代码start--><!--解决方法代码end-->
   <div style="margin-top:50px;height:150px; background-color:red; color:white;">红色</div>
</div>
</div>

在Firefox和IE8中绿色和红色div之间会有50px的白色空隙,而在IE中则为黑色。

解决方法有这么几种,下面我只列出3种:

1.在注释那里插入一个非空的元素<div style="height:0px">&nbsp;</div>
2.把黑色的div加入padding-top属性代替margin-top
3.第一种最好,第二种不错,第三种不太推荐,就是在黑色div加入属性border等于1px,记得也要加入solid哦
原创粉丝点击