0

I have a div including two other divs

<div style="max-height:100px">
   <div> first div  </div>
   <div> second div </div>
</div>

The parent div has a maximum height so cannot grow more than a certain amount. But the two child divs could have any size (calculated dynamically). so I need to show only parts of the child divs if their collective height is bigger than the parent div's max height. My question is how can I make sure first the second div get cut first and then the first div. In other words, I need to see more of first child div than the second one.

Example1: For instance, if the parent div maximum height is 100 px and the height of the first div is 120px and the height of the second div is also 120px, then in total it is 240px. so I need to fully hide the second div and show only 100px of the first div.

Example2: or if the first div height is 80px and the second div height is also 70px, then I need to show 20px from the second div and 80px from the first child.

1
  • your are describing the default behavior of HTML/CSS ... there is notihng to do, only hide the overflow Commented Sep 20, 2018 at 21:02

2 Answers 2

1
<div style="max-height:100px; overflow: hidden; background-color: lightgreen;">
   <div style="height: 80px; background-color: lightblue;">first div </div>
   <div style="height: 70px; background-color: pink;">second div</div>
</div>

https://jsfiddle.net/hxqdLp17/1/

Like this?

0

You only need to add 'overflow: hidden;' to the parent's CSS.

Try this:

<div style="max-height:100px; border: 1px solid #ccc; overflow: hidden;">
   <div style='height:120px;'> first div  </div>
   <div style='height:120px;'> second div </div>
</div>
2
  • Thanks for the comment. I have added overflow: hidden but what it does is first cut from the first div and then second div. But I want the other way around. The content of the first div is more important than the second div. So if the content is going to be hidden, I prefer to hide more from the second div than the first one.
    – amistres
    Commented Sep 20, 2018 at 21:15
  • The code is doing the correct thing. check out here on codepen, where I set first as 60 and the second as 60, total is 120 (bigger than 120). It does show the first DIV's content fully. codepen.io/anon/pen/VGgNpd
    – obvdso
    Commented Sep 20, 2018 at 21:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.