I have to design a responsive grid.
The grid should change based on the number of items (instead of the size of the screen).
From 5 to 6 items should be 2x3,
from 7 to 9 items should be 3x3,
from 10 to 12 items should be 3x4,
from 13 to 16 items should be 4x4,
and so on...
Is this something I can do using CSS Grid?
This is what I've tried:
<main>
<figure>
<img src="https://placekitten.com/600/400" alt="">
</figure>
<figure>
<img src="https://placekitten.com/600/400" alt="">
</figure>
<figure>
<img src="https://placekitten.com/600/400" alt="">
</figure>
<figure>
<img src="https://placekitten.com/600/400" alt="">
</figure>
<figure>
<img src="https://placekitten.com/600/400" alt="">
</figure>
</main>
main {
/* display: flex; */
/* flex-wrap: wrap; */
display: grid;
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
align-content: stretch;
height: 100vh;
}
figure {
margin: 0;
/* flex-grow: 1; */
/* flex-basis: 12rem; */
}
img {
width: 100%;
height: 100%;
object-fit: cover;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…