英文:
Changing the order of items in a CSS grid from left-right to top-down
问题
我正在建立一个标签列表的系统,并且这些标签应该在CSS网格中以三列显示。所以,类似这样的:
```html
<ul>
<li>Alfa</li>
<li>Bravo</li>
<li>Charlie</li>
<li>Delta</li>
<li>Echo</li>
<li>Foxtrot</li>
</ul>
ul {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
在浏览器中查看时,项目的顺序如下:
Alfa Bravo Charlie
Delta Echo Foxtrot
然而,我想要它们按照这样的顺序排列:
Alfa Charlie Echo
Bravo Delta Foxtrot
所以,基本上是从上到下排序,而不是从左到右。
有没有简单的方法来实现这个?我已经尝试过一些:nth-child
选择器和grid-column
,但那不是我想要的。我可能可以用Javascript解决这个问题,但我想知道是否有纯CSS的解决方案。
<details>
<summary>英文:</summary>
I'm building a list of tags in a system, and those tags should be displayed in a CSS grid with three columns. So, something like this:
```html
<ul>
<li>Alfa</li>
<li>Bravo</li>
<li>Charlie</li>
<li>Delta</li>
<li>Echo</li>
<li>Foxtrot</li>
</ul>
ul {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
When viewing this in a browser, the items are ordered like this:
Alfa Bravo Charlie
Delta Echo Foxtrot
However, i would like them to be ordered like this:
Alfa Charlie Echo
Bravo Delta Foxtrot
So, basically ordered from top to bottom instead of left-right.
Is there any way to do this in a simple way? I already experimented a bit with :nth-child
selectors and grid-column
, but that doesn't really do what i want. I could probably solve this with Javascript, but i like to know if there's a CSS-only solution.
答案1
得分: 2
我建议您在这里使用"columns"而不是"grid columns"。
ul {
columns: 3;
}
<ul>
<li>Alfa</li>
<li>Bravo</li>
<li>Charlie</li>
<li>Delta</li>
<li>Echo</li>
<li>Foxtrot</li>
</ul>
祝您有美好的一天!
英文:
I would recommend that you use columns instead of grid columns for this
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
ul{
columns: 3
}
<!-- language: lang-html -->
<ul>
<li>Alfa</li>
<li>Bravo</li>
<li>Charlie</li>
<li>Delta</li>
<li>Echo</li>
<li>Foxtrot</li>
</ul>
<!-- end snippet -->
Have a great day!
答案2
得分: 0
我只进行了简短的研究。
似乎grid-auto-flow: column
(链接)可以实现你想要的效果。
英文:
I did only a short research on this.
It seems like grid-auto-flow: column
(https://www.w3schools.com/cssref/pr_grid-auto-flow.php) does what you want.
答案3
得分: 0
以下是翻译好的代码部分:
<ul style="display: grid;
grid-template-columns: 1fr 1fr 1fr;">
<li>Alfa</li>
<li style="grid-row: 2; grid-column: 1;">Bravo</li>
<li style="grid-row: 1; grid-column: 2;">Charlie</li>
<li style="grid-row: 2; grid-column: 2;">Delta</li>
<li style="grid-row: 1; grid-column: 3;">Echo</li>
<li style="grid-row: 2; grid-column: 3;">Foxtrot</li>
</ul>
如果你想要在一个独立的CSS页面中为每个li
元素赋予特定的类名,如li1
、li2
、li3
等,然后按照我上面所做的方式设置它们的CSS属性,示例代码如下:
li2 {
grid-row: 2;
grid-column: 1;
}
使用grid-row
和grid-column
属性,你可以按照你想要的任何方向排列你的项目。
英文:
you can try this:
<ul style="display: grid;
grid-template-columns: 1fr 1fr 1fr;">
<li>Alfa</li>
<li style="grid-row: 2; grid-column: 1;">Bravo</li>
<li style="grid-row: 1; grid-column: 2;">Charlie</li>
<li style="grid-row: 2; grid-column: 2;">Delta</li>
<li style="grid-row: 1; grid-column: 3;">Echo</li>
<li style="grid-row: 2; grid-column: 3;">Foxtrot</li>
</ul>
if you want to have a separate CSS page give each li a specific class name like li1, li2, li3 and so on, and then set their CSS property to them as I have done above.
for example:
li2{
grid-row:2;
grid-column:1
}
with grid-row and grid-column you can order your items in any direction you want.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论