【问题标题】:How to shrink flex items height to fit container heights如何缩小弹性项目高度以适应容器高度
【发布时间】:2016-04-28 18:34:32
【问题描述】:

我正在尝试在 flex 中显示下面的布局。

我有相同宽度和高度的 div。我想实现下面的布局。通过将一件物品放在左侧,其余四件放在右侧容器上,并在此过程中保持相同的容器高度。那是左边的一个增加以填充左边的高度和宽度,另一个右边的容器大小在四个项目之间共享。

.trades {
  display: flex;
  flex: 1;
}
.trade-panel {
  flex: 1;
}
.layout-5-2 {
  -ms-flex-direction: row;
  flex-direction: row;
  justify-content: space-between;
  display: flex;
}
.layout-container-5-2-1 {
  -ms-flex-direction: column;
  flex-direction: column;
  height: 100%;
  justify-content: space-between;
  flex: 0 0 48%;
  display: flex;
}
.layout-container-5-2-2 {
  flex: 0 0 48%;
  display: flex;
  flex-direction: column;
}
<div class="trades">
  <div class="layout-5-2">
    <div class="layout-container-5-2-1">
      <div class="trade-panel">item 1</div>
      <div class=" trade-panel">item 2</div>
      <div class="trade-panel">item 3</div>
      <div class=" trade-panel">item 4</div>
    </div>

    <div class="layout-container-5-2-1">
      <div class="trade-panel">vertical item.</div>
    </div>
  </div>
</div>

我的布局显示接近我的预期。右侧容器中有四个,左侧有一个。但是,交易容器垂直滚动以适应四个交易高度。交易不会缩小以适合.layout-container 5-2-2 的正确容器。请问我如何缩小四个以固定容器高度?任何帮助将不胜感激。

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    试试这个

    body,
    html {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    .container {
      display: flex;
      min-height: 100vh;
      background: #2A3052;
    }
    .left {
      flex: 1;
      background: #9497A8;
      margin: 10px;
    }
    .right {
      flex: 1;
      display: flex;
      flex-direction: column;
    }
    .box {
      flex: 1;
      background: #9497A8;
      margin: 10px;
    }
    <div class="container">
      <div class="left"></div>
      <div class="right">
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
      </div>
    </div>

    【讨论】:

    • 感谢您的回复。我的问题是left,盒子是高度和大小相等的盒子/物品。因此,right 中的四个项目通过导致垂直滚动来扩展超出容器高度的高度。布局很好。但物品不合适。
    • 如果将内容添加到rightdiv 中的一个框,则left div 将匹配其高度,因此它们的高度将相等jsfiddle.net/Lg0wyt9u/659 或多个框jsfiddle.net/Lg0wyt9u/660
    • 它确实与高度匹配,但带有垂直滚动。我正在寻找一种将物品缩小到左侧并使它们适合容器的方法。尽管如此,谢谢你上面的代码非常有帮助。
    猜你喜欢
    • 2016-07-04
    • 2020-11-27
    • 2019-09-01
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 2021-09-06
    相关资源
    最近更新 更多