The goal is to align square cells with their container\'s leading and trailing edges while achieving a consistent gap between cells on each row, and between each row.
<
The issue is that the grid cells are fine but the content inside (li) is not matching them.
Instead of using fixed width/height on the li you can consider percentage value and they will be a bit bigger in some cases but will remain square elements:
ul {
display: grid;
width: 260px;
grid-template-columns: repeat(auto-fit, minmax(40px, 1fr));
grid-auto-rows: 1fr;
grid-gap: 10px;
list-style-type: none;
border: 2px solid black;
box-sizing: border-box;
margin: 0;
padding: 0;
background: gray;
animation:change 5s linear alternate infinite;
}
li {
width: 100%;
padding-top:100%;
}
@keyframes change {
to {width:120px}
}