css盒子模型又稱框模型 (Box Model) ,包含了元素內容(content)、內邊距(padding)、邊框(border)、外邊距(margin)
幾個要素。css
圖中element元素是實際內容,也就是元素框,緊挨着元素框外部的是內邊距padding,其次是邊框(border),而後最外層是外邊距(margin),整個構成了盒子模型。一般咱們設置的背景顯示區域,就是內容、內邊距、邊框這一塊範圍。而外邊距margin是透明的,不會遮擋周邊的其餘元素。那麼:html
兩個上下方向相鄰的元素框垂直相遇時,外邊距會合並,合併後的外邊距的高度等於兩個發生合併的外邊距中較高的那個邊距值。佈局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> *{ margin:0; padding:0; } .top{ width:400px; background: #0ff; height:100px; margin:30px 30px; } .bottom{ width:400px; height:100px; margin:50px 30px; background: #ddd; } </style> </head> <body> <section class="top"> <h1>上</h1> margin-bottom:30px; </section> <section class="bottom"> <h1>下</h1> margin-top:50px; </section> </body> </html>
須要注意的是:只有普通文檔流中塊框的垂直外邊距纔會發生外邊距合併。flex
BFC其英文拼寫爲 Block Formatting Context 直譯爲「塊級格式化上下文」spa
上面邊距合併例子調整:code
<section class="top"> <h1>上</h1> margin-bottom:30px; </section> <!-- 給下面這個塊添加一個父元素,在父元素上建立bfc --> <div style="overflow:hidden"> <section class="bottom"> <h1>下</h1> margin-top:50px; </section> </div>
關於bfc的應用的案例這裏就不在一一舉出了,你們去網上找一些其餘的文章看一下。orm
box-sizing : content-box|border-box|inherit
總寬度=margin+border+padding+width;總高度=margin+border+padding+height
總寬度=margin+width; 總高度=margin+height
偷兩張圖貼起來~
htm