简体   繁体   English

如何防止Flex项在没有包装的情况下溢出Flex父级?

[英]How to prevent flex-items from overflowing flex parent with no wrap?

I have a flexbox with flex-direction: row . 我有一个带flex-direction: row的flexbox flex-direction: row The child items have a min-width set. 子项设置了min-width When the window is shrunk past the point where the min-width is reached, the items begin to overflow the flex container. 当窗口缩小到达到min-width点时,项目开始溢出flex容器。

 .parent { display: flex; flex-direction: row; background-color: red; } .child { min-width: 100px; flex-basis: 0px; flex-grow: 1; margin: 5px; height: 100px; background-color: blue; } 
 <div class="parent"> <div class="child"></div> <div class="child"></div> <div class="child"></div> </div> 

https://jsfiddle.net/4t8029q8/ https://jsfiddle.net/4t8029q8/

Is there any way to force the container to stretch to contain the items without setting an explicit min-width on the parent? 有什么方法可以强制容器拉伸以包含项目,而无需在父min-width上设置显式的min-width Doing so provides the behavior I am trying to achieve, but is too rigid to accomodate a variable number of items. 这样做可以提供我要实现的行为,但过于僵化,无法容纳可变数量的项目。

 .parent { display: flex; flex-direction: row; background-color: red; min-width: 330px } .child { min-width: 100px; flex-basis: 0px; flex-grow: 1; margin: 5px; height: 100px; background-color: blue; } 
 <div class="parent"> <div class="child"></div> <div class="child"></div> <div class="child"></div> </div> 

https://jsfiddle.net/4t8029q8/1/ https://jsfiddle.net/4t8029q8/1/

This is what I want, for the flex-items to never overflow the flex container even if it causes the page to need a horizontal scroll. 这就是我想要的,即使flex-items导致页面需要水平滚动,它也不会溢出flex容器。

NOTE: I have other more complicated logic requiring flex-basis: 0px and flex-grow: 1 , so those lines cannot be removed. 注意:我还有其他更复杂的逻辑,需要flex-basis: 0pxflex-grow: 1 ,所以这些行不能删除。

Set display: inline-flex on the .parent class to change it to an inline element. .parent类上设置display: inline-flex以将其更改为内联元素。 This will also force the .parent to expand to contain its children. 这也将迫使.parent扩展以容纳其子级。 Then by setting min-width: 100% on the .parent class, it will force it to expand to 100% of the containing element. 然后通过在.parent类上设置min-width: 100% ,它将强制其扩展到包含元素的100%。

.parent {
  display: inline-flex;
  flex-direction: row;
  background-color: red;
  min-width: 100%;
}
.child {
  min-width: 100px;
  flex-basis: 0px;
  flex-grow: 1;

  margin: 5px;
  height: 100px;
  background-color: blue;
}

You're forcing your child elements to have a specific width, much like @Michael_B mentioned, this essentially creates a static environment where they will remain the set width regardless of the parent. 您要强制子元素具有特定的宽度,就像@Michael_B提到的那样,这实际上会创建一个静态环境,无论父元素如何,它们都将保持设置的宽度。

Essentially at this point, since you know the min-width of your children elements, I would create a media query, at the specific width requirement, which in this case is 100px. 基本上在这一点上,由于您知道子元素的最小宽度,因此我将按照特定的宽度要求(在本例中为100px)创建一个媒体查询。 Once your screen reaches said size, force your parent to have wrapping items by adding flex-wrap: wrap; 一旦屏幕达到上述大小,就可以通过添加flex-wrap: wrap;来迫使您的父母包装物品flex-wrap: wrap; to your parent. 给你的父母。 When you're outside the said width, be sure to set your parent's CSS back to not allow wrapping, flex-wrap: nowrap; 当您不在上述宽度范围内时,请务必将父级的CSS设置回不允许包装,请使用flex-wrap: nowrap; . This will allow your child items to wrap, and your parent will create a horizontal to the page. 这将允许您的子项包装,而您的父项将创建页面的水平线。

you can use overflow-y: auto on the parent container, if scrolling doesn't matter you. 如果滚动无关紧要,您可以在parent容器上使用overflow-y: auto

 .parent { display: flex; flex-direction: row; overflow-y: auto;/*add this.*/ background-color: red; } .child { min-width: 100px; flex-basis: 0px; flex-grow: 1; margin: 5px; height: 100px; background-color: blue; } 
 <div class="parent"> <div class="child"></div> <div class="child"></div> <div class="child"></div> </div> 

You're asking for the container to re-size fluidly in order to prevent the child elements from overflowing. 您要求容器流畅地调整大小,以防止子元素溢出。 This is dynamic behavior, which is the realm of JavaScript, not HTML or CSS. 这是动态行为,它是JavaScript的领域,而不是HTML或CSS。

Because the HTML and CSS rendering engines do not automatically re-flow the source documents when child elements overflow their container, there is no way for the container to know when the children have overflowed and, therefore, expand. 因为当子元素溢出其容器时,HTML和CSS呈现引擎不会自动重排源文档,所以容器无法知道子溢出的时间,因此无法扩展。 You're in a static environment. 您处于静态环境中。 The concept applies to wrapping, as well. 该概念也适用于包装。

Here's a more detailed explanation (that covers wrapping behavior, but applies to overflow, as well): 这是更详细的说明(涵盖了包装行为,但也适用于溢出):

You'll need a script or possibly media queries to make your layout work as desired. 您将需要脚本或可能的媒体查询,以使布局按需工作。

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

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