html - Chrome 不会根据 child 的内容展开 flex parent

标签 html css google-chrome flexbox

<分区>

我需要并排显示多个框。每个盒子都有数量可变的子项。父项应根据子项占用的空间进行扩展。元素具有固定宽度很重要。我正在使用 flexbox 进行布局。它应该看起来像这样:

enter image description here

我写了一个Plunkr来说明。

问题:当子项占用更多空间时,Chrome 不会展开父项。子项显示在父项的边界之外并出现在相邻的父项中,导致出现这种情况:

enter image description here

具有讽刺意味的是,如果您在 IE 11 或 Edge 中打开 Plunkr,它工作正常。

我做错了什么?

这是一个代码片段:

body {
  display: flex;
  overflow: scroll;
}

.parent {
  border: 1px solid grey;
  padding: 0 20px;
  display: flex;
  flex-direction: column;
}

.items {
  display: flex;
}

.items span {
  flex-basis: 25px;
  flex-shrink: 0;
}
<div class="parent">
  <h4>Parent 1</h4>
  <div class="items">
    <span>i1</span>
    <span>i2</span>
    <span>i3</span>
    <span>i4</span>
    <span>i5</span>
  </div>
</div>
<div class="parent">
  <h4>Parent 2</h4>
  <div class="items">
    <span>i1</span>
    <span>i2</span>
    <span>i3</span>
    <span>i4</span>
    <span>i5</span>
  </div>
</div>
<div class="parent">
  <h4>Parent 3</h4>
  <div class="items">
    <span>i1</span>
    <span>i2</span>
    <span>i3</span>
    <span>i4</span>
    <span>i5</span>
  </div>
</div>
<div class="parent">
  <h4>Parent 4</h4>
  <div class="items">
    <span>i1</span>
    <span>i2</span>
    <span>i3</span>
    <span>i4</span>
    <span>i5</span>
  </div>
</div>
<div class="parent">
  <h4>Parent 5</h4>
  <div class="items">
    <span>i1</span>
    <span>i2</span>
    <span>i3</span>
    <span>i4</span>
    <span>i5</span>
  </div>
</div>
<div class="parent">
  <h4>Parent 6</h4>
  <div class="items">
    <span>i1</span>
    <span>i2</span>
    <span>i3</span>
    <span>i4</span>
    <span>i5</span>
  </div>
</div>

最佳答案

这似乎是 Chrome 中的错误。

在这段代码中:

.items span {
  flex-basis: 25px;
  flex-shrink: 0;
}

... Chrome 不遵守固定的 25px 宽度。或者,至少,容器没有识别出空间。看起来容器的大小是在所有元素计算都被考虑在内之前确定的。

简单的解决方法是使用 width 而不是 flex-basis

.items span {
    flex-shrink: 0;
    width: 25px;
 }

revised demo


更新

错误报告

Flex-basis is being ignored when sizing nested flex containers.

"Yeah this is kind of a sucky consequence of a bug we have, perhaps combined with an unfortunate spec consequence..."

"When the outer flexbox finds the desired size of the inner flexbox, it asks for its max-content size (see LayoutFlexibleBox::computeInnerFlexBaseSizeForChild). But here's where the bug comes in, the max-content size does not take flex-basis into account, only width properties/actual content. That's why adding an explicit width fixes the bug."

关于html - Chrome 不会根据 child 的内容展开 flex parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45482350/

相关文章:

javascript - Node vs Chrome,instanceof 的实现,多个文件

javascript - 通过 Ext.data.connection Sencha Touch 和 Phonegap 在互联网可用时加载谷歌地图 API

php - 保存到数据库时如何忽略空输入

asp.net - IE 似乎没有缓存我的图像

css - 如何使用@media 查询在 rails 中添加照片作为 div 的背景?

javascript - 在 Service Worker 中访问 Chrome API

javascript - 与jquery相关的输入值?

php - 使用 session 变量从 2 个表中进行选择

html - 在 ruby​​ on rails if 语句中使用 html 标签

html - 带有可折叠导航的 Bootstrap - 我得到的与文档不匹配