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;
}

1 answer

  • answered 2018-05-16 05:22 Bdloul

    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>