使用display:flex填充剩余的垂直空间

Zil*_*ilk 160 css layout flexbox

在3排布局中:

  • 顶行应根据其内容确定大小
  • 底行应具有固定的高度(以像素为单位)
  • 中间行应该展开以填充容器

问题是,当主要内容扩展时,它会删除页眉和页脚行:

弯曲不好

HTML:

<section>
  <header>
    header: sized to content
    <br>(but is it really?)
  </header>
  <div>
    main content: fills remaining space<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    <!-- uncomment to see it break - ->
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    <!-- -->
  </div>
  <footer>
    footer: fixed height in px
  </footer>
</section>
Run Code Online (Sandbox Code Playgroud)

CSS:

section {
  display: flex;
  flex-flow: column;
  align-items: stretch;
  height: 300px;
}
header {
  flex: 0 1 auto;
  background: tomato;
}
div {
  flex: 1 1 auto;
  background: gold;
  overflow: auto;
}
footer {
  flex: 0 1 60px;
  background: lightgreen;
  /* fixes the footer: min-height: 60px; */
}
Run Code Online (Sandbox Code Playgroud)

小提琴:

我很幸运,我可以使用最新最好的CSS,无视旧版浏览器.我以为我可以使用flex布局来最终摆脱旧的基于表格的布局.出于某种原因,它没有做我想要的......

为了记录,有很多关于"填充剩余高度"的相关问题,但没有解决我在flex中遇到的问题.参考文献:

G-C*_*Cyr 211

简单说明:

section {
  display: flex;
  flex-flow: column;
  height: 300px;
}

header {
  background: tomato;
  /* no flex rules, it will grow */
}

div {
  flex: 1;  /* 1 and it will fill whole space left if no flex value are set to other children*/
  background: gold;
  overflow: auto;
}

footer {
  background: lightgreen;
  min-height: 60px;  /* min-height has its purpose :) , unless you meant height*/
}
Run Code Online (Sandbox Code Playgroud)
<section>
  <header>
    header: sized to content
    <br/>(but is it really?)
  </header>
  <div>
    main content: fills remaining space<br> x
    <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    <!-- uncomment to see it break -->
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> x
    <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> x
    <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br> x
    <br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    <!-- -->
  </div>
  <footer>
    footer: fixed height in px
  </footer>
</section>
Run Code Online (Sandbox Code Playgroud)

  • 如果我们希望截面高度为100%怎么办? (18认同)
  • @PaulTotzke然后它更像是另一个问题,你只需要将高度设置为100%.像往常一样,父母需要一个高度设置/可用,否则我们有上面代码的经典100%'null'示例:html,body,section {height:100%;}其中section是body的直接子项http:// jsfiddle.net/7yLFL/445/这会修复页眉和页脚. (4认同)
  • 在Firefox中,此解决方案存在问题。我通过将“ flex:1”更改为“ flex-grow:1”进行了纠正。感谢您的解决方案! (2认同)

gam*_*zii 18

如果展开的中心组件的内容超出其边界,则下面的示例包括滚动行为.此外,中心组件占用视口中剩余空间的100%.

jsfiddle在这里

html, body, .r_flex_container{
    height: 100%;
    display: flex;
    flex-direction: column;
    background: red;
    margin: 0;
}
.r_flex_container {
    display:flex;
    flex-flow: column nowrap;
    background-color:blue;
}

.r_flex_fixed_child {
    flex:none;
    background-color:black;
    color:white;

}
.r_flex_expand_child {
    flex:auto;
    background-color:yellow;
    overflow-y:scroll;
}
Run Code Online (Sandbox Code Playgroud)

可用于演示此行为的html示例

<html>
<body>
    <div class="r_flex_container">
      <div class="r_flex_fixed_child">
        <p> This is the fixed 'header' child of the flex container </p>
      </div>
      <div class="r_flex_expand_child">
            <article>this child container expands to use all of the space given to it -  but could be shared with other expanding childs in which case they would get equal space after the fixed container space is allocated. 
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,
            </article>
      </div>
      <div class="r_flex_fixed_child">
        this is the fixed footer child of the flex container
        asdfadsf
        <p> another line</p>
      </div>

    </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

  • 这可以通过从选择器中删除`html`并将`height:100vh` appyling到`body`来略微简化:https://jsfiddle.net/pm6nqcqh/1/ (3认同)

dav*_*vid 8

这是我能想到的最简单的例子。关键是

  1. 父级是display:flex
  2. 孩子有flex-grow:1
  3. 家长必须height指定。如果您height:100%在父级 div 上指定,请记住,父级存在于 a 中<body>,并且您将看到正文不是 100%,除非您height:100%也在正文上添加。

http://jsfiddle.net/Ljbzsmvf/2/

div#parent {
  height: 300px;
  display: flex;
  flex-direction: column;
}

div#child {
  border: thin solid red;
  flex-grow: 1;
}
Run Code Online (Sandbox Code Playgroud)
<div id='parent'>
  Parent
  <div id='child'>
    Child
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)


wup*_*367 6

一种更现代的方法是使用grid属性。

section {
  display: grid;
  align-items: stretch;
  height: 300px;
  grid-template-rows: min-content auto 60px;
}
header {
  background: tomato;
}
div {
  background: gold;
  overflow: auto;
}
footer {
  background: lightgreen;
}
Run Code Online (Sandbox Code Playgroud)
<section>
  <header>
    header: sized to content
    <br>(but is it really?)
  </header>
  <div>
    main content: fills remaining space<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    
  </div>
  <footer>
    footer: fixed height in px
  </footer>
</section>
Run Code Online (Sandbox Code Playgroud)

  • 看到这样的东西使我发笑。我们一直围绕基于表的设计进行了探索。您在1995年学到的所有内容都再次具有相关性,只是语法不同。 (8认同)
  • CSS 网格支持是相当新的。除非您不必支持旧浏览器,否则我不会使用它。 (2认同)
  • 这超出了视口的高度 (2认同)

Dee*_*ath 5

将该flex-grow属性用于主要内容 div 并将其赋予display: flex;其父级;

body {
    height: 100%;
    position: absolute;
    margin: 0;
}
section {
  height: 100%;
  display: flex;
  flex-direction : column;
}
header {
  background: tomato;
}
div {
  flex: 1; /* or flex-grow: 1  */;
  overflow-x: auto;
  background: gold;
}
footer {
  background: lightgreen;
  min-height: 60px;
}
Run Code Online (Sandbox Code Playgroud)
<section>
  <header>
    header: sized to content
    <br>(but is it really?)
  </header>
  <div>
    main content: fills remaining space<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
    x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
  </div>
  <footer>
    footer: fixed height in px
  </footer>
</section>
Run Code Online (Sandbox Code Playgroud)