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

相关文章:

HTML/CSS : Is it possible to make a table that can have its column selected?

jquery - Bootstrap 高度在显示错误时增加

javascript - 使用 Angular 4、jsPDF、html2canvas 从 HTML/CSS 字符串生成 PDF

html - 悬停时无法显示水平菜单的子菜单

css - 小屏幕上 3 个 div 的不同 flexbox 对齐方式

css - 具有大量嵌套的 Flexbox 工具栏

javascript - 如何重新加载 iframe 的父级而不重新加载它

jquery - 如何使用 jQuery Validation Engine 和 Twitter Bootstrap modals 修复定位错误

javascript - 如何使用 .css() 应用 !important?

android - 在三星安卓浏览器上启用真正的固定定位