【问题标题】:Flexbox (flex-direction: column): wrapped child not positioned correctly when one child has fixed widthFlexbox(flex-direction:column):当一个孩子具有固定宽度时,包裹的孩子没有正确定位
【发布时间】:2019-07-31 00:59:30
【问题描述】:

我正在使用带有flex-flow: column wrap 的弹性框布局。我正在为 flexbox 容器设置一个高度。我想要实现的是让第一个子元素占据整个高度,以便下一个子元素将包裹到下一列。在我的情况下,这些孩子正在换行到下一列,但是他们的列和第一个孩子的列之间有很大的空间。似乎我设置第一个孩子的宽度这一事实以某种方式影响了被包裹的孩子的定位。

这是我试图实现的目标: 这是我遇到的问题:

这里是codepen的链接:codepen

.flex {
  display: flex;
  flex-flow: column wrap;
  height: 100px;
  border: 1px solid red;
}

.child1 {
  background-color: green;
  flex: 1 0 100%;
  max-width: 100px;
}

.child2 {
  flex: 1 0 20px;
  background-color: yellow;
}

.child3 {
  flex: 1 0 20px;
  background-color: blue;
}
<div class="flex">
  <div class="child1"></div>
  <div class="child2"></div>
  <div class="child3"></div>
</div>

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    我知道这是一个 Flexbox 问题,但我想将 CSS Grid 加入其中。它旨在处理x/y 布局方向。虽然flexbox 有时可以处理y 部分,但通常难度更大/黑客。

    .grid {
      display: grid;
      grid-template-columns: 100px 1fr;
      grid-template-rows: repeat(2, 1fr);
      height: 100px;
      border: 1px solid red;
    }
    
    .child1 {
      background-color: green;
      grid-row: 1/-1; /* Begin at 1 and go to the end */
    }
    
    .child2 {
      background-color: yellow;
    }
    
    .child3 {
      background-color: blue;
    }
    <div class="grid">
      <div class="child1"></div>
      <div class="child2"></div>
      <div class="child3"></div>
    </div>

    【讨论】:

    • 同意这一点。 Flexbox 并不是真正设计用于处理不同大小/方向的事物。您可以查看 W3 学校的示例以了解每个示例的使用方法:GridFlexbox
    • @Nick Yah,我非常喜欢 Flexbox,但发现自己过度使用它,因为我非常了解它。我学习 Grid 的越多,我就越意识到每件事都有时间和地点。
    • @AndyHoffman 我已经读到网格仍然缺乏浏览器支持。另外,IE11似乎对grid的支持不好。你怎么看?我应该使用它还是应该暂时避免它?
    • @yankolo 浏览器支持(无前缀)是above 85%。使用预处理器,您仍然可以在 IE/10+ 中使用 Grid。甚至 Opera Mini 也能理解 Grid。我建议今天使用它。
    【解决方案2】:

    您需要为元素设置显式宽度。 Acutally 默认情况下它们会被拉伸。

    如果您删除max-width,您将拥有以下内容。您的元素被拉伸的两个 eqaul 列。

    .flex {
      display: flex;
      flex-flow: column wrap;
      height: 100px;
      border: 1px solid red;
    }
    
    .child1 {
      background-color: green;
      flex: 1 0 100%;
    }
    
    .child2 {
      flex: 1 0 20px;
      background-color: yellow;
    }
    
    .child3 {
      flex: 1 0 20px;
      background-color: blue;
    }
    <div class="flex">
      <div class="child1"></div>
      <div class="child2"></div>
      <div class="child3"></div>
    </div>

    添加max-width 只会限制其列中第一个元素的宽度

    .flex {
      display: flex;
      flex-flow: column wrap;
      height: 100px;
      border: 1px solid red;
    }
    
    .child1 {
      background-color: green;
      flex: 1 0 100%;
      max-width:100px;
    }
    
    .child2 {
      flex: 1 0 20px;
      background-color: yellow;
    }
    
    .child3 {
      flex: 1 0 20px;
      background-color: blue;
    }
    <div class="flex">
      <div class="child1"></div>
      <div class="child2"></div>
      <div class="child3"></div>
    </div>

    如果你改变对齐方式,你会得到这个:

    .flex {
      display: flex;
      flex-flow: column wrap;
      height: 100px;
      border: 1px solid red;
      align-items:flex-start;
    }
    
    .child1 {
      background-color: green;
      flex: 1 0 100%;
    }
    
    .child2 {
      flex: 1 0 20px;
      background-color: yellow;
    }
    
    .child3 {
      flex: 1 0 20px;
      background-color: blue;
    }
    <div class="flex">
      <div class="child1"></div>
      <div class="child2"></div>
      <div class="child3"></div>
    </div>

    您将什么也看不到,因为我们删除了拉伸对齐并且没有定义宽度。

    添加一些宽度以获得所需的结果:

    .flex {
      display: flex;
      flex-flow: column wrap;
      height: 100px;
      border: 1px solid red;
      align-items:flex-start;
    }
    
    .child1 {
      background-color: green;
      flex: 1 0 100%;
      width:20%;
    }
    
    .child2 {
      flex: 1 0 20px;
      background-color: yellow;
      width:80%;
    }
    
    .child3 {
      flex: 1 0 20px;
      background-color: blue;
      width:80%;
    }
    <div class="flex">
      <div class="child1"></div>
      <div class="child2"></div>
      <div class="child3"></div>
    </div>

    【讨论】:

      猜你喜欢
      • 2021-10-16
      • 1970-01-01
      • 2017-12-12
      • 2016-03-13
      • 2022-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多