SpunkArt
Front-end and everyday web

CSS grid layout: tracks, the fr unit and named areas

CSS grid controls rows and columns at the same time. You draw the tracks on the parent, then place children into cells, lines or named areas.

Vocabulary first

TermMeaning
TrackOne row or one column
LineThe numbered boundary between tracks, starting at 1
CellWhere one row track and one column track cross
AreaA rectangle of one or more cells, optionally named
GutterThe gap between tracks

A grid with three columns has four column lines. Line -1 always means the last line, which is handy for spanning the full width with grid-column: 1 / -1.

Sizing tracks with fr

The fr unit is a share of the space that remains after fixed tracks and gaps are taken out.

leftover = container − fixed tracks − gaps
1fr = leftover ÷ total fr

Example. A 1000px container uses grid-template-columns: 200px 1fr 2fr with column-gap: 20px.

  • There are three columns, so two gaps: 2 × 20 = 40px
  • Leftover: 1000 − 200 − 40 = 760px
  • Total fr is 3, so 1fr = 760 ÷ 3 ≈ 253.33px and 2fr ≈ 506.67px (rounded to two decimals)

Unlike flexbox grow, fr splits the whole leftover space, so 1fr 2fr really gives a 1 to 2 ratio, as long as no item’s content forces a track wider.

Responsive columns without media queries

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 20px;
}

The browser fits as many 200px-minimum columns as it can, then stretches them equally.

Example. The container is 1000px wide with a 20px gap.

  • Four columns need 4 × 200 + 3 × 20 = 860px, which fits.
  • Five columns need 5 × 200 + 4 × 20 = 1080px, which does not.
  • So there are four columns, each (1000 − 60) ÷ 4 = 235px wide.

auto-fill keeps empty tracks when there are few items; auto-fit collapses them so the items stretch to fill the row. With a full row of items the two look the same.

Named areas for page layouts

For a whole page, drawing the layout as text is often the easiest to read and to change later:

.page {
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-areas:
    "head head"
    "side main"
    "foot foot";
}
.page header { grid-area: head; }
.page aside  { grid-area: side; }
.page main   { grid-area: main; }
.page footer { grid-area: foot; }

@media (max-width: 700px) {
  .page {
    grid-template-columns: 1fr;
    grid-template-areas: "head" "main" "side" "foot";
  }
}

Each quoted string is one row, and every row must have the same number of cells. Areas must form rectangles; an L shape is invalid and the whole template is ignored.

Steps to plan a grid by hand

  1. Sketch the layout on paper and draw the lines, not the boxes.
  2. Count the column tracks and decide which are fixed and which share space.
  3. Write grid-template-columns, then rows only if they need fixed sizes.
  4. Place items with line numbers or area names.
  5. Turn on the grid overlay in your browser DevTools to see numbered lines.

Grid or flexbox?

Use grid when you care about both rows and columns lining up, such as a card gallery, a form with labels and fields, or a page skeleton. Use flexbox when content flows in one line and its own size should drive the layout, such as a toolbar. The two combine well: a grid card can use flexbox inside it.

Questions

Why is my 1fr column wider than the others?

A track sized with 1fr has a minimum of auto, so long content can stretch it. Use minmax(0, 1fr) to let the track shrink below its content width.

What is subgrid?

With grid-template-columns: subgrid, a nested grid reuses its parent’s tracks, so content inside separate cards can line up across the row.