【问题标题】:How to wrap four columns into two rows of two columns with flexbox?如何用flexbox将四列包装成两行两列?
【发布时间】:2018-08-03 22:58:41
【问题描述】:

我们正在使用 flexbox 并尝试执行以下操作。目标是每个项目在移动断点上有一列,在平板电脑断点上有两列,在桌面断点上有四列。

该示例仅使用了四个项目,但假设我有 5 或 6 个项目,那么我只是希望这些项目具有响应性。如果该行只有足够的空间来显示每行 2 个项目,那么我希望每个项目继续移动到其上方的行下方。

如何做到这一点?

.flex-row {
  display: flex;
  @media screen and (min-width: 768px) {
    flex: 1;
    flex-direction: row;
    justify-content: space-between;
  }
}

.flex-col {
  margin: 6px;
  padding: 16px;
  background-color: black;
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1;
  flex-direction: column;
  color: white;
}
 <div class="flex-row">
    <div class="flex-col">Assertively negotiate interoperable portals without cross functional process improvements. Dramatically incentivize tactical best practices with.</div>
    <div class="flex-col">Seamlessly grow competitive.</div>
    <div class="flex-col">Distinctively optimize user-centric mindshare vis-a-vis plug-and-play infomediaries. Seamlessly optimize impactful solutions and enabled infrastructures.</div>
    <div class="flex-col">Dynamically extend flexible catalysts for change via pandemic supply chains. Efficiently.</div>
    </div>

当前结果

平板电脑上的预期结果

【问题讨论】:

  • 你能说明你是如何处理断点的吗?
  • @KoshVery 背景已换成黑色
  • @MatthewStevenson 我已经添加了包含媒体查询的代码。

标签: html css flexbox


【解决方案1】:

如果没有足够的空间,只需添加flex-wrap:wrap 以允许元素转到下一行,如果要控制何时发生中断,请考虑媒体查询:

.flex-row {
  display: flex;
  flex: 1;
  flex-direction: row;
  flex-wrap: wrap;
}

.flex-col {
  margin: 6px;
  padding: 16px;
  background-color: red;
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1;
  flex-direction: column;
  color: white;
  box-sizing:border-box;
}

@media (max-width:767px) {
  .flex-col {
    flex-basis: calc(50% - 12px);
  }
}

@media (max-width:460px) {
  .flex-col {
    flex-basis: 100%;
  }
}
<div class="flex-row">
  <div class="flex-col">Assertively negotiate interoperable portals without cross functional process improvements. Dramatically incentivize tactical best practices with.</div>
  <div class="flex-col">Seamlessly grow competitive.</div>
  <div class="flex-col">Distinctively optimize user-centric mindshare vis-a-vis plug-and-play infomediaries. Seamlessly optimize impactful solutions and enabled infrastructures.</div>
  <div class="flex-col">Dynamically extend flexible catalysts for change via pandemic supply chains. Efficiently.</div>
</div>

【讨论】:

  • 添加 flex-wrap:wrap 使它们都进入下一行
  • @drupi17 在小型设备上是的,但不是一直
  • 那是我的问题。在小型设备上,它通过让所有项目进入下一行来按预期工作,但在平板电脑上,我希望两行各有两个项目,但它们不起作用。
  • 出于某种原因,当我把它放在 jsfiddle 中时,它对我不起作用。 jsfiddle.net/7jn8hwbc/51
  • @drupi17 注意边距 flex-basis 不是 50%(根据我的回答更新)你需要删除边距
【解决方案2】:

尝试添加一个
或将前两个 div 元素放入另一个带有 display: 块的 div 中。然后将接下来的两个 div 元素放在另一个带有 display: 块的 div 中。

<div class="flex-row">
<div style="display: block;">
<div class="flex-col">
Assertively negotiate interoperable portals without cross functional process improvements. Dramatically incentivize tactical best practices with.
</div>
<div class="flex-col">
Seamlessly grow competitive.
</div>
</div>
<div style="display: block;">
<div class="flex-col">
Distinctively optimize user-centric mindshare vis-a-vis plug-and-play infomediaries. Seamlessly optimize impactful solutions and enabled infrastructures.
</div>
<div class="flex-col">
Dynamically extend flexible catalysts for change via pandemic supply chains. Efficiently.
</div>
</div>
</div>

它应该可以工作...

【讨论】:

  • 这可能有效,但我不相信这是真正的 flexbox。在某些情况下,flex-row 中可能有 5 或 6 个项目。我不能进入每一个都有
  • 如果您使用任何类型的 PHP 循环,它不会那么难。
  • 您是在制作静态网页吗?不会那么好。
  • 我们使用 Drupal,所以不是静态页面。
猜你喜欢
相关资源
最近更新 更多
热门标签