如何使用flexbox填充剩余高度?

时间:2017-01-25 13:20:43

标签: html css flexbox



html,
body {
  height: 100%;
}
.site-main {
  height: calc(100% - 72px);
  display: flex;
}
.site-main > div {
  min-width: 85%;
  height: 100%;
}
.site-main > aside {
  min-width: 15%;
  height: 100%;
  background-color: $darkest-blue-grey;
}

<header>
  <h1>Title</h1>
</header>
<div class="site-main">
  <div></div>
  <aside></aside>
</div>
&#13;
&#13;
&#13;

header位于height 72px

我已.site-main div width 85%

我已.site-main aside width 15%

我想要的是.site-main div.site-main aside并排,.site-main填充header之后的剩余空格。

.site-main div.site-main aside填充.site-main的身高。

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:6)

您可以flex-direction: column使用bodyflex: 1使用site-main

&#13;
&#13;
body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  margin: 0;
  padding: 0;
}
header {
  height: 75px;
}
.site-main {
  display: flex;
  flex: 1;
}
.site-main div {
  flex: 0 0 85%;
  background: lightblue;
}
aside {
  flex: 0 0 15%;
  background: lightgreen;
}
&#13;
<header>
  <h1>Title</h1>
</header>
<div class="site-main">
  <div></div>
  <aside></aside>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

对于需要支持IE11(和10)的人来说,这个解决了 IE min-height bug

注意,为了在IE10上工作,需要添加前缀的flexbox属性

&#13;
&#13;
html, body {
  margin: 0;
}
body {
  display: -ms-flexbox;        /* IE 10 */
  display: flex;               /* IE11/10 bug fix    */
}
.wrapper {
  -ms-flex: 1;                 /* IE 10 */
  flex: 1;                     /*  fill 100% width   */
  display: -ms-flexbox;        /* IE 10 */
  display: flex;
  -ms-flex-direction: column;  /* IE 10 */
  flex-direction: column;
  min-height: 100vh;
}
header {
  height: 72px;
}
.site-main {
  -ms-flex: 1;                 /* IE 10 */
  flex: 1;                     /*  fill 100% height  */
  display: -ms-flexbox;        /* IE 10 */
  display: flex;
}
.site-main > div {
  width: 85%;
  background-color: lightgreen;
}
.site-main > aside {
  width: 15%;
  background-color: lightblue;
}
&#13;
<div class="wrapper">
  <header>
    <h1>Title</h1>
  </header>
  <div class="site-main">
    <div></div>
    <aside></aside>
  </div>
</div>
&#13;
&#13;
&#13;