I have a flexbox with 2 items, direction=row. The text content of the second item is very long. I would like the second item to be as high as the first item, and have a scrollbar. Is this possible?
#wrap { display: flex; }
#item-1 { height: 100px; background: orange; flex: 1; }
#item-2 { overflow: scroll; flex: 1; }
<div id='wrap'>
<div id='item-1'></div>
<div id='item-2'>
I would like this text to have a scrollbar, and thus not take up more height than the orange box.<br> I would like this text to have a scrollbar, and thus not take up more height than the orange box.<br> I would like this text to have a scrollbar, and thus not take up more height than the orange box.<br> I would like this text to have a scrollbar, and thus not take up more height than the orange box.<br> I would like this text to have a scrollbar, and thus not take up more height than the orange box.<br> I would like this text to have a scrollbar, and thus not take up more height than the orange box.<br> I would like this text to have a scrollbar, and thus not take up more height than the orange box.<br>
</div>
</div>
The closest post I've found is this one, but the answer doesn't seem to work in my case.
By default, the child elements of a flexbox container will stretch vertically to fill the height of the container. This can be prevented by using the align-self property on the child element that you do not want to stretch.
Add a wrapper having position: absolute
Now, you can set a min-height
to the left most, which the height of the right most will follow.
#wrap { display: flex; }
#item-1 { min-height: 100px; background: orange; flex: 1; }
#item-2 { position: relative; flex: 1; }
#item-wrap {
position: absolute;
left: 0; top: 0;
right: 0; bottom: 0;
overflow: auto;
}
<div id='wrap'>
<div id='item-1'>
If this gets longer, right most follows<br>
If this gets longer, right most follows<br>
If this gets longer, right most follows<br>
If this gets longer, right most follows<br>
If this gets longer, right most follows<br>
If this gets longer, right most follows<br>
If this gets longer, right most follows<br>
If this gets longer, right most follows<br>
</div>
<div id='item-2'>
<div id='item-wrap'>
I would like this text to have a scrollbar, and thus not take up more height than the orange box.<br> I would like this text to have a scrollbar, and thus not take up more height than the orange box.<br> I would like this text to have a scrollbar, and thus not take up more height than the orange box.<br> I would like this text to have a scrollbar, and thus not take up more height than the orange box.<br> I would like this text to have a scrollbar, and thus not take up more height than the orange box.<br> I would like this text to have a scrollbar, and thus not take up more height than the orange box.<br> I would like this text to have a scrollbar, and thus not take up more height than the orange box.<br>
</div>
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With