1

Is there a CSS solution for having a DIV (D2) filling the remaining parent element height when the DIV above (D1) dynamically changes its size?

|-------------------|    |-------------------|    |-------------------|
| some content   D1 |    | some content   D1 |    | some content   D1 |
| +                 |    | more content      |    | more content      |
|-------------------|    | +                 |    | even more         |
|                D2 |    |-------------------|    | +                 |
|                   |    |                D2 |    |-------------------|
|                   |    |                   |    |                D2 |
|                   |    |                   |    |                   |
|                   |    |                   |    |                   |
|                   |    |                   |    |                   |
|-------------------|    |-------------------|    |-------------------|

D1 also has a max-height and overflow: scroll.

In this jsFiddle make the yellow (d2) fill the rest, whatever the dynamic content of d1 is, without using javascript.

I have seen several question but none treat a dynamic height changing of the upper DIV (D1)

I can not use a Javascript solution. TABLEs are also fine instead of DIVs.

Community
  • 1
  • 1
Gabriel Petrovay
  • 20,476
  • 22
  • 97
  • 168

2 Answers2

0

Check updated answer below.

make structure like this

 div class=main  {
     div class=d1 {
          something 
          more stuff
          even more stuff
          }
     div class=d2{
            something
           }

}

css :

div main { 
      width 200 px; for example
      }
d1, d2 { 
      width 100%,
      and float:left
      }

now by doing this when d1 changes d2 automatically goes down, don't give height to any DIV

Update : Using Flexbox property

.main {
  width: 500px;
  height: 160px;
  background: grey;
  border: 1px solid #333;
  display: flex;
  flex-direction: column;
}
.d1 {
  background: lime;
}
.d2 {
  background: yellow;
  height: 100%;
}
.d1,
.d2 {
  width: 100%;
}
<div class="main">
  <div class="d1">
    d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1 d1
  </div>
  <div class="d2">
    d2 d2 d2 d2 d2 d2 d2 d2 d2 d2 d2 d2 d2 d2 d2 d2 d2 d2 d2 d2 d2 d2 d2 d2 d2 d2 d2 d2 d2
  </div>
</div>

Now how much content is in D1 doesn't matter, D2 will take full height of main div.

shyammakwana.me
  • 5,562
  • 2
  • 29
  • 50
0

I prefer more descriptive class names...

.container {
    display: flex;
    flex-direction: column;
    font: 20px Verdana;
    height: 88vh;
    background: grey;
    padding: 5px;
}
.upper{
    flex: 1 1; /* grow shrink */
    background: lime;
}
.lower{
    flex: 1 1; /* grow shrink */
    background: yellow;
    overflow-y: auto;
}

Here's a screen shot from my fiddle

https://jsfiddle.net/spencerw/zLcwq42s/52/

jbobbins
  • 1,221
  • 3
  • 15
  • 28