CSS网格在包装后具有自动流动的最小列数 [英] CSS Grid to have minimum number of columns possible with auto flow after wrapping

查看:56
本文介绍了CSS网格在包装后具有自动流动的最小列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用具有自动流程的CSS网格,以使包装后的列数最少?

Is it possible to have a CSS grid with auto flow that minimises the number of columns after wrapping? How?

我有一个包含6个可自动放置项目的网格。一旦不是所有6个元素都适合放在一行中,我希望有3列和2行,而不是5列和1行,而第二个元素只有一个元素。

I have a grid with 6 items with auto-placement. Once not all 6 of them fit in one row, I'd like to have 3 columns and 2 rows, instead of 5 columns and 1 row, with only one element on the second one.

如果可能的话,我想避免媒体查询。

I'd like to avoid media queries, if possible.

我所要表达的视觉效果d喜欢这样做:

A visual representation of what I'd like to do:

1)如果所有6个项目都适合一行,则将它们显示在一行中。

1) If all 6 items fit in one row, show them in one row.

| OOO OOO OOO OOO OOO OOO | 
| O1O O2O O3O O4O O5O O6O |
| OOO OOO OOO OOO OOO OOO |

2)如果一行中少于6个元素,则分两行显示,每行3个项目。

2) If fewer than 6 elements fit in one row, show them in 2 rows with 3 items each.

| OOO OOO OOO         | 
| O1O O2O O3O         | 
| OOO OOO OOO         | 
|                     | 
| OOO OOO OOO         | 
| O4O O5O O6O         | 
| OOO OOO OOO         | 

3)如果一行中少于3个元素,则将它们显示在3行中,每行2个项目。

3) If fewer than 3 elements fit in one row, show them in 3 rows with 2 items each.

| OOO OOO   | 
| O1O O2O   | 
| OOO OOO   | 
|           | 
| OOO OOO   | 
| O3O O4O   | 
| OOO OOO   | 
|           | 
| OOO OOO   | 
| O5O O6O   | 
| OOO OOO   | 






这就是我到目前为止所得到的。减小宽度,直到最后一列换行,这时我希望有3列,每列三项,而不是一列5项,另一列1。


Here's what I got so far. Reduce width until the last column wraps, at that point I'd like to have 3 columns with three items each instead of one with 5 and another with 1.

.grid {
  padding: 10px;
  border: 2px solid #444;

  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  gap: 10px;
  justify-content: space-evenly;
}

.item {
  height: 50px;
  background: #5a5a;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2em;
}

<div class="grid">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
  <div class="item">4</div>
  <div class="item">5</div>
  <div class="item">6</div>
</div>

推荐答案

编辑:请注意,在OP将他们的代码添加到问题之前,我在之前写了答案。但是,我仍然认为, @media 查询是解决此问题所必需的,与 div 的方法无关显示元素。


Please note that I wrote the answer before the OP added their code to the question. However, I still think that @media queries are needed to solve this problem, independent of the method in which the div elements are displayed.

我在下面使用 br 标记和 @media 查询进行了演示。

I've made a demonstration below using a br tag and @media queries.

运行该代码段,然后按整页进行测试:

Run the snippet and then press 'Full Page' to test it:

.grid-box {
  display: inline-block;
  width: 200px;
  height: 150px;
  background-color: blue;
  margin: 2px;
}

.row3 {
  display: none;
}

@media (max-width: 1260px) {
  .row3 {
    display: block;
  }
}

@media (max-width: 636px) {
  .row3 {
    display: none;
  }
}

<div class="grid-box"></div>
<div class="grid-box"></div>
<div class="grid-box"></div>
<br class="row3">
<div class="grid-box"></div>
<div class="grid-box"></div>
<div class="grid-box"></div>

这篇关于CSS网格在包装后具有自动流动的最小列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆