Flexbox主体和主要最小高度

Jen*_*ell 12 css height flexbox

https://jsfiddle.net/vz7cLmxy/

我试图让身体扩张,但最小高度不起作用.我已经阅读了其他相关主题,但无法理解它.

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;
}
Run Code Online (Sandbox Code Playgroud)
<div class="menu">Menu</div>

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

<div class="footer">Footer</div>
Run Code Online (Sandbox Code Playgroud)

预期的结果是,如果它小于100%,则主要是拉伸以填充高度.

Ran*_*ndy 9

使用flex: 1的中心元素:

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

.Site-content {
  flex: 1;
  background-color:#bbb;
}
Run Code Online (Sandbox Code Playgroud)
<body class="Site">
  <header>This is the header text ?</header>
  <main class="Site-content">…</main>
  <footer>This is the footer text ?</footer>
</body>
Run Code Online (Sandbox Code Playgroud)


kmg*_*mgt 7

要让min-height相关单位在 IE11 中也能工作,只需要一个小技巧。

的本质min-height是覆盖heightwhenheight更小的then min-height。非常清楚!但陷阱是当min-height有一个实际单位(%vh)并且height未设置时。因为它是相对的,所以没有依据。

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

body {
  min-height: 100vh;
}
Run Code Online (Sandbox Code Playgroud)

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

body {
  height: 1px;
  min-height: 100vh;
}
Run Code Online (Sandbox Code Playgroud)

或者为了遵守%规则也必须适用于html

html, body {
  height: 1px;
  min-height: 100%;
}
Run Code Online (Sandbox Code Playgroud)

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

body {
  min-height: 100vh;
}
Run Code Online (Sandbox Code Playgroud)
body {
  height: 1px;
  min-height: 100vh;
}
Run Code Online (Sandbox Code Playgroud)