Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexbox fill available space vertically

Tags:

html

css

flexbox

Now in a flexbox row I can write

<div layout="row">   <div>some content</div>   <div flex></div> <!-- fills/grows available space -->   <div>another content</div> </div> 

I would like to achieve the same but vertically, like on this picture enter image description here Currently the problem is that the div which would need to grow doesn't have any height so the two contents are below each other. I want my second content to be at the bottom of the parent container which has a fixed height!

I know I could solve this by positioning the second content absolute and bottom: 0; but can I achieve this with flexbox?

like image 736
Jim-Y Avatar asked Oct 13 '16 12:10

Jim-Y


People also ask

How do I fill the remaining space on my flexbox?

You can use the flex-grow property to force an item to fill the remaining space on the main axis of a flex container. The item will expand as much as possible and occupy the free area.

How do I fill a remaining space in CSS?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.


2 Answers

So you can try this:

  1. flex-direction: column for the flex container.

  2. flex: 1 for the element that needs to fill the remaining space.

See demo below where the flexbox spans the viewport height:

body {    margin: 0;  }  *{    box-sizing: border-box;  }  .row {    display: flex;    flex-direction: column;    height: 100vh;  }  .flex {    flex: 1;  }  .row, .row > * {    border: 1px solid;  }
<div class="row">    <div>some content</div>    <div class="flex">This fills the available space</div>    <!-- fills/grows available space -->    <div>another content</div>  </div>

Cheers!

like image 55
kukkuz Avatar answered Oct 02 '22 16:10

kukkuz


You just need to use flex-direction: column on parent and flex: 1 on middle div.

body,  html {    padding: 0;    margin: 0;  }  .row {    display: flex;    flex-direction: column;    min-height: 100vh;  }  .row > div:not(.flex) {    background: #676EE0;  }  .flex {    flex: 1;    background: #67E079;  }
<div class="row">    <div>some content</div>    <div class="flex"></div>    <!-- fills/grows available space -->    <div>another content</div>  </div>
like image 38
Nenad Vracar Avatar answered Oct 02 '22 17:10

Nenad Vracar