I have two sibling elements which each contain dynamic content.
<div class="flex">
<div class="sibling-1"></div>
<div class="sibling-2"></div>
</div>
In some cases sibling-1
will have more content then sibling-2
and vice versa.
I would like the height of the second element sibling-2
always equal the height of the first sibling-1
. If the height of sibling-2
is greater then the height of sibling-1
it will overflow the flex
div and thus be scrollable.
Is there any way to accomplish this with Flexbox?
Yes, it is possible. Leave the sibling setting the max height alone and set the others'
flex-basis: 0
andflex-grow: 1
, which according to the spec will expand them to their sibling's height. No absolute positioning. No setting height on any elements.As I mentioned in the comments and has been continued in other answers, there is no flexbox method.
It is possible using
position:absolute
on the second sibling though...but since that's not actually a flexbox solution, this is here for information only.Basically, no. The flex equal heights feature is based on the height of the container, not any particular sibling.
So
sibling-1
andsibling-2
can always be equal height.But flexbox has no built-in mechanism to limit the height of items to the height of one sibling.
Consider JavaScript or CSS positioning properties.
Here's an example using absolute positioning:
jsFiddle
Yes you can accomplish this by making siblings 1 and 2 also flex containers, then on sibling-2 make an absolute div (also flex container) inside that will be the parent of your scroller
css:
on sibling 2 just set a minimum height on pixels - useful on responsive cases if siblings 1 and 2 stack each other on mobile