0
最近我開始遇到這個問題,在刪除邊框後div會被調整大小。當邊框被刪除時div的大小調整
聲明本的jsfiddle:http://jsfiddle.net/rALvd/1/
如果去掉黑色邊框的紅色格不再在頂部和底部的50像素的保證金,但出現在底部有很多更大的空間。
當您添加邊框時,紅色div會返回到具有正確的邊距。
這裏是代碼:
<div id="main_contact">
<div id="contact_contain">
<div id="contact_form">
</div>
</div>
</div>
#main_contact {
width: 100%;
height: 450px;
border: 1px solid black;
}
#contact_contain {
position: relative;
width: 100%;
height: 350px;
margin: 0 auto;
background: url(homepage/contact.png) center center no-repeat;
margin-top: 50px;
border: 1px solid red;
}
我一直在使用border:1px solid transparent。我不知道這是否是最佳做法。 – TheNameHobbs
當然,沒關係。 –
此行爲被稱爲[margin collapsing](http://reference.sitepoint.com/css/collapsingmargins)。還有其他一些方法可以防止它,例如通過將display:table或overflow:auto設置爲外部div(這使得它建立[新的塊格式化上下文](http://www.w3.org/TR/CSS21/visuren.html#block-formatting ))。您也可以使用外部div的填充而不是內部填充的邊距永不折疊。 –