html - 在 flex box 中使用时,网格不占据其 div 的全部高度

标签 html css flexbox css-grid

我在 flexbox 列中有一个 grid (.genTempGrid)(反过来,它也在 flexbox 列中)。奇怪的是,创建的 grid 并未占据其 div 的全部高度,相反,其高度受限于其内容。

我的理解是,无论如何,grid 的高度 == 它所在的 div 的高度。如果我错了,请纠正我。

.d-flex-column-last-child-flex-grow-1 {
      flex-grow: 1;
      display: flex;
      flex-direction: column;
      /*height: 100%;*/
    }
    
.child-expand {
      flex-grow: 1;
    }

.genTempGrid {
            display: grid;
            height: 100%;
            grid-template-columns: 20% 60% 20%;
            grid-template-rows: 1fr;
            background-color: #f7f7f7;
        }
        
.border1 {
      border: 1px solid red;
    }
<div class="d-flex-column-last-child-flex-grow-1" style="height: 50vh">
  <div class=" d-flex-column-last-child-flex-grow-1 child-expand">
    <div class="pl-3 genTempGrid pt-2 child-expand border1">
      <div class="border1">hi</div>
      <div>hi</div>
      <div>hi</div>
    </div>
  </div>
</div>

enter image description here

编辑:我不是在寻找解决方法。我更有兴趣了解当前行为背后的原因。

最佳答案

这是由于这一行

.d-flex-column-last-child-flex-grow-1 {
      flex-grow: 1;
    }

有关完整说明,请参阅:Why don't flex items shrink past content size?Fitting child into parent

这可以通过使用简写 flex:1 或应用 min-(dimension): 0 来解决,这会强制重新计算整体。

.d-flex-column-last-child-flex-grow-1 {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.child-expand {
  flex-grow: 1;
}

.genTempGrid {
  display: grid;
  height: 100%;
  grid-template-columns: 20% 60% 20%;
  grid-template-rows: 1fr;
  background-color: #f7f7f7;
}

.border1 {
  border: 1px solid red;
}
<div class="d-flex-column-last-child-flex-grow-1" style="height: 50vh">
  <div class=" d-flex-column-last-child-flex-grow-1 child-expand">
    <div class="pl-3 genTempGrid pt-2 child-expand border1">
      <div class="border1">hi</div>
      <div>hi</div>
      <div>hi</div>
    </div>
  </div>
</div>

关于html - 在 flex box 中使用时,网格不占据其 div 的全部高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52851547/

相关文章:

css - 选择第二级的第一个 child

html - ASP Net Core Angular - 使用 Bootstrap 4 而不是 3(SideMenu)的默认元素

html - Flexbox 断点对两列没有响应

html - 是否可以在显示背景的同时在 flexbox 之间留出间隙?

PHP/HTML : If url is domain. co.uk/?debug 然后

javascript - JQUERY如何获取div ID

javascript - 当项目被选择或输入与列表中的项目匹配时,HTML5 数据列表的 jQuery 事件

html - 如何使用 CSS 居中对齐内容?

php - WordPress,禁用部分 Roots 主题(如 Bootstrap)

html - 如何在 flexbox 行之间添加一条线?