-1

I would like to have 3 elements (a header, a footer, and some content) stacked vertically in the page. The header and footer div have a fixed height. How can I make the content div (the one in the middle) take up the remaining vertical space?

My attempt with Flexbox

.wrapper {
  display: flex;
  flex-direction: column;
  width: 100%;
}

.header {
  background-color: red;
  height: 100px;
}

.content {
  background-color: blue;
  flex: 1;
}

.footer {
  background-color: green;
  height: 30px;
}
<div class="wrapper">
  <div class="header" />
    <div class="content" />
    <div class="footer" />
</div>
Espresso
  • 740
  • 13
  • 32

2 Answers2

2

Your css is already doing what you want to achieve.

flex: 1 is shorthand for flex-grow: 1

As other items have fixed height, flex: 1 causes your content to fill remaining space in your flexbox. The issue is however that your flexbox container doesn't have a height, if you want to cover the whole page you should add height: 100vh to your container.

.wrapper {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100vh;
}
Berk Kurkcuoglu
  • 1,453
  • 1
  • 9
  • 11
-2

The footer does not need to at the bottom at the beginning after a certain space is taken by the content it will automatically will come at the bottom.

.wrapper {
  width: 100%;
}

.header {
  background-color: red;
  height: 100px;
}

.content {
  background-color: blue;
  height:200px;
}

.footer {
  background-color: green;
  height: 30px;
}
<div class="wrapper">
  <div class="header"></div>
    <div class="content"></div>
    <div class="footer"></div>
</div>
Nutan Panta
  • 59
  • 1
  • 16