【发布时间】:2012-09-18 16:50:54
【问题描述】:
我一直在进行一些测试,以找出为什么一些 CSS 边距会塌陷而有些则不会。我有以下测试代码:
<div id="seconddiv" style="margin-top:10px; margin-bottom:10px;">
<p style="height:200px; margin-top:5px; margin-bottom:5px;">This is the first paragraph in the second div!This paragraph is 200px tall.</p>
<p style="height:300px; margin-top:5px; margin-bottom:5px;">This is the second paragraph in the second div!This paragraph is 300 px tall.</p>
<p style="height:400px; margin-top:5px; margin-bottom:5px;">This is the third paragraph in the second div!This paragraph is 400px tall.</p>
</div>
我正在尝试准确获取 div 的高度,但 scrollHeight 返回“910px”。这是为什么?我期望“900px”作为scrollHeight,因为它不包括边距。
某些<p> 边距是否塌陷并计入高度?为什么有些而不是其他。我尝试了许多不同的边距高度组合,但没有值显示发生了什么。
【问题讨论】:
-
Nitpick:scrollHeight 不是任何通用标准的一部分,它是微软特定的东西,一些浏览器碰巧复制了它。因此,不同的浏览器完全有可能实现不同的方式。
-
"为什么只有部分 HTML 边距折叠?" because the spec defines it that way.
-
第二个div的高度是
200 + 5 + 300 + 5 + 400。内部p元素之间的边距是div高度的一部分 -
@zzzzBov 是添加到 div 高度的段落的上边距还是下边距?如果scrollHeight 是
200 + 5(margin-bottom) + 5(margin-top) + 300 + 5(margin-bottom) + 5(margin-top) + 400,我可以理解。
标签: html css android-webview margins