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.
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.
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).
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.
You can use multiple unit types:
px— Fixed pixels (200px)%— Percentage of container width (25%)auto— Grows/shrinks to fit contentfr— 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.
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.
The repeat() function
Typing 1fr 1fr 1fr 1fr 1fr is tedious. The repeat() function lets you repeat a pattern. Syntax: repeat(count, value)
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.
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.
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.
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.
Benefits of using gap over margin:
gaponly applies between items, not around the grid.- No need for margin tricks or negative margins.
- Works with any number of items automatically.
Task: Create a 12-item photo gallery using CSS Grid that:
- Uses
repeat(auto-fill, minmax(200px, 1fr))for responsive columns - Has
gap: 16pxbetween 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: gridcreates a grid container with 2D layout control.grid-template-columnsandgrid-template-rowsdefine track sizes.- The
frunit distributes available space proportionally. repeat(3, 1fr)creates 3 equal columns;repeat(auto-fit, minmax(200px, 1fr))creates responsive columns.gapadds space between items without using margins.auto-fitvsauto-fill: auto-fit collapses empty tracks; auto-fill keeps them.
Test your knowledge
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 →