Fill remaining vertical space with CSS using display:flex
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:
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; */
}
<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>
Fiddle:
- http://jsfiddle.net/7yLFL/1/ (working, with small content)
- http://jsfiddle.net/7yLFL/ (broken, with larger content)
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 a div fill the height of the remaining screen space
- Fill remaining vertical space - only CSS
- Have a div to fill out the remaining height/width of a container when sharing it with another div?
- Make nested div stretch to 100% of remaining container div height
- How can I make my flexbox layout take 100% vertical space?
- etc
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*/
}
<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>
Full screen version
section {
display: flex;
flex-flow: column;
height: 100vh;
}
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*/
}
body {
margin: 0;
}
<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>
The example below includes scrolling behaviour if the content of the expanded centre component extends past its bounds. Also the centre component takes 100% of remaining space in the viewport.
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;
}
Example of html that can be used to demonstrate this behaviour
<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>
Use the flex-grow
property to the main content div and give the display: flex;
to its parent;
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;
}
<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>
A more modern approach would be to use the grid property.
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;
}
<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>
This is the simplest example that I can think of. The key is
- Parent is
display:flex
- Child has
flex-grow:1
- Parent MUST have
height
specified. If you specifyheight:100%
on a parent div, then remember, the parent exists in a<body>
and you'll see the body isn't 100% unless you addheight:100%
on the body too.
http://jsfiddle.net/Ljbzsmvf/2/
div#parent {
height: 300px;
display: flex;
flex-direction: column;
}
div#child {
border: thin solid red;
flex-grow: 1;
}
<div id='parent'>
Parent
<div id='child'>
Child
</div>
</div>
Here is the codepen demo showing the solution:
Important highlights:
- all containers from
html
,body
, ....container
, should have the height set to 100% - introducing
flex
to ANY of the flex items will trigger calculation of the items sizes based on flex distribution:- if only one cell is set to
flex
, for example:flex: 1
then this flex item will occupy the remaining of the space - if there are more than one with the
flex
property, the calculation will be more complicated. For example, if the item 1 is set toflex: 1
and the item 2 is se toflex: 2
then the item 2 will take twice more of the remaining space- NOT TRUE: the item 2 will be twice larger than the item 1
- check more about the concept of the remaining space: https://developer.mozilla.org/en-US/docs/Web/CSS/flex-grow
- if only one cell is set to
- Main Size Property
- depends on the value of the
flex-direction
property - in our case height is just a preferred size
- it will be overwritten in the presence of
flex
property: https://www.w3.org/TR/css-flexbox-1/#propdef-flex- When a box is a flex item, flex is consulted instead of the main size property to determine the main size of the box
min-*
andmax-*
will be respected
- depends on the value of the
ReferenceURL : https://stackoverflow.com/questions/25098042/fill-remaining-vertical-space-with-css-using-displayflex
'programing' 카테고리의 다른 글
GCC가 변수를 할당하기 전에 임시 위치로 이동하는 이유는 무엇입니까? (0) | 2023.08.16 |
---|---|
Get-WMIObject \Get-Cim 인스턴스는 실제로 무엇을 합니까? (0) | 2023.08.11 |
스위프트를 사용하여 iOS에서 Numpad에 "완료" 버튼을 추가하는 방법은 무엇입니까? (0) | 2023.08.11 |
SUM과 SUBTOTAL의 차이점은 무엇입니까? (0) | 2023.08.11 |
spring security httpantMatcher(경로가 여러 개인 경우) (0) | 2023.08.11 |