【问题标题】:Why do only some CSS margins collapse?为什么只有一些 CSS 边距折叠?
【发布时间】: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,因为它不包括边距。

某些&lt;p&gt; 边距是否塌陷并计入高度?为什么有些而不是其他。我尝试了许多不同的边距高度组合,但没有值显示发生了什么。

【问题讨论】:

  • 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


【解决方案1】:

我认为您没有理解“margin-collapse”的含义。

我将在以下示例中使用this simplified markup

HTML:

<div>
   <span>A</span>
   <span>B</span>
   <span>C</span>
</div>

CSS:

span {
    display: block;
    height: 100px;
    margin: 5px 0;
}

不是将每个边距显示为单独的间距,元素上的顶部和底部边距将与它们的兄弟元素(或者如果不存在上一个/下一个兄弟元素,则它们的父元素*)合并,以便它们之间的间距是最大的边距。

如果没有边距折叠,上述标记将显示为:

+div-----+
| margin |
|+span--+|
||A     ||
|+------+|
| margin |
| margin |
|+span--+|
||B     ||
|+------+|
| margin |
| margin |
|+span--+|
||C     ||
|+------+|
| margin |
+--------+

使用margin-collapse,标记显示为:

  margin
+div-----+
|+span--+|
||A     ||
|+------+|
| margin |
|+span--+|
||B     ||
|+------+|
| margin |
|+span--+|
||C     ||
|+------+|
+--------+
  margin

这对于 div 的高度意味着它包括每个元素的高度以及它们之间的边距。

在我的例子中,高度是100 + 5 + 100 + 5 + 100 = 310

在您的示例中,高度为200 + 5 + 300 + 5 + 400 = 910


* 还有一些额外的复杂性涉及到填充、定位和浮动,即described by the specification

【讨论】:

  • 哇,我现在明白了!我认为折叠意味着产生的单个边距与前两个边距的高度相同。它实际上只是摆脱了两个边距中较小的一个!谢谢!
  • 这是页边距折叠的绝佳视觉表现。干得好。
【解决方案2】:

边距被添加到盒子模型中,因此在您的情况下高度为 elem 高度 + 边距。然而,当相邻的元素有边距时,边距会崩溃,最大的胜利与添加的两个。好文章在这里解释它http://www.960development.com/understand-css-margins-collapsing/

【讨论】:

【解决方案3】:

scrollHeight 返回

the distance between the top and bottom edges of the object's content

即使您的示例中的每个边距 折叠,第二个p 仍然有上下 5px 的边距,这会计入其边缘之间的总距离。

即 900 像素 + 5 像素 + 5 像素 = 910 像素。

为了更简单,check this example:

<div id="seconddiv" style="margin-top:10px; margin-bottom:10px;">

    <!--
    Top edge is first p's top position (without top margin)
    -->

    <p style="height:100px; margin-top:5px; margin-bottom:5px;">
    First p adds 105px (100px height + 5px bottom margin collapsed) = 105px
    </p>
    <p style="height:100px; margin-top:5px; margin-bottom:5px;">
    Second p adds 105px (100px height + 5px bottom margin collapsed) = 210px
    </p>
    <p style="height:100px; margin-top:5px; margin-bottom:5px;">
    Third p adds 105px (100px height + 5px bottom margin collapsed) = 315px
    </p>
    <p style="height:100px; margin-top:5px; margin-bottom:5px;">
    Last p adds 100px (100px height) = 415px
    </p>

    <!--
    We're at the bottom edge. Margins are excluded, the total height is 415px
    -->

</div>​

【讨论】:

    猜你喜欢
    • 2011-03-05
    • 2010-11-25
    • 2010-09-11
    • 2011-02-09
    • 2017-07-27
    • 2017-07-09
    • 1970-01-01
    相关资源
    最近更新 更多