通常,在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>

正如您所看到的,即使在articlemain标记上都指定了20px的页边距,您也只能得到最后articlefooter之间的20px页边距.

然而,当使用flexbox时,我们在最后的articlefooter之间得到40px的边距-从文章到Main的整整20p倍的边距,以及从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页边距的表现相同?

推荐答案

利润缩水是block formatting context的一个特点.

flex formatting context%的利润率是不会崩溃的.

3. Flex Containers: the flex and inline-flex display values

Flex容器为其 内容.这与建立挡路格式化上下文相同, 只是使用弹性布局,而不是挡路布局.例如, 浮动不会侵入FLEX容器,并且FLEX 容器的边际不会随着其内容物的边际而崩溃.

Css相关问答推荐

下拉面板未与Angular中的下拉文本框垂直对齐

material 设计--Bootstrap输入域问题

截断风景中的柔子或强制在其自己的线上,如果是肖像

使用纯 CSS 访问跨度内容并设置其样式

CSS关键帧中使用transform-origin存在问题的解决方案

在 CSS 中,如何用破折号填充段落中每一行的空白区域?

如何在悬停另一个部分的元素时使元素可见?

Tailwind css break-word 在输入字段中不起作用

使元素在顶部和中心都有粘性

如何使 css 不重复同一个类?

CSS 仅过渡背景 colored颜色

带有 gatsby-plugin-image 的 bootstrap 卡 ImgOverlay 未按预期调整大小

Adobe Illustrator 删除我在 svg 图像上的类名.我怎样才能防止这种情况?

CSS 中的区域类似于 C# 区域?

Bootstrap:在列之间添加边距/填充空间

根据背景 colored颜色 反转 CSS 字体 colored颜色

为什么浏览器会为 CSS 属性创建供应商前缀?

如何在 React 应用程序中使用 CSS 模块应用全局样式?

仅针对使用的类优化 Font Awesome

输入上方带有标签的样式表