Flexbox body 和 main min-height

Flexbox body and main min-height

https://jsfiddle.net/vz7cLmxy/

我试图让 body 展开,但 min-height 不起作用。我已经阅读了其他相关主题,但无法理解它。

CSS 和 html

body,
html {
  padding: 0;
  margin: 0;
  min-height: 100%;
}

body {
  display: flex;
  background: #eee;
  flex-direction: column;
}

.menu {
  background: red;
  color: #fff;
  padding: 10px;
}

.wrap {
  display: flex;
}

.sidebar {
  background: #ddd;
  width: 300px;
}

.main {
  background: #ccc;
  flex: 1;
}

.footer {
  background: #000;
  color: #fff;
  padding: 10px;
}
<div class="menu">Menu</div>

<div class="wrap">
  <div class="sidebar">Sidebar</div>
  <div class="main">Main</div>
</div>

<div class="footer">Footer</div>

预期结果是,如果 main 小于 100%,则它会被拉伸以填充高度。

在居中元素上使用 flex: 1

.Site {
  display: flex;
  min-height: 100vh;
  flex-direction: column;
}

.Site-content {
  flex: 1;
  background-color:#bbb;
}
<body class="Site">
  <header>This is the header text ☺</header>
  <main class="Site-content">…</main>
  <footer>This is the footer text ☻</footer>
</body>

要使 min-height 即使在 IE11 中也能正常工作,只需要一个小技巧。

min-height的本质是当height小于min-height时覆盖height。非常清楚!但陷阱是当 min-height 有一个实际单位(%vh)并且 height 没有设置。既然是相对的,就没有依据。

对于除 Internet Explorer 之外的所有主流浏览器,一种可能是将单位从 % 更改为 vh

body {
  min-height: 100vh;
}

对于 Internet Explorer,它需要 height(将从 min-height 覆盖):

body {
  height: 1px;
  min-height: 100vh;
}

或者要保持 % 规则也必须应用于 html:

html, body {
  height: 1px;
  min-height: 100%;
}

OP 的跨浏览器解决方案在 body 上需要 height: 1px,当然 .wrap 需要 flex-grow: 1 才能让它比 menu 增长得更快,并且footer:

body,
html {
  padding: 0;
  margin: 0;
  height: 1px; /* added */
  min-height: 100%;
}

body {
  display: flex;
  background: #eee;
  flex-direction: column;
}

.menu {
  background: red;
  color: #fff;
  padding: 10px;
}

.wrap {
  display: flex;
  flex-grow: 1; /* added */
}

.sidebar {
  background: #ddd;
  width: 300px;
}

.main {
  background: #ccc;
  flex: 1;
}

.footer {
  background: #000;
  color: #fff;
  padding: 10px;
}
<div class="menu">Menu</div>

<div class="wrap">
    <div class="sidebar">Sidebar</div>
    <div class="main">Main</div>
</div>

<div class="footer">Footer</div>