Tv's Very Own
Tv's Very Own

Reputation: 85

Grid-row-gap not applying percentage

So I have an 4x5 image grid where the images can be a variety of sizes but max out at 180x180px.

<div class="grid">
    <div class="box 1 a"><img src="./assets/Account-Attempt-4-1.gif"></div>
     ...
    <div class="box 5 d"><img src="./assets/really-queer-Christmas1.gif"></div>
</div>

My CSS will space my columns 2.5% apart, but leaves my rows touching. Is this because I defined my row height in pixels?

.grid {
  margin-left: 5%;
  margin-right: 5%;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: 180px 180px 180px 180px 180px;
  grid-gap: 2.5% 2.5%;
}

.box {
  display: flex;
  align-items: center;
  justify-content: center;
}

Upvotes: 5

Views: 8602

Answers (1)

Bdloul
Bdloul

Reputation: 902

If you want to use percentage for your grid-row-gap you need a fixed height for your .grid div. Otherwise you need to use px. Example here: https://jsfiddle.net/t53s25us/

.grid {
  margin-left: 5%;
  margin-right: 5%;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-gap: 2.5% 2.5%;
  height: 200px;
}

.box {
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: red;
}
<div class="grid">
  <div class="box 1 a">AAAA</div>
  <div class="box 1 a">AAAA</div>
  <div class="box 1 a">AAAA</div>
  <div class="box 1 a">AAAA</div>
  <div class="box 1 a">AAAA</div>
</div>

Upvotes: 4

Related Questions