防止flexbox收缩

Sim*_*ias 43 css css3 flexbox

我正在使用flexbox来布局页面,因为增长的行为很有用.但我想完全避免萎缩的行为.

无论如何要管理这个?

示例代码:

<div class="flex-vertical-container">
    <div class="flex-box">
         This one should grow but not shrink
    </div>
    <div></div>
    <div></div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS

.flex-vertical-container {
    display: flex;
    flex-direction: column;
}

.flex-box {
    flex: 1;
}
Run Code Online (Sandbox Code Playgroud)

zgo*_*ood 77

尝试将flex-shrink属性设置0.flex-box.

  • 他们可能意味着`flex:1 0 auto;` (4认同)
  • 弹性收缩:1 0 自动;似乎不是有效的财产。 (2认同)
  • 在这种特定情况下,将 flex-shrink 属性设置为 0 似乎是不够的。使用“flex-direction:column”时,您还需要设置“min-width”值来完成此目标。 (2认同)

fir*_*r_1 24

添加一个min-width您想要的最小可能值的框.Flexbox不会缩小最小宽度以下的宽度.


Eri*_*dán 6

您可以尝试为孩子申请:

.flex-box{
    width: max-content;
}
Run Code Online (Sandbox Code Playgroud)

最后结果:

.flex-vertical-container{
  display: flex;
  flex-direction: column;
}
.flex-vertical-container div{
  background: gray;
}
.flex-box{
  width: max-content;
}
Run Code Online (Sandbox Code Playgroud)
<div class="flex-vertical-container">
    <div class="flex-box">
         This one should grow but not shrink
    </div>
    <div>This is shrinking</div>
    <div>This is shrinking</div>
</div>
Run Code Online (Sandbox Code Playgroud)