CSS Grid: rows and columns at once
A page with a header, a sidebar and a footer needs rows and columns that line up with each other, and flexbox only does one line at a time. Grid sizes both directions at once. Here is how it turns 1fr into pixels, and how it decides where each item lands.
01 A layout flexbox has to fake
Picture a page: a header across the top, a sidebar down the left, the main content, and a footer under the content. With flexbox you build this from nested containers: a column for the page, a row inside it for sidebar and content, another column inside that. Each container sizes its own line and knows nothing about the others, so making the footer’s left edge line up with the content’s takes care.
With display: grid, the container defines the rows and the columns, and children are placed into that one shared grid. The structure lives in CSS, and the HTML can stay flat:
02 Lines, tracks and cells
grid-template-columns: 200px 1fr 1fr creates three columns. Each column is a track. The edges between and around tracks are lines, and they are what you place items by. Three tracks have four lines, numbered from 1 on the left. They can also be counted from the end with negative numbers, so -1 is always the last line of the explicit grid.
| Line | From the end | Where it is |
|---|---|---|
| 1 | -4 | Left edge of the grid |
| 2 | -3 | Between the 200px track and the first 1fr |
| 3 | -2 | Between the two 1fr tracks |
| 4 | -1 | Right edge of the grid |
A cell is where one row track crosses one column track. An item can cover several cells, but always a rectangle.
03 How fr shares out the space
fr means a fraction of the free space: what is left of the container after fixed tracks and gaps are paid for. Here is one row of columns, sized step by step.
- track sized now
- gap
- free space
- not sized yet
The container is 800px wide with grid-template-columns: 100px 1fr 2fr and column-gap: 50px. No track has a size yet.
Check any grid yourself: in the console, getComputedStyle(el).gridTemplateColumns returns the resolved track sizes in pixels. Chrome and Firefox also draw a grid badge next to the element in the Elements panel; click it to see line numbers on the page.
04 Why 1fr is not always equal
1fr is short for minmax(auto, 1fr). The auto minimum means a track never gets narrower than the smallest its content can be. If one cell holds something wide, its track takes that width and the other fr tracks share what remains.
.grid {display: grid;width: 300px;grid-template-columns: 1fr 1fr;}/* first cell holds a 250px-wide box *//* Chrome: 250px 50px */
.grid {display: grid;width: 300px;grid-template-columns:minmax(0, 1fr) minmax(0, 1fr);}/* same content *//* Chrome: 150px 150px */
Both results are real getComputedStyle readings. With minmax(0, 1fr) the columns stay equal and the wide box overflows its cell instead. Which one you want depends on the content, but long URLs, code blocks and wide tables are the usual reason a "two equal columns" grid comes out lopsided.
05 Placing items by line
grid-column: 1 / 3 means "from column line 1 to column line 3". grid-row: 2 / 4 does the same for rows. span 2 means "two tracks, wherever you start". Leave both off and the item is placed automatically.
- placed now
- placed earlier
- empty cell
- track not created
Three columns 200px 1fr 1fr and three rows 60px 1fr 40px make a 3 × 3 explicit grid. The faded bottom row does not exist yet. Five items wait to be placed.
Note row 2. It was 1fr of a 400px container, but the implicit row needed 18.5px (one line of default text), so the free space shrank and row 2 came out 281.5px. An item you forgot to place can quietly resize the rows you did place.
06 Naming areas instead of counting lines
For a page layout, grid-template-areas draws the same grid as a picture in text. Each string is a row, each word a cell, and an item claims an area by name. An area must be a rectangle.
A change of layout for small screens is then a change of the picture inside one media query, and the HTML stays as it is.
07 Grid or flexbox
They work well together: a grid for the page, and flexbox inside the header to spread its links along one line.
08 When grid surprises you
grid-template-columns: 1fr 1fr; /* one cell holds a long URL */Columns that are not equal1fr has a minimum of auto, the content’s smallest size. Use minmax(0, 1fr) when equal columns matter more than fitting the content.grid-column: 1 / -1; /* with only grid-auto-columns */-1 only counts the explicit gridNegative line numbers count from the end of the explicit grid. If there are no template columns, or the item should cross implicit tracks, -1 is not the edge you expect..item { grid-column: 3 / span 3; } /* 3-column grid */Placing past the endThe grid does not clip the item. It adds implicit columns to hold it, which can push the layout wider than its container..grid { width: 100%; grid-template-columns: repeat(4, 25%); gap: 16px; }Percentages plus gapsFour 25% tracks already fill the width, and the three gaps are added on top, so the grid overflows by 48px. Use 1fr tracks, which share what is left after the gaps.aside { } /* no placement, grid full */Forgotten items make new rowsAuto-placement puts extra items into implicit rows, and those can take height from your fr rows, as the aside did above.