Sibling items are limited in size in flex/grid layout
P粉134288794
2023-08-24 12:52:48
<p>I have two sibling elements, each containing dynamic content. </p>
<pre class="brush:php;toolbar:false;"><div class="flex">
<div class="sibling-1"></div>
<div class="sibling-2"></div>
</div></pre>
<p>In some cases, <code>sibling-1</code> will contain more content than <code>sibling-2</code>, and vice versa.
I want the height of the second element <code>sibling-2</code> to always be equal to the height of the first <code>sibling-1</code>. If the height of <code>sibling-2</code> is greater than the height of <code>sibling-1</code>, it will overflow the <code>flex</code> div and become scrollable. </p>
<p>Is there a way to achieve this effect using Flexbox? </p>
Basically not possible. The flex height feature is based on the height of the container, not any specific sibling elements.
So, brother element 1 and brother element 2 can always be of the same height.
However, flexbox has no built-in mechanism to constrain an item to be the same height as a sibling element.
Consider using JavaScript or CSS positioning properties.
Here is an example using absolute positioning:
jsFiddle
Yes, it is possible. Retain the maximum height set by the sibling node, and set the other nodes'
flex-basis: 0
andflex-grow: 1
to, according to the specification, they will expand to the same height as the sibling node high. There is no absolute positioning. No height is set on any element.