gpt4 book ai didi

html - 一个 flex/网格元素设置 sibling 的大小限制

转载 作者:行者123 更新时间:2023-11-27 22:54:17 25 4
gpt4 key购买 nike

我有两个兄弟元素,每个元素都包含动态内容。

<div class="flex">
<div class="sibling-1"></div>
<div class="sibling-2"></div>
</div>

在某些情况下,sibling-1 的内容会比 sibling-2 多,反之亦然。我希望第二个元素 sibling-2 的高度始终等于第一个 sibling-1 的高度。如果 sibling-2 的高度大于 sibling-1 的高度,它将溢出 flex div,因此可以滚动。

有什么方法可以用 Flexbox 来实现吗?

最佳答案

是的,这是可能的。让 sibling 单独设置最大高度并设置其他人的 flex-basis: 0flex-grow: 1,根据规范,这会将它们扩展到他们 sibling 的高度。没有绝对的定位。任何元素都没有设置高度。

main {
display: flex;
}

section {
display: flex;
flex-direction: column;
width: 7em;
border: thin solid black;
margin: 1em;
}

:not(.limiter)>div {
flex-basis: 0px;
flex-grow: 1;
overflow-y: auto;
}
<main>
<section>
<div>I'm longer and will scroll my overflow. in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text
in flow text in flow text in flow text in flow text in flow text in flow text in</div>
</section>

<section class="limiter">
<div>Every parent's siblings match my height. in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text</div>
</section>

<section>
<div>I'm shorter but still match the height. in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text in flow text</div>
</section>
</main>

关于html - 一个 flex/网格元素设置 sibling 的大小限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59323357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com