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

标签 html css flexbox

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

<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/56777768/

相关文章:

jquery - 如何将包含超赞字体图标的 div 定位到另一个 div 的中心?

css - 如何防止 ionic 卡中的自动调整图像大小

html - flexbox 和 bootstrap3 列的溢出布局导致页面宽度增加

html - 如何在 Bootstrap 中按百分比排列 div 的高度,没有滚动条

javascript - 为什么我的 CSS 和 Bootstrap.css 样式没有应用?

css - 如何将表格中的文本(垂直和水平)居中,并防止单元格大小调整?

javascript - 保存用户的黑暗模式偏好的问题

css - Flex box 需要宽度才能适当调整大小

css - 防止前一个 div 调整大小时 div 向下移动

javascript - Konva addEventListener 单击多个图像