一.margin与百分比问题:布局
二.margin重叠问题:spa
三.margin重叠三种状况的具体展示:设计
(2)没有border-top设置 设置border-top;code
(3)没有padding-top值 设置padding-top;blog
(4)父元素和第一个子元素之间没有inline元素分隔 <div class="father"> </div>图片
margin-bottom:前四个与margin-top一致it
(5)没有height/min-height/max-height限制 设置height元素;io
2.空block元素margin重叠:例:
class
.father(background:#f0f3f9;overflow:hidden;)
.son(margin:1em 0;)
<div class="father"> <div class="son"></div> <!--高度只有1em,而非2em;--> </div>
空block元素margin重叠的条件: (1)元素没有border设置容器
(2)元素没有padding值
(3)里面没有inline元素
(4)没有height或min-height
四.margin重叠的计算:(1)正正取大值(2)正负值相加(3)负负最负值
五.善用margin重叠:
例表单列表可用
.list{ margin-top:15px; margin-bottom:15px; }
更具健壮性,最后一个元素移除或位置调换均不破坏原来的布局;
六.margin:auto;问题:
问题1:为何图片margin: 0 auto;不水平居中:
img{width:200px;margin:auto 0;}
由于此时图片是inline水平,就算没有width,其也不会占据整个容器;
能够更改成:
img{display:block;width:200px;margin:auto 0;}
问题2:容器定高,元素定高,margin:auto 0;没法垂直居中
例
.father{height:200px;background:#f0f3f9;}
.son{height:100px;width:500px;margin:auto 0;}
可改成
.father{height:200px;background:#f0f3f9;writing-mode:vertical-lr;} .son{height:100px;width:500px;margin:auto 0;}
也能够改成:
.father{height:200px;background:#f0f3f9;position:relative;} .son{position:absolute;top:0;left:0;bottom:0;right:0;height:100px;width:500px;margin:auto 0;}