標籤:ack -- color 邊框 20px code back class 合并
相鄰的兩個或多個外邊距會合并成一個外邊距,當同為正的,取最大的外邊距,當有正負時,取最小的外邊距加上最大的外邊距,這裡的相鄰指的是外邊距的相鄰:
<div style="width:100px;height:100px;background-color:red;margin-bottom:20px;border:1px solid red"></div> <div style="width:100px;height:100px;background-color:yellow;margin-top:10px"></div>
<!-- 外邊距合并 --> <br><br> <div style="border:1px solid red;width:100px"> <div style="margin-top:50px;background-color:green;height:50px;width:50px"> <div style="margin-top:100px">B</div> </div> </div>
<!-- 外邊距合并 -->
<br><br> <div style="border:1px solid red;width:100px">
<div style="margin-top:50px;border:1px solid blue;background-color:green;height:50px;width:50px">
<div style="margin-top:100px">B</div>
</div>
</div><!-- 外邊距不合并,因為有邊框隔開,兩個外邊距不相鄰-->
<br><br>
<div style="border:1px solid red;width:100px">
<div style="margin-top:-50px;background-color:green;height:50px;width:50px">
<div style="margin-top:100px">B</div>
</div>
</div><!-- 有負的外邊距時,取負值最小的外邊距與最大的外邊距相加,即公用的外邊距 -->
CSS中外邊距重疊的問題