为什么父元素不包含边距?
当具有边距的元素包含在另一个元素中时,父元素不会始终包裹/包含该边距。
很多事情都会导致父级包含子级的边距:
border:solid;
position:absolute;
display: inline-block;
溢出:自动;
(这只是来自一点测试,毫无疑问还有更多。)
我认为这与折叠边距有关,但是:
- W3C 规范页面没有对此类行为的描述。
- 这里没有重叠的边距。
- 所有浏览器在这个问题上的行为似乎都是一致的。
- 该行为受到与边距无关的触发器的影响。
默认为 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:
- The W3C spec page has no description of such behavior.
- There are no overlapping margins here.
- Behavior of all browsers seems to be consistent on this issue.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 CSS 的工作方式根据 W3C:
更具体地说,您的顶部 div 的情况:
我能做的最好的事情就是向您指出 sitepoint 上的“折叠边距”< /a> (汤米·奥尔森和保罗·奥布莱恩)。他们用示例进行非常详细的解释,向您展示您在问题示例代码中演示的行为。
This is how CSS works according to W3C:
More specific to your case of the top div:
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.