Skip to content

CSS Grid Explained: columns, fr units, repeat, minmax, spans and areas

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

Flexbox arranges items along one line. Many layouts are two-dimensional: a product listing where cards line up in neat columns and rows, a dashboard with a sidebar and a main area, a page with a header, content and footer. CSS Grid is built for exactly that. You describe the columns and rows on the parent, and the children fall into the cells. With one line of CSS you can build a card grid that shows four columns on a laptop and one on a phone, with no media queries at all.

Quick answer

Set display: grid on the parent and define columns with grid-template-columns, for example repeat(3, 1fr) for three equal columns. fr shares out the free space, gap spaces the cells, and repeat(auto-fit, minmax(14rem, 1fr)) makes as many columns as fit, which is a responsive card grid in one line. Items can span cells with grid-column: span 2, and grid-template-areas names whole regions of a page. Column 1 is on the right in dir="rtl" pages.

25 min read and practice · Content checked on 29 October 2026

In this lesson you will

  • Create a grid with columns, rows and gaps using fr, repeat() and minmax()
  • Build a responsive card grid in one line with auto-fit
  • Make items span several columns or rows and place them on grid lines
  • Lay out a whole page with named grid areas that also work in Arabic

Columns, rows and the fr unit

Like Flexbox, Grid has a container and items: display: grid goes on the parent, and its direct children become grid items. Nothing changes visually until you define tracks. grid-template-columns: 200px 200px 200px makes three columns 200 pixels wide; every child fills the next free cell, left to right in English and right to left in Arabic, then starts a new row. Rows are created automatically as needed, each as tall as its tallest item.

Fixed pixel columns do not adapt to the screen, so Grid adds the fr unit, a fraction of the free space. grid-template-columns: 1fr 1fr 1fr gives three equal columns that share the container’s width, and 2fr 1fr makes the first column twice as wide as the second. You can mix units: 16rem 1fr is a fixed sidebar beside a flexible main area. Because fr only shares space left after the gaps, columns never overflow the way percentage widths with padding can.

gap sets the space between cells, as in Flexbox; gap: 1rem 2rem sets the row gap then the column gap. repeat() saves typing: repeat(4, 1fr) is four equal columns. grid-template-rows defines rows the same way when you need fixed row heights, but most of the time letting rows size themselves to their content is the safer choice for text.

Try it: Three equal columns, then a sidebar layout
Code
ResultUpdates as you type
Console
  1. Console messages, errors, form data and clicked links appear here.

The fifth item starts a new row by itself. Try 2fr 1fr 1fr in the first grid, then add dir="rtl" to .split and the sidebar moves to the right.

Sign in free to read the full lesson

You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.

Unlocks after sign-in:

  • 3 more sections with live code examples
  • Common mistakes to avoid
  • Hands-on coding exercise with automatic checks
  • Practice quiz and the graded lesson test
Sign in or create a free account

Free forever. No card, no paywall.

Questions people ask

What does fr mean in CSS Grid?

fr stands for fraction. It shares out the free space in the grid container after fixed tracks and gaps are taken. 1fr 1fr 1fr gives three equal columns; 3fr 1fr gives the first column three quarters of the space.

What is the difference between auto-fit and auto-fill?

Both create as many columns as fit. With few items, auto-fit collapses the empty columns so the items stretch across the row, while auto-fill keeps the empty columns so each item stays the same size. With enough items to fill the row, they look the same.

Should I use Grid or Flexbox for a card layout?

Grid, when the cards should line up in columns. A wrapping flex row lets the last line stretch or sit unevenly; Grid keeps every card in a column. Inside each card, Flexbox is often the better tool for the icon, text and button.

Can I use CSS Grid in all browsers?

Yes. Grid has worked in Chrome, Edge, Firefox and Safari since 2017, and newer parts such as subgrid work in all current versions. Internet Explorer had an old, different version, but it is no longer supported by Microsoft.

How do I make a grid item span two columns?

Add grid-column: span 2 to the item. To span rows use grid-row: span 2. If the grid can shrink to one column on phones, reset the span inside a media query, or the item forces an extra column.

Sources

Facts in this lesson were checked against these official pages on 29 October 2026.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.