【发布时间】:2018-12-12 14:47:28
【问题描述】:
通常,在 CSS 中,当父级及其最后一个子级有边距时,边距会折叠以创建单个边距。例如
article {
margin-bottom: 20px
}
main {
background: pink;
margin-bottom: 20px;
}
footer {
background: skyblue;
}
<div id="container">
<main>
<article>
Item 1
</article>
<article>
Item 2
</article>
</main>
<footer>Footer</footer>
</div>
如您所见,即使在article 和main 标签上都指定了20px 的边距,您也只能在最后一篇文章之间获得20px 边距和页脚。
然而,当使用 flexbox 时,我们在最后一个 article 和 footer 之间得到一个 40px 边距 — 一个完整的 20px从文章到 main 的页边距,以及从 main 到 footer 的另一个 20px。
#container {
display: flex;
flex-direction: column;
}
article {
margin-bottom: 20px
}
main {
background: pink;
margin-bottom: 20px;
}
footer {
background: skyblue;
}
<div id="container">
<main>
<article>
Item 1
</article>
<article>
Item 2
</article>
</main>
<footer>Footer</footer>
</div>
有没有办法让 flexbox 边距的行为与非 flexbox 的一样?
【问题讨论】: