【问题标题】:Flex-wrap is not wrapping when I reduce the window size当我减小窗口大小时,Flex-wrap 不换行
【发布时间】:2017-10-23 10:31:43
【问题描述】:

我已经设置了一系列三个容器并将display: flex;flex-wrap: wrap; 应用到它们,但是当我减小窗口大小时它们没有换行?

我已将代码放在下面,但在找到问题的根源方面似乎一无所获。

body {
  font-family: arial;
}

p {
  color: white;
}

.container {
  background-color: #666;
  width: 800px;
  height: 200px;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.item {
  padding: 10px;
  box-sizing: border-box;
}

.item1 {
  flex: 1;
  background-color: red;
}

.item2 {
  flex: 1;
  background-color: blue;
}

.item3 {
  flex: 1;
  background-color: green;
}
<div class="container">

  <div class="item item1">
    <h1>ITEM1</h1>
    <p>flex: 1</p>
  </div>

  <div class="item item2">
    <h1>ITEM2</h1>
    <p>flex: 1</p>
  </div>

  <div class="item item3">
    <h1>ITEM3</h1>
    <p>flex: 1</p>
  </div>

</div>

【问题讨论】:

  • 为什么要换行 - 您没有为您的子项目指定宽度,内容的大小意味着它们都可以放在一行中,但是当您将 flex 设置为 1 时,这就说明了当您将每个项目的 flex-basis 设置为相同时,适合该行的项目
  • 另外,由于您的容器是固定宽度,屏幕大小不会影响它是否换行

标签: html css flexbox


【解决方案1】:

这就是为什么项目没有包装:

你有一个弹性容器设置为width: 800px

容器有三个弹性项目设置为flex: 1,这是以下的简写:

  • flex-grow: 1
  • flex-shrink: 1
  • flex-basis: 0

这意味着每个项目的实际宽度为0 (flex-basis: 0),每个项目的大小是根据行上的可用空间(flex-grow: 1)。

因此,实际上,您已将每个项目的大小调整为行上空间的三分之一,无论是多少。因此,每个项目都可以缩小到width: 0,并且它们永远不会换行。

如果您将内容和/或width 和/或flex-basis 添加到一个或多个项目,并且项目增长到超过 800 像素(容器的宽度),那么您的弹性项目将换行。

但请注意,它们不会根据您重新调整浏览器窗口的大小而换行,因为容器不占用视口的width: 100%。它们只会根据容器的宽度进行换行。

body {
  font-family: arial;
}

p {
  color: white;
}

.container {
  background-color: #666;
  width: 800px;
  height: 200px;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.item {
  padding: 10px;
  box-sizing: border-box;
}

.item1 {
  flex: 1 0 250px;
  background-color: red;
}

.item2 {
  flex: 1 0 250px;
  background-color: blue;
}

.item3 {
  flex: 1 0 400px;
  background-color: green;
}
<div class="container">

  <div class="item item1">
    <h1>ITEM1</h1>
    <p>flex: 1 0 250px</p>
  </div>

  <div class="item item2">
    <h1>ITEM2</h1>
    <p>flex: 1 0 250px</p>
  </div>

  <div class="item item3">
    <h1>ITEM3</h1>
    <p>flex: 1 0 400px</p>
  </div>

</div>

【讨论】:

    【解决方案2】:

    您需要在容器上使用max-width 而不是width,您必须允许容器收缩才能包裹物品。

    body {
      font-family: arial;
    }
    
    p {
      color: white;
    }
    
    .container {
      background-color: #666;
      max-width: 800px;
      height: 200px;
      margin: 0 auto;
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
    }
    
    .item {
      padding: 10px;
      box-sizing: border-box;
    }
    
    .item1 {
      flex: 1;
      background-color: red;
    }
    
    .item2 {
      flex: 1;
      background-color: blue;
    }
    
    .item3 {
      flex: 1;
      background-color: green;
    }
    <div class="container">
    
      <div class="item item1">
        <h1>ITEM1</h1>
        <p>flex: 1</p>
      </div>
    
      <div class="item item2">
        <h1>ITEM2</h1>
        <p>flex: 1</p>
      </div>
    
      <div class="item item3">
        <h1>ITEM3</h1>
        <p>flex: 1</p>
      </div>
    
    </div>

    【讨论】:

    • 我调整了窗口大小,所有内容都保持在同一行...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    相关资源
    最近更新 更多