I need to create a responsive grid of squares. My grid is inside its parent, which is also responsive. I'd like my grid to fit perfectly inside its parent. I don't know how to achieve it and still use square cells.
I know the trick with padding-bottom: 100% to make elements square, but I'm not sure how to apply it here. Also, I can't just calculate the percentage for the cells, as the number of cells can change.
Here's what I have:
.container {
width: 400px;
height: 300px;
background: #ff8;
border: 5px solid blue;
}
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
.grid > * {
background-color: orange;
}
.grid > *::before {
content: '';
display: block;
padding-top: 100%;
}
<div class="container">
<div class="grid">
<div></div> <div></div> <div></div> <div></div>
<div></div> <div></div> <div></div> <div></div>
</div>
</div>
Here are the images to better describe my goal:
Can it even be done?

