CSS Top-Margin Issue

前端 未结 2 1497
小鲜肉
小鲜肉 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

    0 讨论(0)
  • 2021-01-16 15:50

    Its because there is a default margin and padding on html tags. Use a reset like this one to remove all of the browser defaults. (I highly recommend doing this)

    0 讨论(0)
提交回复
热议问题