Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting columns to wrap in CSS Grid

Tags:

css

css-grid

How can you specify the maximum number of columns when using display: grid;, with it automatically breaking when the content becomes too wide for the space (or smaller than a minimum size)? Is there a way to do this without media queries?

For example, I have the following which won't break into a single column mode when there is not enough room for the content.

#grid {    display: grid;    grid-template-columns: repeat(2, 1fr);    grid-gap: 1em;  }    #grid > div {    background-color: #ccddaa;  }
<div id="grid">    <div>text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text</div>    <div>text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text</div>    <div>text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text</div>    <div>text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text</div>    <div>text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text</div>    <div>text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text</div>    <div>text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text</div>    <div>text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text</div>    <div>text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text</div>  </div>
like image 396
Panda TG Attwood Avatar asked Apr 27 '17 15:04

Panda TG Attwood


People also ask

How do you wrap grid content in CSS?

Use minmax(186px, 1fr) to range the items from 186px to a fraction of the leftover space in the grid container. When using auto-fill , the items will grow once there is no space to place empty tracks.

What is auto-fill in CSS Grid?

Auto-fill: The auto-fill property fills the rows with as many columns as it can fit. The newly added column may be empty but it will still occupy a space in the given row. It is an important property in the CSS grid that make a responsive layout without writing a media query for each grid.

How do I set column width in grid?

You can specify the width of a column by using a keyword (like auto ) or a length (like 10px ). The number of columns is determined by the number of values defined in the space-separated list. default grid-template-columns: none; No columns are defined, so you only have one.


1 Answers

Neither HTML or CSS have any concept of when descendants of a container wrap.

Essentially, the browser renders the document during an initial cascade. It does not reflow the document when a child wraps.

Therefore, to change the number of columns, you will need to set a width limit somewhere along the line or use media queries.

Here's a more in-depth explanation: Make container shrink-to-fit child elements as they wrap


If you can define a column width, then grid layout's auto-fill function makes wrapping easy.

In this example, the number of columns is based entirely on the width of the screen:

#grid {    display: grid;    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); /* see notes below */    grid-gap: 1em;  }    #grid > div {    background-color: #ccddaa;  }
<div id="grid">    <div>text</div>    <div>text</div>    <div>text</div>    <div>text</div>    <div>text</div>    <div>text</div>    <div>text</div>    <div>text</div>    <div>text</div>  </div>

jsFiddle demo

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.

The minmax() function allows you to set a minimum and maximum size range for a grid track. In the code above, the width of column tracks will be a minimum of 100px and maximum of whatever free space is available.

The fr unit represents a fraction of the available space. It is similar to flex-grow in flex layout.

like image 128
Michael Benjamin Avatar answered Sep 21 '22 19:09

Michael Benjamin