html - 嵌套的 flexbox 网格不扩展

标签 html css flexbox

我很难使嵌套的 flexbox 展开以填充其父单元格的大小。这两个子单元格应该增长到其父单元格高度的 1/3,但是它们占用的空间尽可能小。我想解决这个问题,但不知道如何解决。

<div class="grid">
<div class="cell blue">
    <p> This is a test grid, it is very tall <br> This is a test grid, it is very tall <br>This is a test grid, it is very tall <br>This is a test grid, it is very tall <br>This is a test grid, it is very tall <br>This is a test grid, it is very tall <br>This is a test grid, it is very tall <br>This is a test grid, it is very tall <br>This is a test grid, it is very tall <br>This is a test grid, it is very tall <br> </p>
</div>
<div class="cell green">
    <p> This is also a test grid, equally tall</p>
</div>
<div class="cell subgrid red">
    <div class="subcell">
        <p>This is a subcell, it should take up 33% if</p>
    </div>
    <div class="subcell">
        <p>this is another subcell, should be small and vertical</p>
    </div>
    <div class="subcell deepgrid">
        <div class="deepcell">
            <p>deep grid 1</p>
        </div>
        <div class="deepcell">
            <p>Deep grid 2</p>
        </div>
        <div class="deepcell">
            <p>Deep grid 3</p>
        </div>
    </div>
</div>

.grid { display: flex; justify-content: center; text-align: center; }
.cell { flex: 1; }
.sub-grid { display: flex; }
.sub-cell { flex: 1 }
.deepgrid  {display: flex; }
.deepcell { flex: 1; }
.blue {background:blue;}
.green{background:green;}
.red{background:red;}

最佳答案

问题是你用过

.sub-grid { display: flex; }
.sub-cell { flex: 1 }

但是,在 HTML 中,您使用了类 subgridsubcell

此外,您还应该添加flex-direction来将子单元格排列在一列中。

因此,你应该使用

.subgrid {
  display: flex;
  flex-direction: column;
}
.subcell {
  flex: 1;
}

.grid {
  display: flex;
  justify-content: center;
  text-align: center;
}
.cell {
  flex: 1;
}
.subgrid {
  display: flex;
  flex-direction: column;
}
.subcell {
  flex: 1
}
.deepgrid {
  display: flex;
}
.deepcell {
  flex: 1;
}
.blue {
  background: blue;
}
.green {
  background: green;
}
.red {
  background: red;
}
<div class="grid">
  <div class="cell blue">
    <p>This is a test grid, it is very tall
      <br>This is a test grid, it is very tall
      <br>This is a test grid, it is very tall
      <br>This is a test grid, it is very tall
      <br>This is a test grid, it is very tall
      <br>This is a test grid, it is very tall
      <br>This is a test grid, it is very tall
      <br>This is a test grid, it is very tall
      <br>This is a test grid, it is very tall
      <br>This is a test grid, it is very tall
      <br>
    </p>
  </div>
  <div class="cell green">
    <p>This is also a test grid, equally tall</p>
  </div>
  <div class="cell subgrid red">
    <div class="subcell">
      <p>This is a subcell, it should take up 33% if</p>
    </div>
    <div class="subcell">
      <p>this is another subcell, should be small and vertical</p>
    </div>
    <div class="subcell deepgrid">
      <div class="deepcell">
        <p>deep grid 1</p>
      </div>
      <div class="deepcell">
        <p>Deep grid 2</p>
      </div>
      <div class="deepcell">
        <p>Deep grid 3</p>
      </div>
    </div>
  </div>
</div>

关于html - 嵌套的 flexbox 网格不扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28199449/

相关文章:

css - 如何让 flex-grow 行为不贪心?

javascript - 如何防止鼠标在悬停元素外滚动?

html - 更改类流体布局 css

javascript - 如何在不被固定菜单覆盖的情况下使容器在减小窗口大小的情况下正确调整大小?

css - Angular 6 - 更改复选框样式是否被选中

javascript - 更改图像

html - 为什么在 Safari 移动设备上使用 flexbox 的图像位置会损坏?

css - flex-shrink 如何影响 padding 和 border-box?

html - Bootstrap 3 列高度为 100%

javascript - 为什么要使用 jQuery $(window).ready() 以及它与 $(window).load() 有什么不同