Grid Fundamentals and Tracks

Beginner · 7 min

How a grid container defines rows and columns before placing items, what the fr unit shares out, and why 1fr columns can blow past their container.

Before thisDisplay and Normal Flow

Coding2GO · 21 min

Grid Fundamentals and Tracks

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Coding2GOReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Grid is layout-first: the container declares rows and columns (tracks), and items are then dropped into the cells. Flexbox works the other way, starting from the content.
  • `grid-template-columns: repeat(3, 1fr)` makes three tracks. `fr` divides the free space left after fixed tracks and gaps, so `200px 1fr 2fr` in an 800px container gives 200px, 200px and 400px.
  • `1fr` really means `minmax(auto, 1fr)`: a track will not shrink below its content's minimum size. A long unbreakable word can push the grid wider than its container. Use `minmax(0, 1fr)` to stop it.
  • Grid lines are numbered from 1, and `-1` is the last line of the explicit grid. `grid-column: 1 / -1` makes an item span every column, and `span 2` makes it two tracks wide.
  • Rows you never defined are created on demand (the implicit grid) and sized by `grid-auto-rows`, which defaults to `auto`.

Layout first, content second

Flexbox starts with items and asks how to line them up. Grid starts with a plan. You draw the lines on the page first, say three columns and two rows, and then say which cell each item goes in. Think of a sheet of graph paper: the grid container draws the lines, and the items are things you place on the squares.

.grid {
  display: grid;
  grid-template-columns: 200px 1fr 2fr;
  gap: 1rem;
}

The rows and columns are called tracks. The lines between them are grid lines, and a rectangular space bounded by four lines is a cell. Only the container’s direct children become grid items, as with flexbox.

Sizing tracks

A track can be a fixed length, a percentage, a content-based keyword (auto, min-content, max-content) or a flexible fraction:

  • auto lets the track fit its content and absorb spare space.
  • fr shares out free space. Free space is the container’s size minus fixed tracks and gaps.
  • minmax(min, max) sets a range. minmax(200px, 1fr) means “at least 200px, otherwise share space”.
  • repeat(3, 1fr) is shorthand for 1fr 1fr 1fr.

Try the arithmetic. With 200px 1fr 2fr in an 800px container and no gap, 600px is left after the fixed track, split into three shares: 200px, 200px, 400px. A browser gave exactly those widths. With a 1rem gap, subtract the gaps from the free space first.

The 1fr trap

1fr looks like “equal share”, but its minimum is not zero. The spec defines 1fr as minmax(auto, 1fr), and auto as a minimum means “no smaller than the content’s minimum size”. If a cell holds something that cannot wrap, the track grows to fit it:

.grid { display: grid; grid-template-columns: 1fr 1fr; width: 300px; }
/* one cell holds a 70-character unbroken string: the first column became 664px wide */

.grid { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
/* 150px and 150px, and the long string overflows its own cell instead */

That’s the same issue as min-width: auto in flex items. Using minmax(0, 1fr) is a good default when the cells hold user content.

Lines, spans and the implicit grid

Grid lines are numbered from 1 on the left and top. Negative numbers count from the end of the explicit grid, so -1 is the last line.

.hero   { grid-column: 1 / -1; }      /* every column */
.wide   { grid-column: span 2; }      /* two columns wide, wherever it lands */
.tall   { grid-row: 2 / span 2; }     /* start at line 2, cover two rows */

You only need to define the tracks you care about. If there are more items than cells, the browser adds rows (the implicit grid) and sizes them with grid-auto-rows:

.grid { grid-template-columns: repeat(2, 1fr); grid-auto-rows: 50px; }

In a test with grid-template-rows: 30px and four items in two columns, the first row was 30px and the second 50px from grid-auto-rows. Placement of items you don’t position yourself is covered in auto-placement and responsive grids.

Aligning inside cells and the grid

Two sets of properties align things:

  • justify-items and align-items align each item inside its cell. place-items: center sets both.
  • justify-content and align-content position the whole grid inside the container, which only matters when the tracks add up to less than the container.

place-items: center on a grid container centers its child in a single cell, which is the shortest way to center something in CSS.

What the video covers

The embed is a clip, 0:00 to 6:32, from Coding2GO’s “CSS Grid Course - The Only Grid Tutorial You’ll Ever Need!” (October 2025, about twenty-one minutes). Its chapters are Grid Basics, Responsive Columns & Fractions, Alignment (How to center a grid), Implicit Grids (auto-columns, auto-rows, auto-flow), Create Bento Grids with grid-template-areas, Grid Stacking & Overlapping Grid-Items, and Grid Wrapping with repeat(), auto-fit & minmax(). The clip takes the first three, which match this lesson. The rest of the video lines up with the next lessons, so you can keep watching. I could not read a transcript, so I don’t describe the demos. The 1fr trap and the arithmetic are the prose’s own and were checked in a browser.

Where this fits

Grid builds on display and normal flow. The next lesson, grid areas and named lines, gives the lines and regions names. The frontend roadmap’s flexbox and grid layout lesson is a shorter first pass at the same material.

Check yourself

  1. 01Columns are `200px 1fr 2fr` in an 800px grid with no gap. How wide is each?

    200px, 200px and 400px. The fixed 200px track is subtracted first, leaving 600px, which is divided into 3 shares of 200px; the `1fr` column gets one share and the `2fr` column two.

  2. 02You use `grid-template-columns: 1fr 1fr` and one cell contains a very long URL. The columns are no longer equal and the grid overflows. Why?

    A `fr` track has an automatic minimum of its content's minimum size, so `1fr` is `minmax(auto, 1fr)`. The long URL can't break, so that track stays wide. Write `minmax(0, 1fr)` to remove the content minimum, then handle the overflow with `overflow-wrap` or `overflow: hidden`.

  3. 03What does `grid-column: 1 / -1` do?

    It places the item from the first grid line to the last line of the explicit grid, so it spans every column you defined. It won't reach implicit columns created by auto-placement, since `-1` refers only to the explicit grid.

  4. 04A grid defines `grid-template-rows: 30px` and holds four items in two columns. How tall are the second row's cells?

    Whatever `grid-auto-rows` says. The second row is implicit. With `grid-auto-rows: 50px` it is 50px tall; with the default `auto` it is as tall as its tallest item.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark