这是由边距塌陷引起的。如果两个元素的边距相接触,则边距会合并。对此有一个很好的解释here http://reference.sitepoint.com/css/collapsingmargins。转到名为的部分Collapsing Margins Between Parent and Child Elements
.
以下是三种不同的解决方案。
一是添加overflow: auto
到容器。这会更改 BCF(块格式化上下文)。该技术有更详细的描述here http://colinaarts.com/articles/the-magic-of-overflow-hidden/.
#container {
height: 100%;
background-color: black;
/* Add oveflow auto to container */
overflow: auto;
}
http://jsfiddle.net/bzVgV/20/ http://jsfiddle.net/bzVgV/20/
第二种是在容器上使用填充,而不是在徽标上使用边距。这将利润排除在外。
#container {
height: 100%;
background-color: black;
/* Use padding on container instead of margin on logo */
padding-top: 30px;
}
http://jsfiddle.net/bzVgV/18/ http://jsfiddle.net/bzVgV/18/
最终的解决方案是使边距不再接触。您可以通过向父级添加 1px 内边距来实现此目的。
#container {
height: 100%;
background-color: black;
/* Now margins of container and logo won't touch */
padding-top: 1px;
}
http://jsfiddle.net/bzVgV/21/ http://jsfiddle.net/bzVgV/21/