html - 最小宽度在 flex-direction : row and flex-direction: column 上呈现不同

标签 html css flexbox

我有一个带有 display: flexflex-direction: row 的容器。

在该容器中有一个子容器也带有 display: flex 但带有 flex-direction: column

问题是如果我在子容器中添加一个输入,该输入的 min-width 将被忽略。

这是我在 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/

如果您减小屏幕尺寸,第 3 行不会像其他行那样使用react。

在 css 中你会看到最后一行被设置为注释。启用该部分后,您只需重新加载,问题就会消失。很完美 !我找到了解决方案!

但是使用我不理解的东西让我很困扰 ^^。

如果有人可以向我解释为什么会出现该错误,为什么该行修复它,以及是否有更好的方法来避免该问题,那就太好了。

最佳答案

一般来说, flex 元素默认情况下不能小于其内容的大小。

更具体地说,这些是 flex 元素的初始设置:

  • min-width: auto(适用于flex-direction: row)
  • min-height: auto(适用于flex-direction: column)

更具体地说,看看规范语言:

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

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. It otherwise computes to 0.

换句话说,最小尺寸算法仅适用于主轴

您在列方向容器中的输入元素未获得 min-width: autobecause the main axis is vertical在那些情况下——所以它们会收缩并且不会溢出容器。您可以在第二个输入元素上看到这种行为。查看此 demo 时缩小屏幕尺寸.

同样的事情发生在第三个输入上,它是具有 flex-direction: column 的嵌套 flex 容器的子容器... EXCEPT,这个列方向容器也是一个 flex item of larger container with flex-direction: row

这意味着嵌套容器的主轴是水平的并且 min-width: auto 适用。因此,这个 flex 元素不会缩小到输入的固有宽度以下。有关插图,请参阅相同的 demo从上面看。

因此,您需要使用 min-width: 0overflow: hidden ( demo ) 覆盖此默认值。

并且出于上述原因,包含在嵌套行方向 flex 容器中的第四个输入也需要覆盖 min-width: auto ( demo )。

相关:Why doesn't flex item shrink past content size?

关于html - 最小宽度在 flex-direction : row and flex-direction: column 上呈现不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40407619/

相关文章:

javascript - 检索新页面时jquery mobile与函数冲突

css - Flexbox 垂直内容与 Internet Explorer 中的溢出对齐

html - Flexbox 会创建大量空白并向下移动它所影响的对象

javascript - 如何使用div名称打开显示 block ?

javascript - 使用 HTML5 进行打印设计

javascript - 如何在通过模板文字创建的元素中添加事件监听器?

php - Zend Framework 中的引导模块特定样式/脚本

html - 使用 flexbox 时增加宽度而不是重叠

php - 检查多行 - PHP 和 MySQL

Flash - 是否有任何控件可以在其中呈现 html 5?