2

I'm new with grid layout, and I'm trying an auto wrapping design with it.

My code is the following:

#grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  grid-template-rows: 200px 200px 200px 200px;
  grid-gap: 10px;
}

#grid > div {
  background-color: #ccddaa;
  min-width: 310px;
}
<div id="grid">
  <div>text</div>
  <div>text</div>
  <div>text</div>
  <div>text</div>
</div>

Codepen example: https://codepen.io/arielcessario/pen/GvdwLJ

The thing I have noticed is that when I resize chrome/Opera window (not in dev mode, not mobile emulation), the result is the following:

enter image description here

As you can see, all is stacked as expected.

But when I try to emulate a mobile device, or try it in a mobile browser, this is what I get:

enter image description here

This is a screenshot of the project in mobile:

enter image description here

It never stacks, no matter the width.

Notes:

  • When I inspect in mobile view, the width of any box is never smaller than 379px, the full width of the grid is never less than 768px.
  • If I use fixed max-with kind of works, but percentage doesn't.
  • On Firefox emulator works fine, but on Firefox mobile browser it doesn't.
  • As Michael_B pointed out, the pen works fine on mobile, the problem is with the real project and if you emulate on chrome.

I guess I'm missing something but I can't find it. Thanks in advance.

KN_
  • 356
  • 3
  • 11
  • I believe there is some media query added by the Codepen. Have you tried you layout outside of it ? – vals Aug 22 '17 at 20:44
  • I have tried in local environment. Empty project, no bootstrap, just that code, but same result every time. Also we have tried this with a few friends, and they have had same issue. Thanks and Regards! – KN_ Aug 22 '17 at 21:51

1 Answers1

0

I can't replicate the problem on mobile.

However, if the goal is a single column for smaller screens, try this:

Add min-width: 0 to your grid items (explanation).

OR

Using a media query, adjust the container like this:

@media ( max-width: 500px ) {
    #grid {
       grid-template-columns: 1fr;
    }
}
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • Thanks for your response, may I ask your mobile set up to try it? Regards. – KN_ Aug 22 '17 at 19:30
  • I have tried min-width and same result. The media query is an option, but I think the behaviour is wrong in mobile browsers, it should respond respond the same way in both cases I guess. Thanks and Regards! – KN_ Aug 22 '17 at 21:56
  • 1
    @KN_ Are you using an old mobile device? It is possible that css grid isn't supported by your browser. – wuppie367 Aug 23 '17 at 07:37
  • Galaxy S6 with android 7.1. We also have tried in other operative systems, and other browsers. – KN_ Aug 23 '17 at 13:08
  • I just tested your codepen in a Galaxy S4, android and chrome, portrait mode. Layout works fine. Single column. – Michael Benjamin Aug 23 '17 at 13:27
  • The pen works fine on mobile, the problem is with the real project (I'll edit the question to add this) or testing the pen on chrome (desktop) with device emulation. Regards! – KN_ Aug 23 '17 at 13:44