【问题标题】:Make flex items stack next to each other使弹性项目彼此相邻堆叠
【发布时间】:2018-04-15 14:03:54
【问题描述】:

我有 10 个 div:2 个隐藏,8 个堆叠在一起。

使用媒体查询,在调整屏幕大小时,我可以显示 2 个隐藏的 div。

所以,现在我在底部有 4 个红色 div,但我希望它们成对出现 - 2 行,2 个红色 div 彼此相邻。

我该怎么做?

html {
  font-size: 20px;
}

.box {
  color: white;
  font-size: 100px;
  text-align: center;
  text-shadow: 4px 4px 0 rgba(0, 0, 0, 0.1);
  padding: 10px;
  Margin: 5px;
  /*  width: calc(33.33% - 10px);*/
}


/* Flexbox code starts here */

.container {
  display: flex;
  /*Must!!!! on the container, in order to turn it to flex*/
  flex-direction: column;
  flex-wrap: wrap;
  justify-content: center;
}


/* Colors for each box */

.blue {
  background: blue;
}

.orange {
  background: Orange;
  height: 300px;
}

.green {
  background: green;
}

.red {
  background: red;
  height: 170px;
}

.hide-reds {
  display: none;
}


/*Media Queries for Different Screen Sizes*/

@media all and (min-width: 800px) {
  .red {
    display: block;
  }
}
<div class="container">
  <div class="box blue">Blue</div>
  <div class="box blue">Blue</div>
  <div class="box orange">Orange</div>
  <div class="box green">Green</div>
  <div class="box green">Green</div>
  <div class="box green">Green</div>
  <div class="box red">Red</div>
  <div class="box red">Red</div>
  <div class="box red hide-reds">Red</div>
  <div class="box red hide-reds">Red</div>
</div>

【问题讨论】:

  • 一切正常。

标签: html css flexbox media-queries


【解决方案1】:

您的容器有flex-direction: column。您的布局有一列。但是没有办法将弹性项目彼此相邻包装在一个列中。 Flexbox doesn't work that way.

但是,您的布局可以使用flex-direction: rowflex-wrap: wrap。通过给每个项目width: 100%,每个项目强制下一个项目到下一行。这将创建单列堆叠项目。然后,给最后四个项目width: 50%,所以每行两个。

.container {
  display: flex;
  flex-wrap: wrap;
}

.box {
  flex: 0 0 100%;
}

.red {
  flex: 1 0 100%;
  background: red;
  height: 170px;
}

.hide-reds {
  display: none;
}

@media all and (min-width: 800px) {
  .red {
    flex-basis: 40%; /* see note below */
    display: block;
  }
}


/* not relevant to the problem */
.blue {
  background: blue;
}

.orange {
  background: Orange;
  height: 300px;
}

.green {
  background: green;
}

html {
  font-size: 20px;
}

.box {
  color: white;
  font-size: 100px;
  text-align: center;
  text-shadow: 4px 4px 0 rgba(0, 0, 0, 0.1);
  padding: 10px;
  margin: 5px;
}
<div class="container">
  <div class="box blue">Blue</div>
  <div class="box blue">Blue</div>
  <div class="box orange">Orange</div>
  <div class="box green">Green</div>
  <div class="box green">Green</div>
  <div class="box green">Green</div>
  <div class="box red">Red</div>
  <div class="box red">Red</div>
  <div class="box red hide-reds">Red</div>
  <div class="box red hide-reds">Red</div>
</div>

jsFiddle demo

关于flex-basis: 40%的注意事项:

flex 简写中定义flex-grow: 1(请参阅.red)后,flex-basis 无需为 50%,由于边距,这实际上会导致每行一个项目(请参阅@987654335 @)。

由于flex-grow 将占用行上的可用空间,flex-basis 只需大到足以强制执行换行。在这种情况下,使用flex-basis: 40%,有足够的空间放置边距,但没有足够的空间放置第三个项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    相关资源
    最近更新 更多