CSS Grid: grid-template-columns, fr and repeat() Explained
Contents
- grid-template-columns
- Fixed, percentage and flexible columns
- The fr unit
- repeat()
- minmax()
- Responsive grid without media queries
- repeat(auto-fill, minmax())
- auto-fill vs auto-fit
- Rows, gaps and placement
- grid-template-rows
- gap
- Span columns and rows
- grid-template-areas
- Center anything with place-items
- Grid vs Flexbox
- FAQ
CSS Grid is a two-dimensional layout system: you define columns and rows on a parent, and its children snap into the cells. Most grids need just two lines:
display: grid; and grid-template-columns: repeat(3, 1fr);. That’s three equal columns.
This guide covers the properties you’ll use every day, with a demo you can copy: grid-template-columns, the fr unit, repeat(), minmax(), named areas and the one-line responsive grid.
Demo:
The outer grid uses named areas for a classic page layout. The cards inside use the one-line responsive pattern: resize the window and the number of columns changes on its own.
grid-template-columns
grid-template-columns defines how many columns the grid has and how wide each one is. Every value you list is one column.
Fixed, percentage and flexible columns
You can mix any units. Fixed sizes stay fixed, and fr columns share whatever space is left.
.grid {
display: grid;
/* 3 columns: 200px, 50% of the grid, and the rest */
grid-template-columns: 200px 50% 1fr;
/* Sidebar layout: fixed sidebar, flexible content */
grid-template-columns: 250px 1fr;
}
The fr unit
fr means “a fraction of the free space”. The browser first gives fixed columns their size, then splits what’s left between the fr columns in proportion. 1fr 2fr makes the second column twice as wide as the first.
fr vs %: percentages are calculated from the whole grid width and ignore gap, so 25% 25% 25% 25% with a gap overflows. fr is calculated after gaps and fixed columns, so repeat(4, 1fr) always fits. (If you really need percentage-based tracks, subtract the gaps with calc().)
.grid {
display: grid;
grid-template-columns: 1fr 2fr 1fr; /* 25% / 50% / 25% of the free space */
gap: 16px; /* no overflow: fr accounts for it */
}
repeat()
repeat(count, size) saves you writing the same column over and over. It can also repeat a pattern of several columns.
.grid {
grid-template-columns: repeat(4, 1fr); /* 1fr 1fr 1fr 1fr */
grid-template-columns: repeat(2, 100px 1fr); /* 100px 1fr 100px 1fr */
grid-template-columns: 200px repeat(3, 1fr); /* fixed + 3 equal */
}
minmax()
minmax(min, max) lets a column flex between two sizes. A very common one is minmax(0, 1fr): a plain 1fr column won’t shrink below its longest word or widest image, so long URLs or code blocks can blow it up. minmax(0, 1fr) fixes that.
.grid {
/* At least 200px, at most an equal share */
grid-template-columns: repeat(3, minmax(200px, 1fr));
/* Equal columns that never overflow because of long content */
grid-template-columns: repeat(3, minmax(0, 1fr));
}
Responsive grid without media queries
repeat(auto-fill, minmax())
This one line gives you a fully responsive card grid: as many columns as fit, each at least 250px wide, all stretching to fill the row. On a phone you get one column, on a laptop three or four.
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 24px;
}
/* On screens narrower than 250px, avoid overflow */
.cards {
grid-template-columns: repeat(auto-fill, minmax(min(250px, 100%), 1fr));
}
auto-fill vs auto-fit
They only differ when there are fewer items than columns that fit. auto-fill keeps the empty columns, so a single card stays card-sized. auto-fit collapses the empty columns, so the cards stretch to fill the whole row. Use auto-fill for grids of cards, and auto-fit when a few items should spread out.
/* 2 items in a wide container: */
.fill { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); } /* 2 small cards + empty space */
.fit { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); } /* 2 cards, each half the row */
Rows, gaps and placement
grid-template-rows
Works exactly like grid-template-columns, for rows. You usually don’t need it: rows are created automatically and sized to their content. Use it for fixed-height layouts, or grid-auto-rows to size the automatic rows. For a sideways-scrolling row, use grid-auto-flow: column with overflow-x: auto, and hide the scrollbar if it gets in the way.
.app {
display: grid;
grid-template-rows: auto 1fr auto; /* header, content fills, footer */
min-height: 100dvh;
}
.gallery {
display: grid;
grid-auto-rows: 200px; /* every automatic row is 200px tall */
}
gap
gap sets the space between rows and columns, never at the outer edges. It works the same way in flexbox. One value sets both; two values set row-gap then column-gap.
.grid {
gap: 16px; /* rows and columns */
gap: 32px 16px; /* 32px between rows, 16px between columns */
}
Span columns and rows
Grid lines are numbered from 1, and negative numbers count from the end, so 1 / -1 always means “the full width”.
.featured {
grid-column: span 2; /* 2 columns wide */
grid-row: span 2; /* 2 rows tall */
}
.full-width {
grid-column: 1 / -1; /* from the first line to the last */
}
grid-template-areas
Name the regions of your layout and draw them as text. Each string is a row, each word a cell, and a . leaves a cell empty. Then assign elements with grid-area. Changing the layout at a breakpoint becomes a matter of redrawing the strings. Or draw the areas visually with my CSS grid generator and copy the code.
.page {
display: grid;
grid-template-columns: 250px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
.page > header { grid-area: header; }
.page > aside { grid-area: sidebar; }
.page > main { grid-area: main; }
.page > footer { grid-area: footer; }
/* Mobile: stack everything */
@media (max-width: 700px) {
.page {
grid-template-columns: 1fr;
grid-template-areas: "header" "main" "sidebar" "footer";
}
}
Center anything with place-items
place-items is shorthand for align-items and justify-items. On a grid, place-items: center is the shortest way to center a div both ways.
.center {
display: grid;
place-items: center;
min-height: 300px;
}
Grid vs Flexbox
Grid is two-dimensional: you define the columns and rows first, and items fill them. Flexbox is one-dimensional: items flow in a single row or column and size themselves from their content. Use Grid for page layouts and anything that must line up in both directions (card grids, dashboards, forms), and Flexbox inside components (navigation bars, buttons with icons, toolbars).
FAQ
What does 1fr mean in CSS?
One fraction of the free space in the grid. After fixed-size columns and gaps are taken out, the rest is split between the fr columns in proportion to their numbers.
How do I make a responsive grid without media queries?
Use grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)). The browser fits as many 250px+ columns as it can and stretches them to fill the row.
What’s the difference between auto-fill and auto-fit?
With few items, auto-fill keeps empty columns (items stay their minimum size), while auto-fit collapses them (items stretch to fill the row). With enough items to fill the row, they look the same.
Why does my 1fr column overflow?
A 1fr column can’t shrink below its content’s minimum width, like a long URL or a wide pre block. Use minmax(0, 1fr) instead. Then you can truncate text inside it with an ellipsis.
Is CSS Grid supported in all browsers?
Yes. CSS Grid has worked in all major browsers since 2017.
Related: CSS Flexbox guide, CSS calc() and CSS tips you won’t see in most tutorials.