Flexbox 高度百分比

8

我有一个基本的flexbox布局,现在想要应用高度百分比,但是它们全部占用相同的百分比...

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
  text-align: center;
}

.row_one {
  background: blue;
  height: 30%;
}

.row_two {
  background: wheat;
  height: 30%;
}

.row_three {
  background: yellow;
  height: 40%;
}

.fill-height-or-more {
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

.fill-height-or-more > div {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
<section class="some-area fill-height-or-more">
  <div class="row_one">
    Row 1
  </div>
  <div class="row_two">
    Row 2
  </div>
  <div class="row_three">
    Row 3
  </div>
</section>

这在flexbox中可行吗?如果是,有人能给我一个示例吗?

问题在于<section>元素没有定义高度(min-height不算)。因此,存在一个缺失的链接,行的百分比高度会回退到内容高度。 - Michael Benjamin
1个回答

6

使用 flex 属性代替百分比。

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
  text-align: center;
}

.row_one {
  background: blue;
  flex: 3
}

.row_two {
  background: wheat;
  flex: 3;
}

.row_three {
  background: yellow;
  flex: 4;
}

.fill-height-or-more {
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

.fill-height-or-more > div {
  display: flex;
  flex-direction: column;
  justify-content: center;
}
<section class="some-area fill-height-or-more">
  <div class="row_one">
    Row 1
  </div>
  <div class="row_two">
    Row 2
  </div>
  <div class="row_three">
    Row 3
  </div>
</section>


@fightstarr20 没问题!当涉及到flexbox时,我尽量避免使用高度百分比和高度。在Safari和IE中经常会出现问题,如果有一种用flex属性定义大小的方法,那总是比%更好;) - Varin
我该如何停止文本影响高度? - fightstarr20
@fightstarr20 试着添加这个类:.elementNotAffectingText {flex-basis: 0; overflow: auto} - MAChitgarha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接