What is CSS Grid?

CSS Grid is a powerful 2D layout system that lets you arrange content in rows and columns at the same time. Unlike Flexbox (which handles 1D layouts), Grid gives you control over both dimensions simultaneously.

Flexbox: One dimension at a time (row OR column). Perfect for navigation bars, button groups, or flexible sequences.

CSS Grid: Two dimensions together (rows AND columns). Perfect for page layouts, photo galleries, dashboards, and complex structures.

Key insight:
Grid is about creating a framework (rows and columns), then placing items into that framework. Flexbox is about distributing space along one axis.

Creating a grid container with display: grid

To activate CSS Grid on an element, set display: grid. This turns the element into a grid container, and all its direct children become grid items.

grid-basics.css Creating a grid container

In the code above:

  • display: grid — Enables CSS Grid.
  • grid-template-columns: 200px 200px 200px — Creates 3 equal columns, each 200px wide.
  • gap: 20px — Adds 20px space between items (both row and column spacing).
Tip:
By default, Grid automatically fills columns left to right, then wraps to the next row. You don't need to set grid-auto-flow unless you want different behavior.

grid-template-columns and grid-template-rows

These properties define the sizes of your columns and rows.

grid-sizing.css Defining columns and rows

You can use multiple unit types:

  • px — Fixed pixels (200px)
  • % — Percentage of container width (25%)
  • auto — Grows/shrinks to fit content
  • fr — Fractional unit (more on this below)

The fr unit — fractional units

The fr unit is CSS Grid's way of distributing available space proportionally. 1fr means "one fraction of the remaining space." It's more flexible than percentages.

fr-unit.css Using the fr unit

Key difference: 1fr 1fr 1fr creates 3 equal columns that scale with the container. 33.33% 33.33% 33.33% would do the same, but fr handles gaps better — gaps don't count toward the percentage calculation.

Try it yourself: Photo gallery grid

Edit the grid below. Try changing column counts from 3 to 2 or 4 by modifying grid-template-columns. Adjust the gap to see spacing change.

✏️ Try It Yourself — Grid columns
Preview

The repeat() function

Typing 1fr 1fr 1fr 1fr 1fr is tedious. The repeat() function lets you repeat a pattern. Syntax: repeat(count, value)

repeat.css Using repeat() to avoid repetition

repeat() saves typing and makes your code more maintainable. Instead of counting columns manually, you specify how many times to repeat a pattern.

minmax() — responsive grids without media queries

The minmax(min, max) function sets a minimum and maximum size. Combine it with auto-fill or auto-fit to create responsive grids that adjust column count automatically.

responsive-grid.css Responsive grids with auto-fill and auto-fit

auto-fill vs auto-fit:

  • auto-fill — Creates as many columns as fit in the container, even if they're empty. Columns stay at their minimum size if there aren't enough items.
  • auto-fit — Creates columns for items, then collapses empty tracks. Remaining items stretch to fill available space.
Magic combo:
repeat(auto-fit, minmax(200px, 1fr)) creates a responsive grid that automatically adjusts from 1–4+ columns based on container width. No media queries needed!

Try it yourself: Responsive grid with auto-fit

Resize the preview or change the minmax() values. The grid automatically adjusts columns without any media queries. Try minmax(150px, 1fr) for more columns, or minmax(300px, 1fr) for fewer.

✏️ Try It Yourself — Responsive grid
Preview

gap — Adding space between grid items

The gap property adds space between columns and rows. You can use gap (both axes) or column-gap and row-gap separately.

gap.css Spacing between grid items

Benefits of using gap over margin:

  • gap only applies between items, not around the grid.
  • No need for margin tricks or negative margins.
  • Works with any number of items automatically.
Mini Challenge: Build a responsive photo gallery

Task: Create a 12-item photo gallery using CSS Grid that:

  • Uses repeat(auto-fill, minmax(200px, 1fr)) for responsive columns
  • Has gap: 16px between items
  • Uses different colors for each of the 12 boxes
  • Grid automatically adjusts from 1–4+ columns based on container width

Build it in one of the editors above, or use your own editor. Test by resizing the preview or browser window!

Hint: Start with a <div class="gallery"> as your grid container. Create 12 child divs. Use the CSS from the "Responsive grid" editor above, and add different background colors using :nth-child() selectors.

Key takeaways

  • display: grid creates a grid container with 2D layout control.
  • grid-template-columns and grid-template-rows define track sizes.
  • The fr unit distributes available space proportionally.
  • repeat(3, 1fr) creates 3 equal columns; repeat(auto-fit, minmax(200px, 1fr)) creates responsive columns.
  • gap adds space between items without using margins.
  • auto-fit vs auto-fill: auto-fit collapses empty tracks; auto-fill keeps them.

Test your knowledge

Question 1 of 5
What does display: grid create?
Question 2 of 5
What does 1fr mean in grid-template-columns?
Question 3 of 5
What does repeat(3, 1fr) create?
Question 4 of 5
What's the difference between auto-fill and auto-fit?
Question 5 of 5
Which property adds space between grid items?
0
/5
Great work! You're mastering CSS Grid.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You just mastered CSS Grid.
Imagine what layouts you'll build next.

Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses →