使用display:flex填充剩余的垂直空间 [英] Fill remaining vertical space with CSS using display:flex

查看:11500
本文介绍了使用display:flex填充剩余的垂直空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在3行布局中:




  • 顶行应根据其内容进行调整

  • 底部一行应以像素为单位具有固定高度

  • 中间一行应展开以填充容器



问题是,当主内容展开时,它会压缩页眉和页脚行:





HTML:

 < section> 
< header>
header:size to content
< br>(但是它真的吗?)
< / header>
< div>
主要内容:填充剩余空间< br>
x< br> x< br> x< br> x< br> x< br> x< br> x< br> x< br> x&
<! - uncomment to see it break - - >
x< br> x< br> x< br> x< br> x< br> x< br> x< br> x< br> x&
x< br> x< br> x< br> x< br> x< br> x< br> x< br> x< br> x&
x< br> x< br> x< br> x< br> x< br> x< br> x< br> x< br> x&
x< br> x< br> x< br> x< br> x< br> x< br> x< br> x< br> x&
<! - - >
< / div>
< footer>
footer:fixed height in px
< / footer>
< / section>

CSS:

 节{
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;
/ *修复页脚:min-height:60px; * /
}

小提琴: b
$ b



我在幸运的情况我可以使用最新和最伟大的CSS,忽视旧版浏览器。我想我可以使用flex布局终于摆脱了旧的基于表的布局。出于某种原因,它不是我想要的...



对于记录,有很多相关问题关于填充剩余高度,但没有什么解决我使用flex的问题。参考:




解决方案

简单: DEMO

 柔性; 
flex-flow:column;
height:300px;
}
header {
background:tomato;
/ *没有flex规则,它将增长* /
}
div {
flex:1; / * 1并且它将填充整个空间给其他孩子* /
背景:金;
overflow:auto;
}
footer {
background:lightgreen;
min-height:60px; / * min-height有它的目的:),除非你的意思是高度* /
}


In a 3-row layout:

  • the top row should be sized according to its contents
  • the bottom row should have a fixed height in pixels
  • the middle row should expand to fill the container

The problem is that as the main content expands, it squishes the header and footer rows:

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>

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; */
}

Fiddle:

I'm in the lucky situation that I can use the latest and greatest in CSS, disregarding legacy browsers. I thought I could use the flex layout to finally get rid of the old table-based layouts. For some reason, it's not doing what I want...

For the record, there are many related questions on SO about "filling the remaining height", but nothing that solves the problem I'm having with flex. Refs:

解决方案

Make it simple : DEMO

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*/
}

这篇关于使用display:flex填充剩余的垂直空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆