zik
zik

Reputation: 3085

Why does grid-gap not work on mobile?

I've been playing around with CSS Grid recently and have noticed something that I can't see to find the answer to. Let's say I split my page out to have 2 columns, and then a row below it, with another column (which spans both columns). On mobile, I'd like them to stack one on top of the other and then go back to layout described above after a certain breakpoint. Here is the markup:

HTML

<div class="grid">
  <div class="upper">
    <div class="a">A</div>
    <div class="b">B</div>
  </div>
  <div class="lower">
    <div class="c">C</div>
  </div>
</div>

SCSS

.upper, .lower {
  display: grid;
}

.upper {
  grid-template-columns: 1fr 1fr;
  grid-template-rows: auto auto;
  background-color:grey;
  grid-gap:10px;

  @media only screen and (max-width:800px) {
    grid-template-columns: 1fr;
    grid-template-rows: auto;
  }
}

.lower {
  grid-template-columns: 1fr;
  grid-template-rows:auto;
  background-color: green;
  grid-gap:10px;
}

I've noticed that on mobile, even though I've defined grid-gap for both of my grid sections, on mobile when the columns stack, the grid-gap is not maintained. So in the fiddle below, when you make the window smaller, you can see that when the columns, stack one on top of the other, the gap between B and C is non existent. Here is the fiddle:

Fiddle

Hope I'm making sense!

EDIT: Bear in mind I'm only testing this in Firefox and Chrome (which support grid).

Upvotes: 3

Views: 10799

Answers (1)

Michael Benjamin
Michael Benjamin

Reputation: 371143

The grid-gap rule doesn't work between B and C because it doesn't apply.

This rule creates gutters between rows and columns inside a grid container.

But you are declaring grid-gap on .upper and .lower, two siblings in a block container. Their parent (.grid) is not a grid container because it doesn't have display: grid or inline-grid.

Therefore, grid-gap: 10px on .upper is creating a 10px gutter between A and B...

and grid-gap: 10px on .lower is creating a 10px gutter between.... nothing (.lower has only one grid item. grid-gap creates gutters between multiple grid items).

fiddle demo 1

For grid-gap to work among the .upper and .lower siblings you need to apply it to their parent, which must be a grid container.

fiddle demo 2

.grid {
  display: grid;    /* NEW */
  grid-gap: 25px;   /* NEW */
}

.upper, .lower {
  display: grid;
}

.upper {
  grid-template-columns: 1fr 1fr;
  grid-gap: 25px;
}

.lower {
  grid-template-columns: 1fr;
  grid-template-rows: auto;
  grid-gap: 10px; /* does nothing unless there are multiple grid items */
}

@media ( max-width:800px ) {
  .upper {
    grid-template-columns: 1fr;
    grid-template-rows: auto;
  }
}

.upper > * { border: 1px dashed red;  }
.lower > * { border: 1px dashed blue; }
<div class="grid">

  <div class="upper">
    <div class="a">A</div>
    <div class="b">B</div>
  </div>

  <div class="lower">
    <div class="c">C</div>
  </div>

</div>

Upvotes: 3

Related Questions