CSS Top-Margin Issue

前端 未结 2 1496
小鲜肉
小鲜肉 2021-01-16 15:24

Does anyone know why I don\'t get a top-margin in the first div based on the CSS below? This renders the same in Chrome, Safari and Firefox 4 running on Mac OSX Lion (render

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-16 15:43

    You're running into a common problem called "collapsing margins." Basically, whenever vertical margins touch (even when one element is inside another element), the margins collapse.

    In this case your margin property on the inner div is collapsing into the margin-bottom: 20px; property on your outer div. To fix this, add a little padding or a border around the containing element.

    Just tried this using your code and it works: http://jsfiddle.net/hWPyD/2/

    More info on collapsing margins: http://www.w3.org/TR/CSS2/box.html#collapsing-margins

提交回复
热议问题