<div class="wrapper">
<div class="div1"></div>
<div class="div2">test</div>
</div>
.wrapper {
height: 500px;
}
.div1 {
float: left;
width: 200px;
height: 200px;
background: red;
}
.div2 {
width: 200px;
height: 200px;
background: dimgray;
}
代码如上所示,div1向左浮动,此时div2会移动到原先div1的位置,但是被div1遮盖,而内容不会和div1重叠,所以会显示在下面。
此时将div2的margin-top设置为100px, div2会向下移动100px,但是同时浮动的div1也会向下移动100px。这是为什么?浮动元素顶端不是应该尽可能的高吗?应该在包含框的上边缘位置吧?
这里实质上是wrapper的margin-top为 100px,你给wrapper加个overflow:hidden看看效果