Flex Div的高度不是100%

问题描述 投票:1回答:2

我身体全高,但是包含最小高度为100%的div不会填充。我希望蓝色填充它的身体容器并弯曲对齐内部div。

https://codepen.io/steventnorris/pen/oVdKqY

html {
  min-height: 100%;
  width: 100%;
}

body {
  min-height: 100%;
  width: 100%;
  background-color: green;
}

.main {
  display: flex;
  flex-direction: row;
  min-height: 100%;
  justify-content: space-between;
  background-color: blue;
}
<div class="main">
  <div>Text 1</div>
  <div>Text 2</div>
</div>
html css css3 flexbox height
2个回答
1
投票

你可能想要通过Height Calculation By Browsers : Containing Blocks and Children。显然heightmin-height在CSS中不是一回事。


min-height更改为height为您的bodyhtml - 请参阅下面的演示:

html{
  height: 100%; /* changed to height */
  width: 100%;
}
body{
  height: 100%; /* changed to height */
  width: 100%;
  background-color: green;
}
.main{
  display: flex;
  flex-direction: row;
  min-height: 100%;
  justify-content: space-between;
  background-color: blue;
}
<div class="main">
  <div>Text 1</div>
  <div>Text 2</div>
</div>

您还可以将height: 100vh提供给main元素以设置flexbox的内在高度 - 请参阅下面的演示:

body{
  margin: 0;
  height: 100vh; /* full viewport height */
  background-color: green;
}
.main{
  display: flex;
  flex-direction: row;
  min-height: 100%;
  justify-content: space-between;
  background-color: blue;
}
<div class="main">
  <div>Text 1</div>
  <div>Text 2</div>
</div>

2
投票

你可以给.main类一个100vh的最小高度。

.main{
 display: flex;
 flex-direction: row;
 min-height: 100vh;
 justify-content: space-between;
 background-color: blue;
 }

但是:Kua zxsw指出

© www.soinside.com 2019 - 2024. All rights reserved.