Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make Flexbox children 100% height of their parent?

Tags:

css

flexbox

I'm trying to fill the vertical space of a flex item inside a Flexbox.

.container {    height: 200px;    width: 500px;    display: flex;    flex-direction: row;  }  .flex-1 {    width: 100px;    background-color: blue;  }  .flex-2 {    position: relative;    flex: 1;    background-color: red;  }  .flex-2-child {    height: 100%;    width: 100%;    background-color: green;  }
<div class="container">    <div class="flex-1"></div>    <div class="flex-2">      <div class="flex-2-child"></div>    </div>  </div>

And here's the JSFiddle

flex-2-child doesn't fill the required height except in the two cases where:

  1. flex-2 has a height of 100% (which is weird because a flex item has a 100% by default + it is buggy in Chrome)
  2. flex-2-child has a position absolute which is also inconvenient

This doesn't work in Chrome or Firefox currently.

like image 509
Raz Avatar asked Mar 13 '13 09:03

Raz


People also ask

How do I make my Div take 100% height of parent DIV?

container div has two parent elements: the <body> and the <html> element. And we all know that the default value of the height property is auto , so if we also set the height of <body> and <html> elements to 100%, the resulting height of the container div becomes equal the 100% height of the browser window.

How do I change my height to 100% in CSS?

height:100vh The . box class has only 100vh which is 100% of the viewport height. When you set the height to 100vh, the box element will stretch its height to the full height of the viewport regardless of its parent height.

How do I set the height on my Flexbox?

You have to give the section a height to limit it to cause the buttons to wrap, otherwise the flex element will just grow to fit the height of whatever's inside of it. I would use height: calc(100vh - 100px) on the flex container to make it take up all of the available space.

How do you inherit parent height in CSS?

height: 100% will match the height of the element's parent, regardless of the parent's height value. height: inherit will, as the name implies, inherit the value from it's parent. If the parent's value is height: 50% , then the child will also be 50% of the height of it's parent.


1 Answers

Use align-items: stretch

Similar to David Storey's answer, my workaround is:

.flex-2 {     display: flex;     align-items: stretch; } 

Note that height: 100% should be removed from the child component (see comments).

Alternatively to align-items, you can use align-self just on the .flex-2-child item you want stretched.

like image 124
B T Avatar answered Sep 19 '22 14:09

B T