【发布时间】:2023-04-07 08:47:02
【问题描述】:
我正在尝试了解边距何时崩溃。
我关注了这篇文章:What’s the Deal with Margin Collapse?,但仍然不明白为什么以下边距不折叠。
我违反了哪条规则?
.container1 {
background-color: red;
border-style: solid;
border-color: black;
margin-top: 10em;
}
.container2 {
background-color: green;
border-style: solid;
border-color: purple;
margin-top: 10em;
}
<div class="container1">
<div class="container2">
aaaaa
</div>
</div>
【问题讨论】:
-
因为有边框
The top margin of an in-flow block element collapses with its first in-flow block-level child's top margin if the element has no top border, no top padding, and the child has no clearance.w3.org/TR/CSS2/box.html#collapsing-margins