CSS Grid layout - defining rows and columns, the fr unit, grid areas, gap, auto-fit/auto-fill, minmax(), and implicit vs explicit grids.
CSS Grid
CSS Grid is a two-dimensional layout system. While Flexbox handles rows or columns, Grid handles rows and columns at the same time. It is the most powerful layout tool in CSS.
When to use Grid vs Flexbox
In practice, you often use both together: Grid for the overall page layout, Flexbox for smaller components inside it.
Interactive demo
Try fixed tracks vs auto-fit + minmax(), then shrink the container width:
Grid container and grid items
Set display: grid on a container. Its direct children become grid items:
By default, grid items stack vertically (one per row), just like block elements. You need to define columns and rows to see the grid effect.
Defining columns and rows
grid-template-columns
Defines the number and width of columns:
This creates three columns, each 200px wide.
grid-template-rows
Defines the number and height of rows:
Two rows, each 100px tall.
The fr unit
The fr (fraction) unit distributes available space proportionally. It is the most important unit in Grid:
Three equal-width columns that fill all available space. 1fr 2fr 1fr gives the middle column twice the width of the
side columns.
You can mix fr with fixed units:
A fixed 250px sidebar and a main content area that takes the remaining space.
The repeat() function
Shorthand for repeating track definitions:
Equivalent to 1fr 1fr 1fr. You can repeat any pattern:
Gap
Just like Flexbox, gap adds space between grid items:
Set row and column gaps separately:
Placing items
Grid items automatically fill cells left to right, top to bottom. You can override this with placement properties.
Line-based placement
Grid lines are numbered starting at 1. Use grid-column and grid-row to place items:
This means: start at column line 1, end at column line 4 - spanning all three columns in a 3-column grid.
The sidebar occupies column 1 and spans rows 2 through 3.
The span keyword
Instead of specifying end lines, you can use span:
span 3 means "occupy three columns from wherever the item is placed."
Grid template areas
For complex layouts, named areas are the most readable approach:
The grid-template-areas property draws a visual map of the layout. Each string is a row; each word is a cell. Repeat
a name to span multiple cells. Use a period (.) for empty cells:
Tip: Grid template areas are excellent for page-level layouts. The visual representation in the CSS directly mirrors the visual result, making the code self-documenting.
Alignment in Grid
Grid supports the same alignment properties as Flexbox, plus a few extras.
Container-level alignment
Item-level alignment
The place shorthand
place-items, place-content, and place-self combine the align and justify versions:
Responsive grids with auto-fit and auto-fill
The most powerful responsive Grid pattern uses auto-fit or auto-fill with minmax():
This creates a responsive grid where:
- Each column is at least 280px wide
- Columns expand to fill available space (up to
1fr) - The browser automatically creates as many columns as fit
- No media queries needed
auto-fit vs auto-fill
In most cases, auto-fit is what you want.
minmax()
The minmax(min, max) function sets a minimum and maximum size for a grid track:
The first column is at least 200px but can grow up to 1fr. The second column gets 3fr.
Implicit vs explicit grid
The explicit grid is what you define with grid-template-columns and grid-template-rows. If items overflow past
your defined tracks, the browser creates implicit tracks automatically.
Control the size of implicit tracks:
grid-auto-rows: 150px makes any extra rows 150px tall. Without it, implicit rows auto-size to fit their content.
Use minmax() for flexible implicit rows:
Practical examples
Photo gallery
Holy Grail layout
The classic header/sidebar/main/footer layout:
Dashboard with different-sized cards
What you learned
- CSS Grid is two-dimensional - it handles rows and columns
grid-template-columnsandgrid-template-rowsdefine the grid structure- The
frunit distributes space proportionally;repeat()avoids repetition gapadds space between tracks- Items can be placed by line numbers (
grid-column: 1 / 3), by span (span 2), or by named areas auto-fitwithminmax()creates responsive grids without media queries- Implicit tracks handle overflow items automatically
Next step
Grid and Flexbox handle layout. The next chapter covers responsive design - media queries, breakpoints, and mobile-first development to make your layouts work on every screen size.