Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get grid items of different lengths to wrap?

Tags:

css

css-grid

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.

like image 403
Michael Benjamin Avatar asked Nov 02 '17 18:11

Michael Benjamin


People also ask

Can you wrap with grid?

If you can define a column width, then grid layout's auto-fill function makes wrapping easy. Notes: The auto-fill function allows the grid to line up as many grid tracks (columns or rows) as possible without overflowing the container. This can create similar behavior to flex layout's flex-wrap: wrap .

How do you arrange things in a grid?

Items with the lowest order value are placed first in the grid. Items with higher values are placed later. Items which have the same order value will be placed in the order in which they appear in the source document. Let's take a look at an example.


1 Answers

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.

like image 137
BoltClock Avatar answered Sep 22 '22 21:09

BoltClock