css - Flexbox 子项不尊重具有 flex-direction 列的父项的高度

标签 css flexbox

一般的想法是让 2 行等高,第一行包含 2 等宽的列以实现完整的页面布局。我遇到的问题是,当其中一个单元格充满子元素时,父行的高度会扩展并超过兄弟行,而此时高度应该相等。

body {
  min-height: 100vh;
  height: auto;
  display: flex;
  flex-direction: column;
}
.content {
  flex: 1;
  display: flex;
  flex-direction: column;
}
.row {
  border: dashed 1px;
  flex: 1;
}
.row1 {
  display: flex;
}
.cell {
  flex: 1;
  padding: 8px;
  border: dashed 1px black;
  margin: 4px;
  display: flex;
  flex-direction: column;
}
.title {
  border: solid 1px;
  padding: 8px;
  font-size: 24px;
}
.things {
  flex: 1;
  margin: 8px 0 0;
  padding: 0;
  overflow-y: scroll;
}
.things li {
  list-style-type: none;
  padding: 4px;
  border: solid 1px;
  margin: 8px 8px 8px;
}
<div class="content">
  <div class="row row1">
    <div class="cell">
      <div class="title">Cell 1</div>
      <ul class="things">
        <li>thing 1</li>
        <li>thing 2</li>
        <li>thing 3</li>
        <li>thing 4</li>
        <li>thign 5</li>
        <li>thing 1</li>
        <li>thing 2</li>
        <li>thing 3</li>
        <li>thing 4</li>
        <li>thign 5</li>
      </ul>
    </div>
    <div class="cell">
      <div class="title">Cell 2</div>
    </div>
  </div>
  <div class="row row2">Row 2</div>
</div>

最佳答案

由于flex是一个简写属性,flex: 1表示

flex-grow: 1;
flex-shrink: 1;
flex-basis: 0%;

但出于某种原因,0% 似乎让 Chrome 感到困惑。所以手动添加flex-basis: 0:

.row { flex-basis: 0; }

并且由于 Firefox 实现了新的 auto 作为 min-height 的初始值,它需要

.row { min-height: 0; }

所以最后的代码是

.row {
  flex: 1;
  flex-basis: 0;
  min-height: 0;
}

/* Styles go here */
body {
  min-height: 100vh;
  height: auto;
  display: flex;
  flex-direction: column;
}
header {
  background: #000;
  color: #fff;
  display: flex;
  justify-content: space-between;
}
a {
  color: #fff;
  padding: 4px;
  text-decoration: none;
}
a:hover {
  background: #fff;
  color: #000;
}
.content {
  flex: 1;
  display: flex;
  flex-direction: column;
}
.row {
  border: dashed 1px;
  flex: 1;
  flex-basis: 0;
  min-height: 0;
}
.row1 {
  display: flex;
}
.cell {
  flex: 1;
  padding: 8px;
  border: dashed 1px black;
  margin: 4px;
  display: flex;
  flex-direction: column;
}
.title {
  border: solid 1px;
  padding: 8px;
  font-size: 24px;
}
.things {
  flex: 1;
  margin: 8px 0 0;
  padding: 0;
  overflow-y: scroll;
}
.things li {
  list-style-type: none;
  padding: 4px;
  border: solid 1px;
  margin: 8px 8px 8px;
}
<header>
  <a href="#">Home</a>
  <a href="#">Acct</a>
</header>
<div class="content">
  <div class="row row1">
    <div class="cell">
      <div class="title">Cell 1</div>
      <ul class="things">
        <li>thing 1</li>
        <li>thing 2</li>
        <li>thing 3</li>
        <li>thing 4</li>
        <li>thign 5</li>
      </ul>
    </div>
    <div class="cell">
      <div class="title">Cell 2</div>
    </div>
  </div>
  <div class="row row2">Row 2</div>
</div>

关于css - Flexbox 子项不尊重具有 flex-direction 列的父项的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30861247/

相关文章:

css - 在每个浏览器中将 div 置于屏幕中央

css - PhoneGap Build 应用程序中未显示图像

html - 是否可以将第二个 child 的宽度设置为行列表中最大的一个?

html - 如何使用省略号和 Angular-FlexLayout 缩短文本?

html - 使用 FlexBox 创建响应式网格

html - img 位置固定和溢出隐藏问题

html - 如果其中的文本太长,则垂直居中 block

html - 如何使页面扩展以适合屏幕?

html - 在 IE9 中 flex 属性不起作用

html - CSS,Firefox 和 IE 中不同的展开和折叠行为