Html

CSS grid wrapping

25 September 2026 · 8 min read

CSS grid wrapping

CSS Grid wrapping is a powerful layout technique that allows developers to create flexible and responsive web designs. It offers a significant advantage over traditional methods like floats or flexbox by enabling two-dimensional layouts with ease. This means you can control both the horizontal and vertical arrangement of items within a container, simplifying the process of building complex grid-based designs that adapt seamlessly to different screen sizes. Mastering CSS grid wrapping is essential for any front-end developer aiming to create modern, dynamic, and user-friendly web experiences. It provides the tools to build everything from image galleries and product listings to intricate dashboard layouts, all while ensuring a clean and organized visual presentation.

Understanding grid-template-columns and grid-template-rows

These two properties are fundamental to CSS Grid. grid-template-columns defines the number and size of columns in your grid, while grid-template-rows does the same for rows. You can use various units like pixels, percentages, fractions (fr), and even the new auto keyword to create flexible grid structures. For example, grid-template-columns: 1fr 2fr 1fr; creates three columns, with the middle one twice as wide as the outer ones.

The fr unit is particularly useful for distributing available space proportionally. Imagine you want a sidebar and a main content area. Using fractions, you can easily allocate, say, 1/4 of the container width to the sidebar and 3/4 to the main content, and the grid will handle the calculations automatically. This ensures your layout remains balanced across different screen sizes.

Furthermore, you can combine different units within these properties. grid-template-columns: 200px 1fr auto; creates a grid with a fixed-width first column (200px), a flexible second column that takes up the remaining space, and a third column that adjusts its size based on its content.

The Magic of fr Units

The fr unit in CSS Grid represents a fraction of the available space in the grid container. It’s incredibly useful for creating responsive layouts, especially when combined with other units like pixels or percentages. Imagine creating a three-column layout where the first and last columns are fixed width, and the middle column occupies the remaining space. Using fr units makes this a breeze.

For instance, grid-template-columns: 200px 1fr 200px; creates a grid where the first and last columns are 200 pixels wide, and the center column automatically fills the remaining space. This dynamic adjustment is crucial for responsive design, ensuring your layout adapts gracefully to different screen widths. No more complex calculations or media queries needed to achieve this flexibility.

Moreover, fr units allow for proportional distribution of space. grid-template-columns: 1fr 2fr; creates two columns, with the second one occupying twice the space of the first. This proportional distribution is powerful for creating visually balanced layouts, regardless of the screen size.

Controlling Wrapping with grid-auto-rows and grid-auto-columns

These properties control how grid items wrap onto new rows or columns when they exceed the defined grid area. grid-auto-rows specifies the height of implicitly created rows, while grid-auto-columns does the same for columns. This is where the true power of CSS Grid wrapping comes into play.

By default, grid items are placed in a single row, overflowing horizontally if there isn’t enough space. Using grid-auto-rows: 100px;, for example, forces items to wrap onto new rows every 100 pixels, creating a neat grid layout even with a dynamic number of items. Similarly, grid-auto-columns can be used to control wrapping behavior for columns.

Imagine building an image gallery. By setting grid-auto-rows to a desired height and leaving the number of columns flexible, you can create a responsive gallery that automatically adjusts the number of images per row based on the available screen width. This ensures optimal display and user experience across different devices.

Alignment and Justification in CSS Grid

CSS Grid offers powerful alignment and justification properties that give you fine-grained control over how items are positioned within their grid areas. Properties like justify-items, align-items, justify-content, and align-content allow you to center content, distribute it evenly, or pack it tightly within the grid container.

For example, justify-items: center; centers items horizontally within their grid cells, while align-items: center; centers them vertically. justify-content: space-between; distributes grid tracks evenly along the row axis, with space between each track. These properties provide a flexible and intuitive way to manage the layout and visual presentation of your grid items.

Imagine designing a card-based layout. By using these alignment and justification properties, you can easily center the content within each card and arrange the cards themselves in a visually appealing manner, ensuring a consistent and polished look across different screen sizes and content variations.

  • Use fr units for flexible layouts.
  • Control wrapping with grid-auto-rows and grid-auto-columns.
  1. Define the grid container.
  2. Set grid-template-columns and grid-template-rows.
  3. Place items using grid-column and grid-row.

Learn more about CSS GridFeatured Snippet: CSS Grid wrapping provides a flexible mechanism for controlling how grid items are arranged. By using properties like grid-auto-rows and grid-auto-columns, you can define how items wrap onto new rows or columns, creating dynamic and responsive layouts.

FAQ

Q: What’s the difference between grid-template-rows and grid-auto-rows?

A: grid-template-rows defines the explicit rows in your grid, while grid-auto-rows specifies the size of implicitly created rows when content wraps.

CSS Grid wrapping offers an elegant and efficient solution to complex layout challenges. By mastering these core concepts, you can create dynamic and responsive web designs that adapt effortlessly to various screen sizes and content variations. From simple image galleries to intricate dashboard layouts, CSS Grid empowers you to build user-friendly interfaces with ease and precision. Dive deeper into the world of CSS Grid and unlock its full potential by exploring resources like MDN Web Docs and CSS Tricks. Start experimenting with different grid configurations and wrapping behaviors to discover the vast possibilities for creating visually stunning and highly functional web layouts. Don’t wait – start building your next project with the power of CSS Grid wrapping today!

Question & Answer :
Is it possible to make a CSS grid wrap without using media queries?

In my case, I have a non-deterministic number of items that I want placed in a grid and I want that grid to wrap. Using Flexbox, I’m unable to reliably space things nicely. I’d like to avoid a bunch of media queries too.

Here’s some sample code:

``` .grid { display: grid; grid-gap: 10px; grid-auto-flow: column; grid-template-columns: 186px 186px 186px 186px; } .grid > * { background-color: green; height: 200px; } ```
<div class="grid"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> </div>
And here's a GIF image:

GIF image of what I’m seeing

As a side-note, if anyone can tell me how I could avoid specifying the width of all the items like I am with grid-template-columns that would be great. I’d prefer the children to specify their own width.

Use either auto-fill or auto-fit as the first argument of the repeat() notation.

<auto-repeat> variant of the repeat() notation:

repeat( [ auto-fill | auto-fit ] , [ <line-names>? <fixed-size> ]+ <line-names>? ) 

auto-fill

When auto-fill is given as the repetition number, if the grid container has a definite size or max size in the relevant axis, then the number of repetitions is the largest possible positive integer that does not cause the grid to overflow its grid container.

https://www.w3.org/TR/css-grid-1/#valdef-repeat-auto-fill

``` .grid { display: grid; grid-gap: 10px; grid-template-columns: repeat(auto-fill, 186px); } .grid>* { background-color: green; height: 200px; } ```
<div class="grid"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> </div>
The grid will repeat as many tracks as possible without overflowing its container.

Using auto-fill as the repetition number of the repeat() notation

In this case, given the example above (see image), only 5 tracks can fit the grid-container without overflowing. There are only 4 items in our grid, so a fifth one is created as an empty track within the remaining space.

The rest of the remaining space, track #6, ends the explicit grid. This means there was not enough space to place another track.


auto-fit

The auto-fit keyword behaves the same as auto-fill, except that after grid item placement any empty repeated tracks are collapsed.

https://www.w3.org/TR/css-grid-1/#valdef-repeat-auto-fit

``` .grid { display: grid; grid-gap: 10px; grid-template-columns: repeat(auto-fit, 186px); } .grid>* { background-color: green; height: 200px; } ```
<div class="grid"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> </div>
The grid will still repeat as many tracks as possible without overflowing its container, but the empty tracks will be collapsed to `0`.

A collapsed track is treated as having a fixed track sizing function of 0px.

Using auto-fit as the repetition number of the repeat() notation

Unlike the auto-fill image example, the empty fifth track is collapsed, ending the explicit grid right after the 4th item.


auto-fill vs auto-fit

The difference between the two is noticeable when the minmax() function is used.

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.

``` .grid { display: grid; grid-gap: 10px; grid-template-columns: repeat(auto-fill, minmax(186px, 1fr)); } .grid>* { background-color: green; height: 200px; } ```
<div class="grid"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> </div>
When using `auto-fit`, the items will grow to fill the remaining space because all the empty tracks will be collapsed to `0px`.
``` .grid { display: grid; grid-gap: 10px; grid-template-columns: repeat(auto-fit, minmax(186px, 1fr)); } .grid>* { background-color: green; height: 200px; } ```
<div class="grid"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> </div>
---

Playground:

CodePen

Inspecting auto-fill tracks

auto-fill


Inspecting auto-fit tracks

auto-fit