0

I'm looking for a solution to fix the max-height of a div related to another div in CSS, if it exceeds it should let use the scrollbar.

I was planned to use flexbox or grid, but I do not success to the result wanted...

The objective is to not specify any height in px but keep something flexible without JS.

// Not looking for JS solution
.parent {
  display: flex;
}

.child1 {
  background: #eeeeee;
  height: 100%
}

.child2 {
  overflow: auto;
  max-height: 100%;
  background: #cccccc
}
<div class="parent">
  <div class="child1">
    <h2>Child1</h2>
    <p>Some content</p>
    <p>Some content</p>
    <p>Some content</p>
    <p>Some content</p>
    <p>Some content</p>
  </div>
  <div class="child2">
    <h2>Child2</h2>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>

  </div>
</div>
Mike5
  • 61
  • 11

3 Answers3

-1

if you dont want to specify fixed heihgt you can just add max-height: calc(100vh - 200px) it means if screen height = 1000px your div will be not longer than 800px if it is more than it than div will be use scrollbar

.parent {
 display: flex;
 max-height: calc(100vh - 100px)
}

.child1 {
  background: #eeeeee;
  overflow:auto;
  width:max-content;
}

.child2 {
 overflow: auto;
 max-height: 100%;
 background: #cccccc
}

view codepen example here

-1

You should set a height property to your parent div https://stackblitz.com/edit/js-dqkz2u?file=index.html,index.js,style.css

-1

height: auto; max-height: 100vh;

add these two lines to child2 class

kavi castelo
  • 16
  • 1
  • 4
  • Thank's, but then it's related to viewport. It should be related to child1. It's the content of child1 which must set the height of parent. And child2 related to this height. – Mike5 Nov 17 '22 at 17:56