简体   繁体   English

调整弹性项目以适应容器(防止项目溢出)

[英]Resize flex items to fit container (prevent items from overflowing)

I've been struggling with an issue that i think will have a pretty simple solution, but i just can't see it right now.我一直在为一个我认为会有一个非常简单的解决方案的问题而苦苦挣扎,但我现在看不到它。 I have a nested flexbox layout.我有一个嵌套的 flexbox 布局。 The first level of flex items are divs that are display:flex themselves, and depending on class they can have a flex-directon: of row or column.第一层 flex 项目是 div,它们是 display:flex 本身,根据类,它们可以有一个 flex-directon: 行或列。 Both the first and second level of these nested flex items should grow to fill up the remaining parent size, and shrink if new elements are added, never overflowing.这些嵌套的 flex 项的第一级和第二级都应该增长以填充剩余的父级大小,如果添加新元素则收缩,永远不会溢出。

My Problem: When adding new flex items to the "first level", this seems to work fine.我的问题:将新的 flex 项添加到“第一级”时,这似乎工作正常。 However, adding new flex items to the second level seems to always overflow the parent container height.但是,将新的 flex 项添加到第二级似乎总是会溢出父容器的高度。

As i'm not really good at describing stuff like this, here's a picture of what i need: The purple box is the parent container (not flex) The orange box is the flex container with flex-direction: row, with the red lines being its flex items (each item is a flexbox as well).由于我不太擅长描述这样的东西,这是我需要的图片:紫色框是父容器(不是 flex)橙色框是带有 flex-direction: row 的 flex 容器,带有红线作为它的弹性项目(每个项目也是一个弹性盒)。 The blue lines are the nested flex items.蓝线是嵌套的弹性项目。

Nested Flexbox layout嵌套 Flexbox 布局

Adding new red items works and expands or shrinks as needed, but adding new blue items, overflows the container.添加新的红色项目可以根据需要扩展或缩小,但添加新的蓝色项目会溢出容器。 https://jsfiddle.net/t40x7or8/ https://jsfiddle.net/t40x7or8/

Here's my code sample: CSS这是我的代码示例:CSS

  width: 1800px;
  height: 500px;
  border: 4px blue solid;
}

.flex-container {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
}

.flex-item {
  flex-grow: 1;
  flex-basis: 0;

  display: flex;
}

.flex-item > div {
  flex-grow: 1;
  border: 1px solid orange;
}

.height-1 {
  flex-direction: column;
  max-height: 100%;
}

.height-2 {
  max-height: 100%;
}

img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

HTML HTML

  <div class="flex-container">

    <div class="height-2 flex-item">
      <div class="img-div">
        <img src="https://picsum.photos/1080/600" />
      </div>
    </div>

    <div class="height-1 flex-item">
      <div class="img-div">
        <img src="https://picsum.photos/1080/601" />
      </div>
      <div class="img-div">
        <img src="https://picsum.photos/1080/602" />
      </div>

    </div>
    </div>
  </div>

Sorry for the chaotic description, i'm quite bad at these kind of things.对不起,混乱的描述,我对这些事情很不擅长。 Thanks for the help谢谢您的帮助

I think we should use flex: 1 1 0 for the fix the layout of the flex items.我认为我们应该使用flex: 1 1 0来修复 flex 项目的布局。

And using width: 100% to control the size of the inner image.并使用width: 100%来控制内部图像的大小。

https://jsfiddle.net/ramseyfeng/kxf46bju/ https://jsfiddle.net/ramseyfeng/kxf46bju/

 .page { border: 3px solid green; width: 600px; height: 300px; } .flex { display: flex; } .cols { display: flex; flex-direction: row; } .rows { display: flex; flex-direction: column; } .flex-1-1-0 { flex: 1 1 0; } .img-div { flex: 1 1 0; display: flex; } .img-div img { width: 100%; }
 <div class="page cols"> <div class="flex-1-1-0 rows"> <div class="img-div"> <img src="https://picsum.photos/1080/600" /> </div> </div> <div class="flex-1-1-0 rows"> <div class="img-div"> <img src="https://picsum.photos/1080/601" /> </div> <div class="img-div"> <img src="https://picsum.photos/1080/602" /> </div> </div> <div class="flex-1-1-0 rows"> <div class="img-div"> <img src="https://picsum.photos/1080/607" /> </div> <div class="img-div"> <img src="https://picsum.photos/1080/608" /> </div> </div> <div class="flex-1-1-0 rows"> <div class="img-div"> <img src="https://picsum.photos/1080/605" /> </div> <div class="img-div"> <img src="https://picsum.photos/2000/1000" /> </div> <div class="img-div"> <img src="https://picsum.photos/2000/1001" /> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM