FF下margin-top不起作用分析

常常可以碰到这样一个问题,就是外层DIV设置了高与宽,内层DIV如果设置maring-top不起作用(FIREFOX和IE6中测试),原因大致是内层div没有获得layout。如下面的代码:

<style>

.aDiv {background:red; width:300px; height:300px; }
.bDiv {background:green; position:relative; width:100px; height:20px; margin-top:10px;}
.cDiv {background:black; position:relative; width:100px; height:20px;}
</style>

<div>
<div></div>
<div></div>
</div>

测试发现,bDiv的margin-top不起作用,仍是0px的显示效果。如果在firefox中用firebug查看,可以看到margin-top是有值的,为10px;解决问题如下:

1、把margin-top改成padding-top,不过,前提是内层的Div没有设置边框
2、给外层的Div加padding-top
3、给外层DIV加:

A、float: left或right

B、position: absolute

C、display: inline-block或table-cell或其他 table 类型

D、overflow: hidden或auto

比如,可以更改上述代码如下:

<style>

.a {background:red; width:300px; height:300px; float:left; }
.b {background:green; position:relative; width:100px; height:20px; margin:10px;}
.c {background:black; position:relative; width:100px; height:20px;}

.clear{ clear:both;}
</style>

<div>
<div></div>
<div></div>
</div>

<div></div>

注意:后面要加一个清除浮动。

Leave a comment

Your comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word