Page layouts used to be built from floats, clearfix hacks and nested wrappers that each solved one row. CSS Grid replaced all of that with a single container that knows about rows and columns at the same time. A dashboard with a header, sidebar, main area and footer takes a handful of lines, and a responsive card gallery can adapt to any width without a single media query.
Grid has its own vocabulary, though, and the syntax for placing items can look like arithmetic. This guide explains the core concepts, from tracks and fr units to line numbers and named areas, and shows the patterns you will use most.
The core vocabulary
- Grid container: the element with
display: grid. Its direct children become grid items. - Tracks: the rows and columns, defined with
grid-template-columnsandgrid-template-rows. - Grid lines: the numbered lines between tracks. A three-column grid has column lines 1 to 4.
- Cells: the space where one row and one column cross.
- Areas: rectangles of one or more cells that an item occupies.
- Gap: the space between tracks, set with
gap(orrow-gapandcolumn-gap).
A minimal grid:
.layout {
display: grid;
grid-template-columns: 240px 1fr;
grid-template-rows: auto 1fr auto;
gap: 16px;
min-height: 100vh;
}
That gives a fixed 240px sidebar column, a flexible main column, and three rows: header, content that fills the remaining height, and footer.
Sizing tracks
The fr unit
fr means a fraction of the leftover space after fixed tracks and gaps are subtracted. 1fr 2fr splits the remainder one third to two thirds. 240px 1fr gives the second column everything the first does not use.
One subtlety: a 1fr track has a minimum size of auto, which means it will not shrink below its content's minimum width. A long URL or a wide table can push a "flexible" column wider than you expect. Writing minmax(0, 1fr) instead lets it shrink and makes overflow behave predictably.
auto, min-content and max-content
autosizes to content but can stretch to fill space.min-contentis as narrow as the content allows (the longest word).max-contentis as wide as the content wants without wrapping.
minmax()
minmax(min, max) gives a track a range. minmax(200px, 1fr) means "at least 200px, otherwise share the space". For rows, minmax(48px, auto) keeps empty rows from collapsing.
repeat() and responsive grids
repeat(4, 1fr) is shorthand for four equal columns. Combined with auto-fit and minmax(), it creates a gallery that adjusts its column count automatically:
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 1.5rem;
}
The browser fits as many 220px-or-wider columns as the container allows, then stretches them to fill the row. auto-fill behaves the same but keeps empty column slots when there are few items, while auto-fit collapses them so items stretch.
Placing items
By default, items flow into the next free cell, row by row. To place an item explicitly, refer to grid lines.
Line numbers
.header {
grid-column: 1 / 5; /* from column line 1 to line 5: four columns */
grid-row: 1 / 2;
}
The grid-area shorthand combines all four values in the order row-start / column-start / row-end / column-end:
.sidebar {
grid-area: 2 / 1 / 4 / 2;
}
That item starts at row line 2 and column line 1 and ends at row line 4 and column line 2, so it spans two rows in the first column. Negative numbers count from the end: grid-column: 1 / -1 spans the full width, whatever the column count. span 2 is another option: grid-column: span 2 takes two columns from wherever the item lands.
Named template areas
For page layouts, naming areas makes the CSS read like a picture of the layout:
.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"nav main"
"footer footer";
gap: 1rem;
}
.page > header { grid-area: header; }
.page > nav { grid-area: nav; }
.page > main { grid-area: main; }
.page > footer { grid-area: footer; }
@media (max-width: 40em) {
.page {
grid-template-columns: 1fr;
grid-template-areas:
"header"
"main"
"nav"
"footer";
}
}
Each string is a row; each word is a cell. Areas must be rectangles. A . marks an empty cell. Rearranging the whole layout for small screens only needs a new set of strings.
Build a grid visually
Line numbers are easiest to get right when you can see them. The CSS Grid Generator lets you draw the layout and copy the code.
- Open the CSS Grid Generator. Use the plus and minus buttons in Columns and Rows to set the number of tracks (up to 12 each).
- Type a size for each track. Sizes accept
fr,px,%,rem,auto,min-content,max-content,minmax(…)andfit-content(…). - Set Column gap and Row gap with the sliders.
- On the Grid canvas, click and drag across cells to create an item. Repeat for each region of your layout.
- Rename each item's class in the list below the canvas, or remove it. Clear items starts over.
- Switch between the CSS and HTML tabs to copy or download the container rules, the
grid-areafor each item, and matching markup.
The generated CSS uses line-number grid-area values. If you prefer named areas, the positions translate directly into a grid-template-areas map like the one above.
Grid or flexbox?
They are complementary, not competitors:
- Flexbox is one-dimensional. It lays items out in a row or a column and is ideal for toolbars, navigation, button groups and centring things. Content size drives the layout.
- Grid is two-dimensional. It controls rows and columns together, so items align across both. The layout drives where content goes.
A common pattern is grid for the page skeleton and card galleries, flexbox inside each card or header. Try the flex side of things in the flexbox playground.
Pitfalls and troubleshooting
Only direct children are grid items
Grid properties apply to the container's children, not grandchildren. If a wrapper div sits in between, the inner elements ignore the grid. Remove the wrapper, give it display: contents with care, or use subgrid where supported.
Columns overflow on small screens
Look for a 1fr column with long unbreakable content, or a fixed pixel track that is wider than the phone. Use minmax(0, 1fr) and allow long words to wrap with overflow-wrap: anywhere.
Items overlap unexpectedly
Two items placed on the same lines will stack. That can be intentional (overlays, captions on images), but if not, check the line numbers. Browser DevTools have a grid inspector that draws the lines and numbers on the page.
Gaps in rem or px
Gaps accept any length. Using rem keeps spacing in proportion with text size; the guide to rem, em and px covers the trade-offs, and the PX to REM converter converts values. Tidy the final stylesheet with the CSS formatter and the markup with the HTML formatter.
Frequently asked questions
What does 1fr mean in CSS Grid?
It is one share of the free space left after fixed-size tracks and gaps. Two 1fr columns split that space equally.
What is the order of values in grid-area?
Row start, column start, row end, column end, separated by slashes. When the value is a single name, it refers to a named template area instead.
What is the difference between auto-fit and auto-fill?
Both create as many columns as fit. auto-fill keeps empty tracks when there are not enough items; auto-fit collapses them so the existing items stretch to fill the row.
Can I make a responsive grid without media queries?
Yes. grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)) adapts the column count to the available width.
Should I use grid or flexbox?
Use grid when you need rows and columns aligned together, such as page layouts and galleries. Use flexbox for single rows or columns of items, such as navigation and toolbars.