Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

CSS Grid: Defining Layouts with Rows and Columns

Hello! Welcome back to the "Responsive Design and Modern CSS" module.

In our last lesson, we explored CSS Flexbox, a powerful tool for creating one-dimensional layouts. You learned how to align items in a single row or column, which is perfect for components like navigation bars and button groups. You saw the direct parallel between Flexbox and Figma's "Auto Layout" feature.

Today, we move from one dimension to two. The learning outcome for this lesson is to build two-dimensional layouts using CSS Grid with grid-template-columns and grid-template-rows. While Flexbox excels at arranging items in a line, CSS Grid is designed for arranging items in both rows and columns simultaneously. This makes it the ideal tool for creating the overall structure of a web page—the kind of layout grids you might define in Figma to guide your designs.

This image illustrates a classic page layout. CSS Grid makes creating such two-dimensional structures straightforward by defining columns and rows.

By the end of this lesson, you'll understand how to define a grid and create responsive layouts that automatically adapt to different screen sizes.

1. From Flexbox to Grid: The Second Dimension

The first step is to tell the browser that an element should manage its children using a grid layout. Just like with Flexbox, this is done with the display property.

  • Flexbox: display: flex; (arranges children in one dimension)
  • Grid: display: grid; (arranges children in two dimensions)

Once you've created a grid container, you need to define its structure. This is where Grid's core properties come in:

  • grid-template-columns: Defines the number and width of the columns.
  • grid-template-rows: Defines the number and height of the rows.

Let's watch a short video that introduces these fundamental properties and shows how to create your first grid. It also introduces the fr (fraction) unit, which is key to creating flexible layouts.

CSS Grid Course - The Only Grid Tutorial You'll Ever Need!

This video from the Coding2GO channel will walk you through the basics of setting up a grid container and defining its columns and rows using both fixed (pixel) and flexible (fraction) units.

Watch from 00:48 to 05:05. Pay close attention to: How display: grid creates a grid container. How grid-template-columns and grid-template-rows define the grid's structure. The difference between using fixed pixel values and the flexible fr unit.

As you saw, the fr unit works similarly to flex-grow in Flexbox, distributing available space among grid tracks. For example, grid-template-columns: 2fr 1fr; creates two columns where the first is twice as wide as the second.

2. Building a Truly Responsive Grid

Defining a fixed number of columns (e.g., repeat(3, 1fr)) is a good start, but it isn't truly responsive. On a narrow screen, those three columns might become too squished. On a very wide screen, you might have too much empty space.

The real power of CSS Grid for responsive design comes from a powerful combination of functions that allow the browser to automatically figure out how many columns can fit: repeat(), auto-fit, and minmax().

Here's the pattern:
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));

Let's break that down:

  • repeat(...): A helper function to create multiple tracks.
  • auto-fit: A special keyword that tells the browser to fit as many columns as possible into the available space. When the space gets too narrow, items will wrap onto the next line automatically.
  • minmax(250px, 1fr): This is the size for each column. It says each column should have a minimum width of 250px. If there's extra space after fitting all the columns, that space should be distributed equally among them (that's what the 1fr maximum does).

This single line of CSS creates a fully responsive grid without any need for media queries. It's a foundational pattern in modern web development.

Let's see it in action.

Learn how to create a responsive CSS grid layout

Kevin Powell provides an excellent, clear demonstration of this responsive grid pattern. Watching this will solidify your understanding of how auto-fit and minmax work together.

Watch the segment from 19:25 to 22:32. Observe how the grid automatically changes the number of columns as the browser window is resized.

3. Spanning Multiple Tracks

So far, we've seen each grid item occupy a single cell. But the true two-dimensional power of Grid is revealed when you make items span across multiple columns or rows. This is how you create the more complex, asymmetrical layouts common in modern web design (often called "bento grids").

You can control this using the grid-column and grid-row properties on a specific grid item.

For example, to make an item span two columns, you would use:

.featured-item {
  grid-column: span 2; /* This item will now take up the space of two columns */
}

And to make it span two rows:

.featured-item {
  grid-row: span 2; /* This item will now take up the space of two rows */
}

This is perfect for creating a "featured" article or product in a list that you want to make larger and more prominent.

The following video segment builds on the previous example to show you exactly how to implement this and make it responsive.

Learn how to create a responsive CSS grid layout

Continuing with Kevin Powell's tutorial, this part demonstrates how to make a specific card 'featured' by having it span multiple rows and columns, and how to use a media query to apply this effect only on larger screens.

Watch from 24:41 to 30:12. Notice how grid-row: span 2 and grid-column: span 2 are used to create the featured item, and how a media query is used to ensure the layout looks good on smaller screens.

4. Further Reading (Optional)

The videos provide a great practical overview. If you'd like to solidify your understanding with a detailed written guide, the MDN Web Docs are the definitive source.

Basic concepts of grid layout - CSS - MDN Web Docs

The 'Basic concepts of grid layout' article on MDN provides a comprehensive text-based explanation of the concepts we've covered.

Skim through this document to reinforce the terminology. Focus on the sections covering 'Grid tracks', 'The fr unit', 'Track listings with repeat() notation', and 'Track sizing and minmax'.

Conclusion

Great work today! You've now unlocked the second major layout tool in modern CSS. By understanding how to define a two-dimensional grid, you can move beyond arranging simple components and start building complex, responsive page structures.

Key Takeaways:

  • CSS Grid is for two-dimensional layouts (rows and columns), making it ideal for overall page structure.
  • You create a grid with display: grid.
  • The grid's structure is defined with grid-template-columns and grid-template-rows.
  • The fr unit allows for flexible tracks that share available space.
  • The pattern repeat(auto-fit, minmax(min_width, 1fr)) is a powerful way to create responsive grids that automatically adjust the number of columns.
  • Individual items can be made to span multiple tracks using grid-column: span N and grid-row: span N.

In our next lesson, we'll look at another aspect of positioning elements: the position property. While Flexbox and Grid are for managing the overall flow of a layout, the position property gives you fine-grained control to place elements in specific locations, such as creating overlays, sticky headers, or decorative elements that break out of the grid.

Can't find a good explanation? Sign up and we'll make it for you

Sign up