0

I essentially want the first block to take up whatever height it will naturally. Then I want the second block fill out the rest of the page with its natural height, without letting it create a vertical scrollbar.

I'm basically hard-coding the 100px via the template row. How can I make things more flexible? I've tried various values like 100% instead of 100px in the rows, as well as auto.

.container {
  display: grid;
  grid-template-rows: 100px 1fr;
}

.rest {
  max-height: calc(100vh - 100px);
}
<div class="container">
  <div>
    <h1>Welcome</h1>
    <h2>Some text that can change length/height</h2>
  </div>
  <div>
    <div class="rest">I want this block to be the remaining 100% height</div>
  </div>
</div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
Dave Stein
  • 8,653
  • 13
  • 56
  • 104

2 Answers2

2

fr won't work here as the height of the container is auto as you are not setting a fixed height to it. So you can set the height of the container to 100vh (note I have set margin of body to zero to override the default browser margin).

Also Use grid-template-rows: auto 1fr to let the first row take auto height and the second row the remaining height. See demo below:

body {
  margin: 0;
}

.container {
  display: grid;
  grid-template-rows: auto 1fr;
  height: 100vh;
}

.container>div {
  border: 1px solid;
}
<div class="container">
  <div>
    <h1>Welcome</h1>
    <h2>Some text that can change length/height</h2>
  </div>
  <div>
    <div class="rest">I want this block to be the remaining 100% height</div>
  </div>
</div>
kukkuz
  • 41,512
  • 6
  • 59
  • 95
0

I was able to accomplish this with flex-box.

Checkout this snippet:

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.parent {
  background-color: steelblue;
  width: 500px;
  height: 500px;
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.child-1 {
  background-color: orangered;
  width: 100%;
}

.child-2 {
  background-color: yellowgreen;
  width: 100%;
  height: 100%;
}
<div class="parent">
   <div class="child-1">
     <p>dsadsa</p>
     <p>dsadsa</p>
     <p>dsadsa</p>
     <p>dsadsa</p>
     <p>dsadsa</p>
  </div>
   <div class="child-2"></div>
</div>
Amiratak88
  • 1,204
  • 12
  • 18
  • This doesn't appear to work. When I go to full screen and shrink the browser window, a scrollbar appears. – Dave Stein Feb 23 '19 at 18:14
  • @DaveStein I edited the code and added a `height: 100vh` to the ***parent*** div, plus a global reset. It should be fine now – Amiratak88 Feb 24 '19 at 18:43