Skip to content
Tutorials Center
My learning

Topics

Lesson 74 of 139 ยท Grid

Grid Tracks: fr, repeat() and minmax()

Size CSS Grid columns and rows with the fr unit, repeat(), minmax() and intrinsic keywords, and learn exactly when auto-fill and auto-fit behave differently.

A grid is only as good as its tracks. Fixed pixel columns break the moment the screen changes size, so Grid comes with its own sizing toolkit: the fr unit for sharing space, repeat() for writing many tracks at once, minmax() for setting limits, and the auto-fill and auto-fit keywords that let the browser decide how many columns fit.

Together these tools let you build layouts that adapt to any width, often without a single media query. This lesson takes them one at a time and then combines them into the most famous line in modern CSS.

The fr unit

fr stands for fraction. It shares out the free space in the grid container: whatever is left after fixed tracks and gaps are subtracted.

CSS
.grid {
  grid-template-columns: 1fr 1fr 1fr;   /* three equal columns */
  grid-template-columns: 2fr 1fr;       /* the first is twice as wide */
  grid-template-columns: 240px 1fr;     /* fixed sidebar, flexible main */
}

For 2fr 1fr, the browser adds up the fractions (3), divides the free space into three shares and gives two to the first column and one to the second.

Why not just use percentages? Because percentages ignore gaps. Three columns of 33.33% plus two 20px gaps add up to more than 100%, so the grid overflows. fr is calculated after gaps are removed, so it always fits.

Sharing space with fr

In the last row, 90px is set aside first, then the rest is split one share to two. Resize the preview: the fixed column never changes, the fr columns stretch.

1fr has a hidden minimum. A 1fr track will not shrink below the width of its longest unbreakable content, such as a long URL or a wide image. That can make "equal" columns unequal or cause overflow. Writing minmax(0, 1fr) removes that minimum and forces truly equal columns. You will see why in the minmax() section below.

Writing many tracks with repeat()

Typing 1fr twelve times is tedious and error-prone. repeat() takes a count and a track list:

CSS
.grid {
  grid-template-columns: repeat(4, 1fr);           /* 1fr 1fr 1fr 1fr */
  grid-template-columns: repeat(3, 100px 1fr);     /* 100px 1fr, three times */
  grid-template-columns: 200px repeat(2, 1fr);     /* mix with other tracks */
}

The second line repeats a pattern, giving six columns that alternate between 100px and flexible. repeat() works in grid-template-rows too.

Intrinsic sizes: auto, min-content, max-content

Not every track should be a fixed length or a fraction. Sometimes the content should decide.

  • min-content is as narrow as the content can get: text wraps at every opportunity, so the track is as wide as the longest word.
  • max-content is as wide as the content wants with no wrapping at all.
  • auto behaves like a sensible mix: at least min-content, at most max-content, and it stretches to take up leftover space when there are no fr tracks.
  • fit-content(300px) is like auto but never grows past the length you give it.
Content-sized tracks

min-content wraps the label onto two lines; max-content keeps it on one; fit-content(120px) grows with the text but stops at 120px and wraps after that. These keywords are perfect for label columns in forms or for a sidebar that should be just wide enough for its menu.

Setting limits with minmax()

minmax(min, max) makes a track that is never smaller than min and never larger than max. It is the key to flexible grids that do not collapse.

CSS
.grid {
  /* rows are at least 100px, but grow to fit content */
  grid-auto-rows: minmax(100px, auto);

  /* a sidebar between 180px and 300px, main column takes the rest */
  grid-template-columns: minmax(180px, 300px) 1fr;

  /* truly equal columns that can shrink below their content */
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

That last line explains the earlier warning. A plain 1fr is actually shorthand for minmax(auto, 1fr), and auto as a minimum means "no smaller than the content". Changing the minimum to 0 lets each column shrink evenly, and long content wraps or overflows inside its own cell instead of pushing the column wider.

auto-fill and auto-fit: let the browser count

Here is where Grid gets clever. Instead of a number, repeat() accepts auto-fill or auto-fit. The browser then creates as many tracks as will fit in the container.

CSS
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
}

Read it aloud: "Make as many columns as will fit, where each column is at least 140px and they share any leftover space equally." On a wide screen you might get five columns; on a phone, two; in a very narrow box, one. No media queries needed. This pattern is so common it has become a signature of modern CSS.

A gallery that counts its own columns

The difference between them

auto-fill and auto-fit only behave differently when there are fewer items than columns that would fit.

  • auto-fill keeps the empty columns. The items stay at their minimum-ish width and the extra space is left empty at the end of the row.
  • auto-fit collapses the empty columns to zero, so the 1fr maximum lets the existing items stretch across the full width.
auto-fill vs auto-fit with only two items

A practical rule: use auto-fill when you want cards to keep a consistent size no matter how many there are (a product grid), and auto-fit when a few items should fill the row (a set of feature boxes).

Avoiding overflow on tiny screens

minmax(240px, 1fr) has a catch: if the container is narrower than 240px, the single column still demands 240px and overflows. Wrap the minimum in min() so it can never exceed the container:

CSS
.cards {
  grid-template-columns: repeat(auto-fill, minmax(min(240px, 100%), 1fr));
}

Now the minimum is 240px or the full container width, whichever is smaller. The min() function is explained in calc(), min(), max() and clamp().

Quick reference

You want Write
N equal columns repeat(N, 1fr)
Equal columns that ignore long content repeat(N, minmax(0, 1fr))
Fixed sidebar + flexible main 250px 1fr
Column sized by its content auto, max-content or fit-content(Xpx)
Rows at least X tall grid-auto-rows: minmax(X, auto)
As many columns as fit repeat(auto-fill, minmax(min(X, 100%), 1fr))

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A container is 520px wide with grid-template-columns set to 100px 1fr 3fr and a 10px gap. How wide is the 3fr column?

    Free space is 520 โˆ’ 100 โˆ’ 2 ร— 10 = 400px. Split into 4 shares of 100px; the 3fr column gets 300px.
  2. 2. Why can three 1fr columns end up with different widths?

    1fr means minmax(auto, 1fr). Long words or wide images set a minimum. Use minmax(0, 1fr) to force equal columns.
  3. 3. You have three cards and room for six columns. Which value lets the three cards stretch across the full width?

    auto-fit collapses the empty columns, so the 1fr maximum spreads the three cards over the whole row. auto-fill keeps the empty columns.
  4. 4. What does minmax(min(220px, 100%), 1fr) protect against?

    Without min(), the column demands at least 220px even in a 180px box. min(220px, 100%) lowers the minimum to the container width when needed.
  5. 5. Which track size makes a column exactly as wide as its content without wrapping?

    max-content is the width of the content laid out on one line. min-content is the narrowest the content can wrap to.

Summary

  • fr shares the free space left after fixed tracks and gaps, so it never overflows the way percentages can.
  • repeat(count, tracks) writes many tracks at once and can repeat a pattern.
  • min-content, max-content, auto and fit-content() size tracks by their content.
  • minmax(min, max) sets limits; minmax(0, 1fr) gives truly equal columns.
  • repeat(auto-fill | auto-fit, minmax(โ€ฆ)) lets the browser choose the column count. auto-fit collapses empty tracks; auto-fill keeps them.
  • Wrap the minimum in min(X, 100%) to avoid overflow in narrow containers.

Next: Placing Grid Items, where you put items exactly where you want them using line numbers and spans.