【发布时间】:2012-03-15 17:52:28
【问题描述】:
首先是html标记,
<p>Bananas For BreakFast</p>
<h1>What the what!?</h1>
...还有css,
body, p, h1, h2, h3, h4{
margin: 0; padding: 0;
}
body{
padding: 30px;
}
/*remove the comment to trigger the effect
body{
padding: 0;
}
*/
p{
float: left;
background-color: red; /*just to aid visualization*/
}
h1{
clear: both;
margin-top: 200px;
background-color: grey; /*just to aid visualization*/
}
您可以在下面的 tinkerbin 页面上看到它的实际效果。
那么,我的问题是什么?
简单:当 body 元素上有填充时,清除元素上的 margin-top 应用。当'填充:0;'同一个清除元素上的 'margin-top' 停止工作。
为什么?
被清除的元素不参与其上方元素的边距折叠,因此这不能是边距顶部与正文的边距折叠。即使是这样,也应该在 body 元素上应用边距,将整个元素向下推——但事实并非如此。
【问题讨论】:
-
那么你想做什么?仅对 body 应用 padding 并将 margin-top 仅应用于 h1 元素?
-
没有。这只是我遇到的一些奇怪的行为,并且想知道对此的解释。我不是想实现布局 - 我只是想了解行为。
-
body { float: left } ha - 不确定可能对所有内容产生的其他负面影响,但似乎可以解决问题。
-
在这里,我以为我是世界上唯一一个使用“What the what!?”这个表达方式的人。 :)
-
我很想知道两件事:1)为什么这个问题至少被否决了两次? 2)@Aaron Brewer:为什么你认为浮动元素不是块元素?那么它们是什么?