为什么父元素不包含边距?

发布于 2024-08-19 20:15:00 字数 2010 浏览 4 评论 0原文

当具有边距的元素包含在另一个元素中时,父元素不会始终包裹/包含该边距。

很多事情都会导致父级包含子级的边距:

  • border:solid;
  • position:absolute;
  • display: inline-block;
  • 溢出:自动;

(这只是来自一点测试,毫无疑问还有更多。)

我认为这与折叠边距有关,但是:

  1. W3C 规范页面没有对此类行为的描述。
  2. 这里没有重叠的边距。
  3. 所有浏览器在这个问题上的行为似乎都是一致的。
  4. 该行为受到与边距无关的触发器的影响。

默认为 overflow: auto 的元素应包含与溢出设置为 auto 的元素不同的材料,其逻辑是什么?

为什么除了常规 div 的默认行为之外的所有内容都应该假设边距由父级包含 - 以及为什么常规默认值不应该包含边距?


编辑:最终的答案是 W3C 确实指定了这种行为,但是:

  • 这些规范实际上没有任何意义。
  • 规格混合,没有任何解释
    • “自由边距”(父级不包含触及父级顶部或底部的边距)和
    • “折叠边距”(相邻边距允许重叠)。

演示:

body {
  margin: 0;
}

div.block {
  background-color: skyblue;
}
div.inline-block {
  display: inline-block;
  background-color: lawngreen;
}
div.position-absolute {
  background-color: rgba(255,255,0,.7);
  position: absolute;
  bottom: 0;
  right: 0;
}
div.overflow-auto {
  background-color: hotpink;
  overflow: auto;
}
div.border {
  background-color: aquamarine;
  border: solid;
}

h2 {
  margin: 80px;
  width: 250px;
  border: solid;
}
<div class="block">
  <h2>Is the margin contained (block)?</h2>
</div>
<div class="inline-block">
  <h2>Is the margin contained (inline-block)?</h2>
</div>
<div class="position-absolute">
  <h2>Is the margin contained (position-absolute)?</h2>
</div>
<div class="overflow-auto">
  <h2>Is the margin contained (overflow-auto)?</h2>
</div>
<div class="border">
  <h2>Is the margin contained (border)?</h2>
</div>

When an element with a margin is contained within another element, the parent does not consistently wrap/contain that margin.

Many things will cause the parent to contain the child's margin:

  • border: solid;
  • position: absolute;
  • display: inline-block;
  • overflow: auto;

(And this is just from a little testing, no doubt there are more.)

I would assume this has to do with collapsing margins, but:

  1. The W3C spec page has no description of such behavior.
  2. There are no overlapping margins here.
  3. Behavior of all browsers seems to be consistent on this issue.
  4. The behavior is affected by triggers that are not related to the margins.

What is the logic by which an element which defaults to overflow: auto should contain different material than the one where the overflow is set to auto?

Why should everything but the default behavior of a regular div assume that the margin is contained by the parent – and why should the regular default not include the margin?


EDIT: The final answer is that the W3C really does specify this behavior, but that:

  • The specs do not really make any sense.
  • The specs mix, without any word of explanation:
    • 'free margins' (margins that would touch the top or bottom of their parent are not contained by the parent) and
    • 'collapsed margins' (adjacent margins are allowed to overlap).

Demo:

body {
  margin: 0;
}

div.block {
  background-color: skyblue;
}
div.inline-block {
  display: inline-block;
  background-color: lawngreen;
}
div.position-absolute {
  background-color: rgba(255,255,0,.7);
  position: absolute;
  bottom: 0;
  right: 0;
}
div.overflow-auto {
  background-color: hotpink;
  overflow: auto;
}
div.border {
  background-color: aquamarine;
  border: solid;
}

h2 {
  margin: 80px;
  width: 250px;
  border: solid;
}
<div class="block">
  <h2>Is the margin contained (block)?</h2>
</div>
<div class="inline-block">
  <h2>Is the margin contained (inline-block)?</h2>
</div>
<div class="position-absolute">
  <h2>Is the margin contained (position-absolute)?</h2>
</div>
<div class="overflow-auto">
  <h2>Is the margin contained (overflow-auto)?</h2>
</div>
<div class="border">
  <h2>Is the margin contained (border)?</h2>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无妨# 2024-08-26 20:15:00

这是 CSS 的工作方式根据 W3C

在本规范中,“折叠边距”一词是指两个或多个框(可以彼此相邻或嵌套)的相邻边距(没有非空内容、填充或边框区域或间隙将它们分开)组合在一起形成单一边距。

更具体地说,您的顶部 div 的情况:

如果框的顶部和底部边距相邻,则边距可能会折叠穿过它。在这种情况下,元素的位置取决于它与其他边距被折叠的元素的关系。

  • 如果元素的边距与其父级的上边距折叠在一起,则框的上边框边缘将定义为与父级的相同。
  • 否则,要么该元素的父元素不参与边距折叠,要么只涉及父元素的下边距。元素顶部边框边缘的位置与元素具有非零底部边框时的位置相同。

我能做的最好的事情就是向您指出 sitepoint 上的“折叠边距”< /a> (汤米·奥尔森和保罗·奥布莱恩)。他们用示例进行非常详细的解释,向您展示您在问题示例代码中演示的行为。

This is how CSS works according to W3C:

In this specification, the expression collapsing margins means that adjoining margins (no non-empty content, padding, or border areas, or clearance separate them) of two or more boxes (which may be next to one another or nested) combine to form a single margin.

More specific to your case of the top div:

If the top and bottom margins of a box are adjoining, then it is possible for margins to collapse through it. In this case, the position of the element depends on its relationship with the other elements whose margins are being collapsed.

  • If the element's margins are collapsed with its parent's top margin, the top border edge of the box is defined to be the same as the parent's.
  • Otherwise, either the element's parent is not taking part in the margin collapsing, or only the parent's bottom margin is involved. The position of the element's top border edge is the same as it would have been if the element had a non-zero bottom border.

The best thing I can do is point you to on "Collapsing Margins" on sitepoint (by Tommy Olsson and Paul O’Brien). They do a very detailed explanation with examples showing you exactly the behaviors you demoed in the question example code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文