12

With the repeat() and auto-fit / auto-fill functions it's easy to get grid items to wrap when there is a defined length pattern for the columns or rows.

In the example below, all columns are a minimum width of 100px and maximum width of 1fr.

jsFiddle demo

#grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  grid-gap: 1em;
}

#grid > div {
  background-color: #ccddaa;
  padding: 10px;
}
<div id="grid">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>

But when the tracks have no length pattern (i.e., lengths vary randomly), how can grid items be made to wrap?

In the example below, the first column is 60% width. The second column is min/max 250px/1fr. How can I get the second column to wrap on smaller screens?

jsFiddle demo

#grid {
  display: grid;
  grid-template-columns: 60% minmax(250px, 1fr);
  grid-gap: 1em;
}

#grid > div {
  background-color: #ccddaa;
  padding: 10px;
}
<div id="grid">
  <div></div>
  <div></div>
</div>

I know flexbox and media queries provide solutions, but I'm wondering if Grid Layout can do this. I searched the spec but didn't find anything. Maybe I missed the section. Thanks for your help.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • grid builds grid via column and rows, wich makes each rows and columns the same size, like an HTML table does. Like table, elements can span a few rows and or columns, .. but it needs to be set in the css for each containers via a class/id or a pseudo-class. auto-fill can make numbers of item to varies, but columns width and row's height will take the widest elements it contains. see : https://www.w3.org/TR/css-grid-1/#auto-repeat auto-fit might be what you are looking for – G-Cyrillus Nov 02 '17 at 19:43
  • Thanks. I'm familiar with that layout concept and section in the spec ([I've written about it before](https://stackoverflow.com/q/46226539/3597276)). I was wondering if Grid has a different method for wrapping tracks, either through a method I haven't discovered yet or by creating a, let's say, 12-column grid, then using line-based placement to make the items span across columns and eventually wrap. @G-Cyr – Michael Benjamin Nov 02 '17 at 19:51
  • 1
    oups, nop unfortunately as far as i know, it does not, you may end up with chidren overlapping each others unfortunately :( Your question and expectation seems very similar to inline elements 101 behavior ... if its holding inline content. It would be a nice and smart feature indeed . – G-Cyrillus Nov 02 '17 at 19:58
  • yep, grid(table 2.0 display version) is very much alike the table display with the col and row span that was missing from the beginning. Flex is definitely the most .... flexible grid system for now – G-Cyrillus Nov 02 '17 at 20:01

1 Answers1

12

Grid items do not actually wrap. The reason you see grid items "wrapping" to the next row is really because the explicit grid is being altered to keep within the constraints stipulated by minmax(). The number of columns generated by repeat() is proportional to the used width of the grid container, and the grid items are laid out one by one according to the number of columns, with new rows being created as necessary.

So, it is not possible to force the second grid item to wrap when there are two columns and there is room in the explicit grid to insert that grid item in the second column. Besides, even if you could tell the second grid item to "wrap", it would mean placing it in a new row in the first column, so its layout will be governed by the first column and not the second. Having it still be sized according to the second column would, of course, break the grid layout entirely.

If the intention is to accommodate smaller screens by wrapping elements to new lines, flex layout should be used, not grid layout. Grid layout is not suitable for this purpose.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 3
    Flexbox forced me to think outside traditional CSS concepts. Now Grid is forcing me to think outside flex concepts. Flex wrapping behavior was the (erroneous) thinking behind my question. Thanks for the clarification. – Michael Benjamin Jan 19 '18 at 17:41