【问题标题】:Flexbox row inside flexbox columnFlexbox 列内的 Flexbox 行
【发布时间】:2016-09-18 19:59:29
【问题描述】:

我有这样的布局:

<div class="flexbox">
  <div class="header"></div>
  <div class="main"></div>
  <div class="footer"></div>
</div>

和它的css:

html,
body {
  height: 100%;
}
.flexbox {
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  justify-content: flex-start;
  align-content: stretch;
  align-items: flex-start;
  height: 100%;
  width: 100%;
}
.flexbox .main {
  order: 0;
  flex: 1 1 auto;
  align-self: stretch;
}
.flexbox .header,
.flexbox .footer {
  order: 0;
  flex: 0 1 auto;
  align-self: stretch;
}

在上面的.main 列中,height 被拉伸了 100%,这一切都很完美,现在在 .main 中添加.flexbox-row

<div class="flexbox">
  <div class="header"></div>
  <div class="main">
    <div class="flexbox-row">
      <div class="first"></div>
      <div class="second"></div>
      <div class="third"></div>
    </div>
  </div>
  <div class="footer"></div>
</div>

和css:

.flexbox-row {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: flex-start;
  align-content: stretch;
  align-items: flex-start;
}
.flexbox-row .first,
.flexbox-row .second,
.flexbox-row .third {
  order: 0;
  flex: 0 1 auto;
  align-self: stretch;
}
.flexbox-row .second {
  order: 0;
  flex: 1 1 auto;
  align-self: stretch;
}

但由于某种原因,.main 列的第二个不再在 height 中拉伸 100%。

这里是jsbin演示

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    height:100% 添加到.flexbox-row

    我已经简化了你的规则,那里有不必要的属性。

    html,
    body {
      height: 100%;
      margin: 0
    }
    .flexbox {
      display: flex;
      flex-direction: column;
      flex-wrap: nowrap;
      height: 100%;
      width: 100%;
    }
    .flexbox .main {
      flex: 1;
      background: red
    }
    .flexbox .header,
    .flexbox .footer {
      background: green;
      padding:1em
    }
    .flexbox-row {
      display: flex;
      height: 100%;
    }
    .flexbox-row .first,
    .flexbox-row .second,
    .flexbox-row .third {
      background: #C9E1F4;
      padding: 1em;
    }
    .flexbox-row .second {
      flex: 1;
      background: #A8EDAC
    }
    <div class="flexbox">
      <div class="header">Header</div>
      <div class="main">
        <div class="flexbox-row">
          <div class="first">First</div>
          <div class="second">Main</div>
          <div class="third">Third</div>
        </div>
      </div>
      <div class="footer">Footer</div>
    </div>

    更新

    在 Chrome 和 Safari 中,(非 flex)子项的高度不是 以百分比确认。然而 Firefox 和 IE 识别和扩展 孩子的身高百分比。

    Source

    因此,解决方法是将.position:relative 添加到.mainposition:absolutewidth/height:100%.flexbox-row

    html,
    body {
      height: 100%;
      margin: 0
    }
    .flexbox {
      display: flex;
      flex-direction: column;
      flex-wrap: nowrap;
      height: 100%;
      width: 100%
    }
    .flexbox .main {
      flex: 1;
      background: red;
      position: relative
    }
    .flexbox .header,
    .flexbox .footer {
      background: green;
      padding: 1em
    }
    .flexbox-row {
      display: flex;
      position:absolute;
      height: 100%;
      width: 100%
    }
    .flexbox-row .first,
    .flexbox-row .second,
    .flexbox-row .third {
      background: #C9E1F4;
      padding: 1em;
    }
    .flexbox-row .second {
      flex: 1;
      background: #A8EDAC
    }
    <div class="flexbox">
      <div class="header">Header</div>
      <div class="main">
        <div class="flexbox-row">
          <div class="first">First</div>
          <div class="second">Main</div>
          <div class="third">Third</div>
        </div>
      </div>
      <div class="footer">Footer</div>
    </div>

    【讨论】:

    • 谢谢,flexbox-row 的高度是问题所在。如此简单。
    • 我刚刚在chrome中测试过,并不能正常工作,FF和Edge都可以。也许 webkit 需要额外的属性。
    • @Alko 这是一个已知问题 - 在 Chrome 和 Safari 中,无法以百分比识别(非 flex)子项的高度。然而 Firefox 和 IE 会根据高度百分比识别和缩放孩子。 Chrome bug - 所以你可以使用vh
    • 谢谢 我不知道。我可能必须找到另一种解决方法。我接受了你的回答。
    猜你喜欢
    • 2013-04-08
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 2020-04-26
    • 1970-01-01
    • 2015-09-05
    相关资源
    最近更新 更多