I have an unknown amount of items that need to be displayed in a grid. I'd like the number of columns to auto-fill
as needed, with no limit on the number of rows. I can get this working just fine:
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
list-style: none;
}
<ul class="grid">
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
<li>f</li>
<li>g</li>
<li>h</li>
</ul>
However, I'd also like the items to be sorted alphabetically by column:
a d g
b e h
c f
I know I can use grid-auto-flow: column
to place each item by column instead of by row, but if I do that, I just get one long row.
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
grid-auto-flow: column;
list-style: none;
}
<ul class="grid">
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
<li>f</li>
<li>g</li>
<li>h</li>
</ul>
How can I keep the behavior of the first snippet but with sorting from top to bottom instead of from left to right?