简体   繁体   English

最小宽度渲染在flex-direction:行和flex-direction:列上有所不同

[英]min-width rendering differently in flex-direction: row and flex-direction: column

I have a container with display: flex and flex-direction: row . 我有一个带有display: flexflex-direction: row的容器。

In that container there is a sub-container also with display: flex but with flex-direction: column . 在该容器中,还有一个子容器,其display: flex但其flex-direction: column

The problem is if I add an input in the sub-container, the min-width of that input will be ignored. 问题是如果我在子容器中添加输入,则该输入的min-width将被忽略。

This is the code where I tried several cases of input in flexbox: 这是我在flexbox中尝试几种输入情况的代码:

 form { margin: 100px; } div.flex_ctn { display: flex; } input { flex: 1; min-width: 40px; } div.column { flex-direction: column; } div.row { flex-direction: row; } div.sub_ctn { flex: 1; /*min-width:40px;*/ } 
 <form> <div class="flex_ctn row"> <input /> </div> <div class="flex_ctn column"> <input /> </div> <div class="flex_ctn row"> <div class="flex_ctn column sub_ctn"> <input /> </div> </div> <div class="flex_ctn column"> <div class="flex_ctn row sub_ctn"> <input /> </div> </div> </form> 

https://fiddle.jshell.net/s3gu32ku/2/ https://fiddle.jshell.net/s3gu32ku/2/

If you reduce the screen size, the 3rd line doesn't react like the others. 如果减小屏幕尺寸,第三行不会像其他行那样反应。

In the css you will see that the last line is set as comment. 在CSS中,您将看到最后一行设置为注释。 When that part is enabled you just have to reload and the issue disappears. 启用该部分后,您只需重新加载,问题就会消失。 So, perfect ! 那么完美 ! I have got the solution! 我有解决方案!

But that bothers me to use something that I don't understand ^^. 但这困扰着我使用我不理解的东西^^。

This would be great if someone can explain to me why that error occurs, why that line fix it, and also if there a better way to avoid that issue. 如果有人可以向我解释该错误的原因,该行的修复原因以及是否有避免该问题的更好方法,那就太好了。

Generally speaking, flex items, by default, cannot be smaller than the size of their content. 一般来说,弹性项目默认情况下不能小于其内容的大小。

More specifically, these are initial settings of flex items: 更具体地说,这些是弹性项目的初始设置:

  • min-width: auto (applies in flex-direction: row ) min-width: auto (应用于flex-direction: row
  • min-height: auto (applies in flex-direction: column ) min-height: auto (应用于flex-direction: column

Even more specifically, take a look at the spec language: 更具体地说,请看一下规范语言:

4.5. 4.5。 Implied Minimum Size of Flex Items 弹性项目的隐含最小大小

To provide a more reasonable default minimum size for flex items, this specification introduces a new auto value as the initial value of the min-width and min-height properties defined in CSS 2.1. 为了为弹性商品提供更合理的默认最小尺寸,此规范引入了一个新的auto值作为CSS 2.1中定义的min-widthmin-height属性的初始值。

auto

On a flex item whose overflow is visible in the main axis, when specified on the flex item's main-axis min-size property, specifies an automatic minimum size. 在其主轴上visible overflow的弹性项目上,当在弹性项目的主轴min-size属性中指定时,指定自动最小尺寸。 It otherwise computes to 0 . 否则计算为0

In other words, the minimum sizing algorithm applies only on the main axis . 换句话说,最小尺寸算法仅适用于主轴

Your input elements in column-direction containers don't get min-width: autobecause the main axis is vertical in those cases – so they shrink and won't overflow the container. 您在列方向容器中的输入元素不会达到min-width: auto - 因为在这种情况下主轴是垂直的 -因此它们会收缩并且不会溢出容器。 You can see this behavior play out on your second input element. 您可以在第二个输入元素上看到这种行为。 Reduce the screen size while viewing this demo . 观看此演示时,请减小屏幕尺寸。

The same thing happens with the third input, which is a child of a nested flex container with flex-direction: column ... EXCEPT , this column-direction container is also a flex item of larger container with flex-direction: row . 第三个输入发生相同的事情,它是具有flex-direction: column ... EXCEPT的嵌套flex容器的子代,此列方向容器也是具有flex-direction: row的较大容器的flex项目。

This means the main axis of the nested container is horizontal and min-width: auto applies. 这意味着嵌套容器的主轴是水平且min-width: auto应用。 As a result, this flex item will not shrink below the intrinsic width of the input. 结果,该弹性项目将不会收缩到输入的固有宽度以下。 For an illustration, see the same demo from above. 有关示例,请从上方查看相同的演示

Therefore, you need to override this default with min-width: 0 or overflow: hidden ( demo ). 因此,您需要使用min-width: 0overflow: hiddendemo )覆盖此默认值。

And for the reasons explained above, the fourth input, contained in a nested row-direction flex container, will also need to have min-width: auto overridden ( demo ). 出于上述原因,嵌套行方向flex容器中包含的第四个输入也将需要具有min-width: auto覆盖( demo )。

Related: Why doesn't flex item shrink past content size? 相关: 为什么弹性项目不能缩小到超过内容大小?

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

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