2012-09-12 238 views
1

這裏是源:下邊距和邊距不起作用

<div style="border:1px solid red; margin-bottom:10px">test block1,</div>;  
<div style="border:1px solid red; margin-top:10px">test block2</div>; 

它總是有塊1和塊2之間10px ...

我不知道什麼是錯的。請不要說「使用padding」和「改變20px

我不得不使用margin-bottommargin-top只有10px

+0

因爲我認爲這會發生,因爲如果我們不使用float和寬度或明確 –

回答

5

這就是所謂的倒塌保證金

按照W3C

在CSS中,兩個或多個框的相鄰邊緣(可能或 可能沒有兄弟姐妹)可結合成一個單一的保證金。據說這種結合的邊際 將崩潰,並且由此產生的合併邊際被稱爲摺疊邊際。

閱讀本http://reference.sitepoint.com/css/collapsingmargins

+0

非常感謝。 – user1664718

0

使用float: left;,也許還clear: both;兩個div的,這將肯定工作;)