Skip to content

CSS Grid: rows and columns at once

Working knowledgeConcept

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:

page.html
<div class="page">
<header>Shop</header>
<nav>Menu</nav>
<main>Products</main>
<footer>Help</footer>
<aside>Ad</aside>
</div>

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.

Column lines for grid-template-columns: 200px 1fr 1fr
LineFrom the endWhere it is
1-4Left edge of the grid
2-3Between the 200px track and the first 1fr
3-2Between the two 1fr tracks
4-1Right 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.

Sizing 100px 1fr 2fr in an 800px container
Step 1 of 6
the row, one cell per 50px (800px wide)
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
·
  • 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.

800px container

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.

1fr 1fr
.grid {
display: grid;
width: 300px;
grid-template-columns: 1fr 1fr;
}
/* first cell holds a 250px-wide box */
/* Chrome: 250px 50px */
minmax(0, 1fr)
.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.

page.css
.page {
display: grid;
width: 800px;
height: 400px;
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: 60px 1fr 40px;
}
header { grid-row: 1; grid-column: 1 / -1; }
nav { grid-row: 2 / 4; grid-column: 1; }
main, footer { grid-column: span 2; }
/* aside: no placement */
Placing five items on the page grid
Step 1 of 6
the page grid: 3 explicit rows, room for a 4th
···
···
···
  • 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.

page.css
.page {
display: grid;
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: 60px 1fr 40px;
grid-template-areas:
"head head head"
"nav main main"
"nav foot foot";
}
header { grid-area: head; }
nav { grid-area: nav; }
main { grid-area: main; }
footer { grid-area: foot; }

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

FlexboxOne line, sized by contentItems share one row or column and each line sizes on its own. Good for toolbars, button groups and lists of tags whose sizes come from their content.
GridRows and columns togetherThe container fixes the tracks and items fit into them, so things line up in both directions. Good for page layouts, card galleries and forms with aligned labels.

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.

09 Check yourself

Question

A 1000px grid has grid-template-columns: 200px 1fr 3fr and column-gap: 40px. How wide is the 3fr column?

Question

With three column tracks, what does grid-column: 2 / -1 cover?

Question

Every cell of a 2 × 2 grid is full and a fifth item has no placement. What happens?

10 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?