CSS Grid, the grid layout system in Cascading Style Sheets (CSS), lets you lay out a page, or a small part of one, in rows and columns at the same time. This guide walks through the core ideas: making a grid container, sizing columns with the fr unit, adding gaps, placing items on grid lines, naming areas, and building a card layout that adds or removes columns to fit the space.
The words you need
MDN Web Docs’ Basic concepts of grid layout describes CSS grid layout as a two-dimensional grid system for CSS: a set of intersecting horizontal and vertical lines that define rows and columns. That is the key difference from flexbox, which handles one row or one column at a time. A few terms come up again and again:
- Grid lines are the lines that divide the grid.
- A track is the space between two neighboring lines: a row or a column.
- A cell is the smallest unit, like a table cell.
- An area is one or more cells grouped together. Areas must be rectangular.
Step 1: Make a grid container
Set display: grid on a parent element, and all of its direct children become grid items. At first nothing seems to change: without column definitions you get a single-column grid, so the items still stack. Most browser developer tools can overlay the grid on the page so you can see its lines.
Step 2: Define columns with fr and repeat()
Columns are set with grid-template-columns and rows with grid-template-rows. You can use any length, such as 200px 200px 200px, but grid adds a flexible unit, fr, which represents a fraction of the available space. 2fr 1fr 1fr splits the space into four parts and gives two of them to the first column. Fixed and flexible sizes mix: in 500px 1fr 2fr, the 500 pixels are taken first and the rest is shared. The repeat() function saves typing:
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: minmax(100px, auto);
gap: 16px;
}
This creates three equal columns. Only the columns are defined, so the browser creates rows as needed. MDN calls these the explicit grid (what you define) and the implicit grid (tracks the browser adds). Here grid-auto-rows uses minmax() so each new row is at least 100 pixels tall but grows to fit taller content.
The gap property, covered in MDN’s gap reference, sets the space between rows and columns. With two values, the first is the gap between rows and the second the gap between columns. Gaps appear only between tracks, never before the first or after the last, and nothing can be placed in them.
Step 3: Place items on grid lines
When you define tracks, grid numbers the lines for you, starting at 1. A three-column grid has four column lines. In a left-to-right language, line 1 is on the left; in a right-to-left language, it is on the right. To place an item, give its start and end lines:
.featured {
grid-column: 1 / 4;
grid-row: 1 / 3;
}
This item runs from column line 1 to line 4, across all three columns, and from row line 1 to line 3, covering two rows. Items you do not place fill the empty cells on their own.
Step 4: Name areas with grid-template-areas
The MDN learning module on grids shows another way to place items: draw the layout with names.
.page {
display: grid;
grid-template-columns: 1fr 3fr;
grid-template-areas:
"header header"
"sidebar content"
"footer footer";
}
header { grid-area: header; }
aside { grid-area: sidebar; }
main { grid-area: content; }
footer { grid-area: footer; }
Each quoted string is one row. MDN gives the rules: every cell must be filled; repeat a name to span cells; use a period (.) to leave a cell empty; areas must be rectangular; and the same name cannot appear in two separate places.
Step 5: Fit as many columns as the space allows
Combining repeat() and minmax() gives a layout that adapts to the width of its container:
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
gap: 20px;
}
The auto-fit keyword asks the grid to create as many 230-pixel columns as will fit, then share the leftover space among them, because the maximum is 1fr.
Common mistakes
- Counting tracks instead of lines. To span three columns, an item runs from line 1 to line 4, not 1 to 3.
- Expecting nested elements to follow the grid. Only direct children are grid items. A nested grid does not inherit the parent’s gap, and its lines do not line up with the parent’s unless you use
grid-template-columns: subgrid. - Drawing L-shaped areas. Areas in
grid-template-areasmust be rectangles, and every cell needs a name or a period. - Using the old property name. Early versions of the specification called the property
grid-gap. Browsers still accept it as an alias, butgapis the current name.
Grid works best when you think in rows and columns together: define the tracks on the container, let items flow into cells, and place only the items that need a special position. Start with repeat(), fr and gap, then add line numbers or named areas as your layout grows.


