因此,我有一个网格的网格,如下所示。基本上,其布局的方式如下:
[1] [ Item 1 ] [2] [ Item 2 ]
[3] [ Item 3 ] [4] [ Item 4 ]
一旦屏幕通过媒体查询缩小到移动设备的范围,它就会改变这一点:
[1] [ Item 1 ]
[2] [ Item 2 ]
[3] [ Item 3 ]
[4] [ Item 4 ]
这是完美的,这就是我所需要的。我的问题是,在第一个示例中,我需要像这样进行:
[1] [ Item 1 ] [3] [ Item 3 ]
[2] [ Item 2 ] [4] [ Item 4 ]
现在,在我的实际代码中,有更多的网格项目,所以我基本上希望它布置第一列,然后是第二列,而不是填充行行。我可以实际更改HTML中网格项目的顺序,但是在移动屏幕上,订单弄乱了。我看了看网格 - 自动流量
,我无法弄清楚这是否是我需要的。解决方案可能是如此简单,但我认为我与这个项目非常接近,很难退后一步并弄清楚它。任何帮助将不胜感激!
.grid {
display: grid;
grid-template-columns: 48px 1fr 48px 1fr;
column-gap: 12px;
align-items: center;
justify-content: center;
background-color: orange;
border: 1px solid black;
}
.grid-item {
padding: 16px;
border: 1px solid black;
font-size: 12px;
background-color: aliceblue;
}
<section class="grid">
<div class="grid-item">1</div>
<div class="grid-item">Item 1</div>
<div class="grid-item">2</div>
<div class="grid-item">Item 2</div>
<div class="grid-item">3</div>
<div class="grid-item">Item 3</div>
<div class="grid-item">4</div>
<div class="grid-item">Item 4</div>
</section>
So I have a grid for a section of a page as you can see below. Basically, the way its laid out is as follows:
[1] [ Item 1 ] [2] [ Item 2 ]
[3] [ Item 3 ] [4] [ Item 4 ]
And once the screen narrows to mobile via media queries it changes to this:
[1] [ Item 1 ]
[2] [ Item 2 ]
[3] [ Item 3 ]
[4] [ Item 4 ]
Which is perfect, that's what I need. My issue is that in the first example, I need it to go like this:
[1] [ Item 1 ] [3] [ Item 3 ]
[2] [ Item 2 ] [4] [ Item 4 ]
Now in my actual code, there are a lot more grid-items so basically I would like it to lay out the first column and then the second instead of filling row by row. I could physically change the order of the grid items in the HTML but then on mobile screens the order is messed up. I took a look at grid-auto-flow
and I could not figure out if that's what I needed. The solution is probably so simple but I think I am so close to this project it's hard to take a step back and figure it out. Any help is greatly appreciated!
.grid {
display: grid;
grid-template-columns: 48px 1fr 48px 1fr;
column-gap: 12px;
align-items: center;
justify-content: center;
background-color: orange;
border: 1px solid black;
}
.grid-item {
padding: 16px;
border: 1px solid black;
font-size: 12px;
background-color: aliceblue;
}
<section class="grid">
<div class="grid-item">1</div>
<div class="grid-item">Item 1</div>
<div class="grid-item">2</div>
<div class="grid-item">Item 2</div>
<div class="grid-item">3</div>
<div class="grid-item">Item 3</div>
<div class="grid-item">4</div>
<div class="grid-item">Item 4</div>
</section>
发布评论
评论(2)
要更改 order ,您可以使用
order
;)使用
nth-child(n)
选择器,从您的html中,可能是:ressources
https://developer.mozilla.org/en-us/docs/web/css/order
对于未知数的项目(&amp;如果您有最大可能已知的项目),则可以使用
:nth-last-- Child():First-Child
选择器要测试将X项目放入不同列中的项目数。具有8个项目的示例(16)
从最后一个片段中,您可以重复更新
.grid-item的规则:nth-last-child(xx):first-child〜:nth-child(x)
,带有子项目的测试量它的JavaScript也可以轻松地帮助将元素设置为特定的列。
to change the order, you may use
order
;)using the
nth-child(n)
selector and from your HTML , it could be :ressources
https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child
https://developer.mozilla.org/en-US/docs/Web/CSS/order
For an unknown number of item (& if you have a maximum possible known ) , you could prepare a few rules using the
:nth-last-child():first-child
selector to test numbers of item to place x item into different column.Example with 8 items (16)
From that last snippet, you can repeat the rules updating
.grid-item:nth-last-child(XX):first-child~:nth-child(X)
with the testing amount of child item it hasJavascript can also easily help to set elements into a specific column.
我已经更改了您的布局以实现您想要的东西。就像您说的那样,有很多解决方案,在这里您有我的!希望它有帮助。
总而言之,我已经使用了Display Flex,而不是将布局分为两个列(移动第一策略),这些布局将在移动屏幕中的另一个下方打印出一个,然后通过媒体查询更改为另一个。
这是我的代码:
I have changed your layout to achieve what you want. Like you say, there is a lot of solutions and here you have mine! Hope it helps.
In summary, I have used display flex instead of grid dividing the layout in two columns (mobile first strategy) that will be printed one below the other in mobile screens and then change to one beside the other via media query.
Here is my code: