Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrollable child when max-height of parent is reached

Tags:

html

css

My problem is quite similar to this one but the difference I can see is that my parent div (#container) have a min-height and a max-height (no height as it should grow with its content), but the scrollable div (#dynamic-part) does not know its height nor max-height either. It should be scrollable when its parent has reached its max-height.

Is that something possible with pure CSS ?

HTML

<div id="container">
  <div id="fixed-part">
  This will always be 60px big
  </div>
  <div id="dynamic-part">
    This can grow and so should be scrollable<br><br>
    <a href="#" onclick="addContent()">Add content</a><br><br>
    <div id="insert">
    </div>
  </div>
</div>

CSS

#container {
  min-height: 100px;
  max-height: 300px;
  border: solid 1px black;
  width: 200px;
  padding: 0 5px;
}

#fixed-part {
  width: 100%;
  height: 60px;
  border: solid 1px pink;
}

#dynamic-part {
  width: 100%;
  max-height: calc(100% - 60px);
  border: solid 1px blue;
}

Here is a fiddle to help: https://jsfiddle.net/bz0b4ydz/

like image 474
Valentin Coudert Avatar asked Jan 30 '23 16:01

Valentin Coudert


1 Answers

If you use the flexbox model, then this is easy:

var count = 1;//demo purpose

function addContent() {
  let div = document.getElementById("insert");
  divChild = document.createElement('div');
  divChild.setAttribute('class', 'content');
  divChild.innerHTML = 'New content ' + count;
  div.insertBefore(divChild, div.firstChild);// prepend new div
  count++ // demo update count
}
#container {
  display: flex;
  flex-flow: column;
  min-height: 100px;
  max-height: 300px;
  border: solid 1px black;
  width: 200px;
  padding: 0 5px;
}

#fixed-part {
  flex-shrink: 0;
  /* or 
                       no height set but the full flex set : 
                       flex:1 0 60px;*/
  height: 60px;
  /* can be avoid, see previous comment */
  border: solid 1px pink;
}

#dynamic-part {
  flex: 1;
  border: solid 1px blue;
  overflow: auto;
}

.content {
  background-color: grey;
  width: 100%;
  height: 50px;
  margin: 5px auto;
}
<div id="container">
  <div id="fixed-part">
    This will always be 60px big
  </div>
  <div id="dynamic-part">
    This can grow and so should be scrollable<br><br>
    <a href="#" onclick="addContent()">Add content</a><br><br>
    <div id="insert">
    </div>
  </div>
</div>
like image 91
G-Cyrillus Avatar answered Feb 03 '23 03:02

G-Cyrillus