【发布时间】:2021-04-05 22:17:35
【问题描述】:
我了解项目在一个弹性容器中遵守与正常情况不同的边距折叠规则(即,当不在弹性包装器内时)。但是,容器本身的边距应该仍然正常运行,对吧?换句话说,为什么this CodePen中两个容器的margin不一样?
h1, h2 {
margin: 2rem 0;
}
.inline-flexed,
.flexed {
margin: 2rem 0;
align-items: center;
}
.inline-flexed {
display: inline-flex;
}
.flexed {
display: flex;
}
.left {
display: inline-block;
width: 2rem;
height: 2rem;
background-color: blue;
}
.right {
line-height: 1.5;
background-color: lightgray;
}
<h1>I have margins!</h1>
<span class="inline-flexed">
<span class="left"></span>
<span class="right">My container is inline-flex'ed. For some reason my parent's container's margins don't collapse.</span>
</span>
<h2>I also have margins!</h2>
<span class="flexed">
<span class="left"></span>
<span class="right">My container is flex'ed. My parent's container's margins collapse as you'd expect.</span>
</span>
<h2>Is this a browser bug?</h2>
【问题讨论】:
-
inline-*元素不是边距折叠规则的一部分:w3.org/TR/CSS2/box.html#collapsing-margins(只有块级元素是)--> 内联块框的边距不会折叠(不是即使是他们的流入儿童)。