So i have a grid setup like this
<div class="grid-container">
<div>a</div>
<div>b</div>
<div>c</div>
<div>d</div>
<div>e</div>
<div>f</div>
...
<div>z</div>
</div>
How can i make it appear in two columns, grouped in rows of say 4 high. Creating an arrangement like this:
a | e
b | f
c | g
d | h
-----
i | m
j | n
...
s | w
t | x
-----
y |
z |
Without the obvious answer of grouping 4 or 8 divs to a separate gridbox.
Implementations that differ from using divs are welcome too
I currently got it semi working except for the alphabetical sorting by using
.grid-container {
display: grid;
grid-template-columns: repeat(2, 150px);
grid-template-rows: repeat(4, auto);
}
.grid-container DIV:nth-child(8n) {
padding-bottom: 10px;
}
But that does not honor the alphabetical order, giving this:
a | b
c | d
e | f
g | h
-----
i | j
Adding
grid-auto-flow: column;
to the container makes it add more columns on overflow, disregarding the defined 2 columns.
2
Answers
There is no way (AFAIK) to do this dynamically.
Grouping in 8s and leveraging
subgrid
would be logical.You have a pattern that repeats each 8 elements so:
And with some optimization: