1. 程式人生 > >float和margin-top

float和margin-top

如果三個兄弟元素A,B,C按順序包含在同一個元素下,A元素使用了float,則B元素不能夠設定margin-top值,但可以設定margin-bottom和margin-left/right的值;C的四個margin值都可以設定;

 所以,如果你希望A和B之間又間距,其中一個辦法是,在A和B之間新增一個div,將div的高度設定為希望兩者相距的px值(此時B不需要再新增margin) 或者不設定div高度,直接設定B的Margin也行;

這個div(help)可以不新增內容,但是一定要設定width和height,哪怕設定為0;

<div  class="father">

  <div  class="A">  </div>

 <div   class="help"></div>

<div class="B"></div>

</div>

<style>

.A {   float:left;  }

.help {   width:100%;    height:20px;   }

<style>