question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[css-grid] grid-gap is not retained through media queries

See original GitHub issue

I wrote this test css-grid code and realised that the grid-gap functionality wasn’t getting carried through media queries.

/* changing a grid-template with a media-query */
.grid {
  display: grid;
  grid-gap: 10px; /* grid-gap defined here */
  grid-template:
    "a   b" 100px
    "c   d" 100px
    "e   f" 100px /
    1fr  1fr;
}

.cell-A {
  grid-area: a;
}

.cell-B {
  grid-area: b;
}

.cell-C {
  grid-area: c;
}

.cell-D {
  grid-area: d;
}

.cell-E {
  grid-area: e;
}

.cell-F {
  grid-area: f;
}

@media (min-width: 600px){
  .grid {
    /* grid-gap setting should be remembered here */
    grid-template-areas:
      "a   b   c"
      "d   e   f";
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(2, 100px);
  }
}

It compiles into this:

.grid {
  display: -ms-grid;
  display: grid;
  grid-gap: 10px;
  -ms-grid-rows: 100px 10px 100px 10px 100px;
  -ms-grid-columns: 1fr 10px 1fr;
      grid-template: "a   b" 100px
 "c   d" 100px
 "e   f" 100px /
 1fr  1fr;
}

.cell-A {
  -ms-grid-row: 1;
  -ms-grid-column: 1;
  grid-area: a;
}

.cell-B {
  -ms-grid-row: 1;
  -ms-grid-column: 3;
  grid-area: b;
}

.cell-C {
  -ms-grid-row: 3;
  -ms-grid-column: 1;
  grid-area: c;
}

.cell-D {
  -ms-grid-row: 3;
  -ms-grid-column: 3;
  grid-area: d;
}

.cell-E {
  -ms-grid-row: 5;
  -ms-grid-column: 1;
  grid-area: e;
}

.cell-F {
  -ms-grid-row: 5;
  -ms-grid-column: 3;
  grid-area: f;
}

@media (min-width: 600px) {
  .grid {
        grid-template-areas: "a   b   c"
 "d   e   f";

    /* !!! IT HAS FORGOTTEN ABOUT THE GRID-GAP SETTING !!! */

    -ms-grid-columns: (1fr)[3];
    grid-template-columns: repeat(3, 1fr);
    -ms-grid-rows: (100px)[2];
    grid-template-rows: repeat(2, 100px);
  }
  .cell-A {
    -ms-grid-row: 1;
    -ms-grid-column: 1;
  }
  .cell-B {
    -ms-grid-row: 1;
    -ms-grid-column: 2;
  }
  .cell-C {
    -ms-grid-row: 1;
    -ms-grid-column: 3;
  }
  .cell-D {
    -ms-grid-row: 2;
    -ms-grid-column: 1;
  }
  .cell-E {
    -ms-grid-row: 2;
    -ms-grid-column: 2;
  }
  .cell-F {
    -ms-grid-row: 2;
    -ms-grid-column: 3;
  }
}

My idea is that if the grid-area name is consistent throughout various media-queries then it is safe to assume that it is looking at the same grid.

If the grid-gap setting is defined outside of a media query, then that gap setting needs to be applied as a default across all grids that share that same area name.

.grid {
  display: grid;
  grid-gap: 10px;/* grid-gap defined */
  grid-template:
    "a   b" 100px
    "c   d" 100px
    "e   f" 100px /
    1fr  1fr;
}


@media (min-width: 600px){
  .grid {
    /* grid-gap: 10px; << grid-gap inherited */
    grid-template-areas:
      "a   b   c"
      "d   e   f";
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(2, 100px);
  }
}

If a grid-gap setting is applied inside of a media query, it will only be applied as a default to other grids that live inside media queries that agree with the grid-gap media query.

.grid {
  display: grid;
  /* grid-gap ignored */
  grid-template:
    "a   b" 100px
    "c   d" 100px
    "e   f" 100px /
    1fr  1fr;
}


@media (min-width: 600px){
  .grid {
    grid-gap: 10px;/* grid-gap defined */
    grid-template-areas:
      "a   b   c"
      "d   e   f";
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(2, 100px);
  }
}

@media (min-width: 900px){
  .grid {
    /* grid-gap: 10px; << grid-gap inherited */
    grid-template-areas:
      "a   b   c   d   e   f";
    grid-template-columns: repeat(6, 1fr);
    grid-template-rows: 100px;
  }
}

@media (max-width: 400px){
  .grid {
    /* grid-gap ignored */
    grid-template-areas:
      "a"
      "b"
      "c"
      "d"
      "e"
      "f";
    grid-template-columns: 1fr;
    grid-template-rows: repeat(6, 100px);
  }
}

If there is a conflict then the grid-gap setting that is defined last in the style sheet should have the priority.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
aicommented, May 16, 2018

I think it is safe too. Anyway grid logic is not enabled by default. We can expect some limits from grid prefixes users.

0reactions
aicommented, Aug 13, 2018

Released in 9.1.1

Read more comments on GitHub >

github_iconTop Results From Across the Web

css-grid media queries not responding - Stack Overflow
I am working with grid, and for some reason, using max-width in the media query seems ...
Read more >
Look Ma, No Media Queries! Responsive Layouts Using CSS ...
In this article, we'll start dipping our toes into the power of CSS Grid by building a couple of common responsive navigation layouts....
Read more >
Look Ma, No Media Queries! Responsive Layouts with CSS Grid
In this article, we'll start dipping our toes into the power of CSS Grid by building a couple of common responsive navigation layouts....
Read more >
Grid template areas - CSS: Cascading Style Sheets | MDN
Redefining the grid using media queries. As our layout is now contained in one part of the CSS, this makes it very easy...
Read more >
CSS Grid – A tutorial with examples and explanation - IONOS
Anyone who already has experience with CSS will not have any problems with ... You could also create a gap using the “grid-gap”...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found