CSS margin合并

來源:互聯網
上載者:User

標籤:https   top   min   ack   margin   span   gre   org   eve   

1. 外邊距合并

頂部外邊距和底部外邊距有時被組合(摺疊)為單個外邊距,其大小是組合到其中的最大外邊距

2. 發生外邊距合并的三種基本情況
  • 相鄰的兄弟姐妹元素
<div id="margin_parent"><div><b>父margin-top: 80px,子margin-top: 50px,則最後margin-top為80px</b></div></div>

CSS

#margin_parent{    width: 200px;    height: 200px;    background-color: green;    margin-top:80px;}#margin_parent div{    width: 200px;    height: 200px;    background-color: yellow;    margin-top:50px;    opacity: 0.5;}
  • 塊級父元素與其第一個/最後一個子項目

margin-top:塊級父元素和其第一個子項目會發生上外邊距合并

margin-bottom:塊級父元素與它的最後一個子項目會發生下邊距合并,要求:父元素沒有border、padding、inline content、height、min-height、max-height等

<div id="margin_parent"><div><b>父margin-top: 80px,子margin-top: 50px,則最後margin-top為80px</b></div></div>

CSS

#margin_parent{    width: 200px;    height: 200px;    background-color: green;    margin-top:80px;}#margin_parent div{    width: 200px;    height: 200px;    background-color: yellow;    margin-top:50px;    opacity: 0.5;}
  • 空塊元素

如果存在一個空的區塊層級元素,其border、padding、inline content、height、min-height都不存在。那麼此時它的上下邊距中間將沒有任何阻隔,此時它的上下外邊距將會合并

<p style="margin-bottom: 0px;background-color:green;">這個段落的和下面段落的距離將為50px</p><div style="margin-top: 50px; margin-bottom: 50px;"></div><p style="margin-top: 0px;background-color:green;">中間div的設定margin-top: 50px; margin-bottom: 50px;<br/>
兩個會摺疊成一個50px</p>
 
參考

https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_Box_Model/Mastering_margin_collapsing

CSS margin合并

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.