Grid of squares to fit its parent

Viewed 127

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:

Horizontal Grid

Vertical Grid

Can it even be done?

2 Answers

You can set the container to a flex parent, and then align-items for the grid parent.

.container {
  width: 100%;
  min-height: 200px;
  background: #ff8;
  border: 5px solid blue;
  box-sizing: border-box;
  display: flex;
  align-items: center;
}

.grid {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

.grid>* {
  background-color: orange;
  border-radius: 100%;
}

.grid>*::before {
  content: '';
  display: block;
  padding-top: 100%;
}

.container2 {
  width: 200px;
  min-height: 800px;
  background: #ff8;
  border: 5px solid blue;
  box-sizing: border-box;
  display: flex;
  align-items: center;
}

.container2 .grid {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 10px;
}
<div class="container">
  <div class="grid">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
  </div>
</div>

<div class="container2">
  <div class="grid">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
  </div>
</div>

The answer of @disinfor is correct but if you don't want to use the flex display and keep grid display, have you tried to add a percent padding-top?

I added also a padding-bottom to fix the problem when width: 1000px

.container {
    width: 1000px
    height: 300px;
    background: #ff8;
    border: 5px solid blue;
}

.grid {
    padding-top: 15%;
    padding-bottom: 15%;
    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>

Related