Why margin-top of the top div would apply to `<body>`?

后端 未结 7 1608
长情又很酷
长情又很酷 2020-12-15 21:26

Here I posted a demo http://jsfiddle.net/LxYMv/1/.

As you can see gets margin-top:10px from the top div, and therefor

7条回答
  •  长情又很酷
    2020-12-15 21:47

    I think the box model explains this behavior: http://www.w3.org/TR/CSS2/box.html Specifically these lines: "A collapsed margin is considered adjoining to another margin if any of its component margins is adjoining to that margin." & "The top margin of an in-flow block element collapses with its first in-flow block-level child's top margin if the element has no top border, no top padding, and the child has no clearance."

    Instead of adding margin to child element, you can add same px padding to parent or apply some border to parent element and you get what you want.

提交回复
热议问题