【问题标题】:flex-wrap not working in nested flex containerflex-wrap 在嵌套的 flex 容器中不起作用
【发布时间】:2017-07-16 00:40:25
【问题描述】:

在这个例子中,当屏幕宽度较小时,我希望蓝色和红色框可以换行,因为青色框设置为 flex-wrap。

但是,这并没有发生。为什么这不起作用?

代码如下:

.foo,
.foo1,
.foo3 {
  color: #333;
  text-align: center;
  padding: 1em;
  background: #ffea61;
  box-shadow: 0 0 0.5em #999;
  display: flex;
}

.foo1 {
  background: green;
  width: 200px;
  flex: 1 1 100%;
}

.foo2 {
  background: pink;
  display: flex;
  padding: 10px;
}

.foo3 {
  background: cyan;
  flex-wrap: wrap;
}

.bar1,
.bar2 {
  background: blue;
  width: 50px;
  height: 30px;
}

.bar2 {
  background: red;
}

.column {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  background: orange;
  height: 150px;
}
<div class="column">
  <div class="foo1"></div>
  <div class="foo">
    <div class="foo2">
      <div class="foo3">
        <div class="bar1"></div>
        <div class="bar2"></div>
      </div>
    </div>
  </div>
  <div class="foo1"></div>
</div>

详情请看以下链接:http://codepen.io/anon/pen/vxExjL

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    这可能是column wrap flex 容器的错误。青色框拒绝收缩,防止红色框包裹。

    您可以将宽度设置为第二列或.foo2.foo3,这将强制项目换行。但这可能不是你想要的。

    一种解决方法是在这种情况下不使用column wrap。坚持row nowrap

    .column {
      display: flex;
      background: orange;
      height: 150px;
    }
    
    .foo,
    .foo1,
    .foo3 {
      display: flex;
      justify-content: center;
      color: #333;
      text-align: center;
      padding: 1em;
      background: #ffea61;
      box-shadow: 0 0 0.5em #999;
    }
    
    .foo {
      flex: 1;
    }
    
    .foo1 {
      flex: 0 0 200px;
      background: green;
    }
    
    .foo2 {
      display: flex;
      background: pink;
      padding: 10px;
    }
    
    .foo3 {
      flex-wrap: wrap;
      margin: 0 auto;
      background: cyan;
    }
    
    .bar1,
    .bar2 {
      background: blue;
      width: 50px;
      height: 30px;
    }
    
    .bar2 {
      background: red;
    }
    <div class="column">
      <div class="foo1"></div>
      <div class="foo">
        <div class="foo2">
          <div class="foo3">
            <div class="bar1"></div>
            <div class="bar2"></div>
          </div>
        </div>
      </div>
      <div class="foo1"></div>
    </div>

    revised codepen

    【讨论】:

    • 感谢您的回答!这行得通。我接受了这个作为正确答案。我的实际问题比这更复杂,需要我使用 flex-dir: column。所以 flex-dir:row 解决方案在我的情况下不起作用。但这是一个很好的答案!谢谢!
    • @louis.luo,仅供参考,以防您还没有意识到:column wrap 独有的几个 flexbox 问题。我个人认为column wrap 还没有准备好迎接黄金时段。这也是我通常坚持使用基于row 的布局的原因之一。
    • 欲了解更多详情,请参阅:herehereherehere
    猜你喜欢
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 2021-08-27
    • 2017-12-03
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多