Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

height 100% in flex

Tags:

html

css

flexbox

I want to make the content div to fit 100% height. For some reason, the container div happen to be a flex item. Is that appropriate to set 100% height to a div within a flex item? or I should set the content div to be a flex item as well.

Also, the flex-direction part is confusing. column do not work, but row do. I suppose the flex-direction only effect on the flex item.

jsfiddle here

<div class="wrapper">
  <div class="container">
    <div class="content">
      Hello there
    </div>
  </div>
</div>


html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

.wrapper {
  border: 1px solid red;
  padding: 20px;
  min-height: 100%;
  display: flex;
  /* change flex-direction from column to row will work */
  flex-direction: column;
}

.container {
  border: 1px solid green;
  padding: 20px;
  flex: 1;
}

.content {
  border: 1px solid blue;
  height: 100%;
}
like image 335
arpeggie Avatar asked Dec 05 '25 13:12

arpeggie


1 Answers

You can overlap (nest) flex boxes :

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

.wrapper {
  border: 1px solid red;
  padding: 20px;
  min-height: 100%;
  display: flex;
  /* change flex-direction from column to row will work */
  flex-direction: column;
  box-sizing:border-box;
}

.container {
  display:flex;
  flex-direction: column;/* up to your needs */
  border: 1px solid green;
  padding: 20px;
  flex: 1;
}

.content {
  border: 1px solid blue;
  flex:1;
}
<div class="wrapper">
  <div class="container">
    <div class="content">
      Hello there
    </div>
  </div>
</div>

You may also mind the box-sizing properties to include borders and padding in size calculation.

like image 72
G-Cyrillus Avatar answered Dec 08 '25 08:12

G-Cyrillus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!