Tracks and the fr unit

Topic 2 of 4 in Grid fundamentals7 min readBeginnerFree course

Overview

The real power of grid is how it sizes tracks. You can mix fixed sizes, content-based sizes and flexible fractions in one definition, and the browser works out the rest. This lesson covers every track sizing tool and the one trap that catches everyone.

The fr unit

fr stands for fraction of the free space. After fixed-size tracks and gaps have been subtracted, the remaining space is shared between fr tracks in proportion to their values:

grid-template-columns: 200px 1fr 2fr with gap: 20px in an 800px container. The tracks measure 200px, 186.67px and 373.33px. 1. Subtract fixed tracks and gaps: 800 − 200 − 20 − 20 = 560px of free space. 2. Divide by the total fr: 560 ÷ 3 = 186.67px per fr. 3. Give each track its share: 1fr = 186.67px, 2fr = 373.33px. Measured in Chrome. Unlike %, fr never causes overflow when you add gaps.

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

fr vs percentages

repeat(3, 33.33%) plus a gap is wider than the container, because percentages don’t account for gaps — so the grid overflows. repeat(3, 1fr) divides only the space left after the gaps, so it always fits. Prefer fr for flexible tracks.

Content-based sizes

Value Track size
auto fits the content; as a maximum it can also stretch to fill leftover space
min-content the smallest the content can be — e.g. the longest word
max-content the content’s full size without wrapping
fit-content(20rem) like max-content, but no larger than 20rem
CSS
/* A label column as wide as its longest label, and a flexible input column */
.form {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.75rem 1rem;
}

auto tracks are particularly useful for rows: grid-template-rows: auto 1fr auto gives a header and footer exactly as tall as their content, and a middle row that takes everything else.

minmax()

minmax(min, max) gives a track a range:

CSS
grid-template-columns: minmax(12rem, 1fr) 3fr;   /* the first column: at least 12rem, otherwise 1 share */
grid-template-rows: minmax(4rem, auto);          /* at least 4rem tall, taller if content needs it */

The browser first gives each track its minimum, then distributes the rest towards the maximums. You’ll combine minmax() with repeat(auto-fit, …) for responsive grids in auto-fit and minmax.

The 1fr overflow trap

Here’s the most common grid surprise. A 1fr column contains a <pre> code block, a wide table, a fixed-width image, or a long word with no break points — and instead of the content wrapping or scrolling, the column grows and the grid overflows the page.

The reason: 1fr actually means minmax(auto, 1fr). The auto minimum means the track can’t shrink below the minimum content size of what’s inside it. A code line that can’t wrap has a very large minimum.

The fix is to set the minimum explicitly:

CSS
grid-template-columns: 14rem minmax(0, 1fr);

minmax(0, 1fr) lets the track shrink to whatever its share is; the content then wraps, scrolls (overflow: auto on the <pre>), or truncates as you’ve styled it.

Note what actually triggers it: the wide content sits inside the grid item. Giving the inner <pre> overflow: auto isn’t enough on its own — its width still counts towards the item’s minimum size. (If the grid item itself is a scroll container, with overflow other than visible, its automatic minimum is already 0, so the trap doesn’t apply.)

The same issue exists in flexbox as the “min-width: auto” trap (see flex-grow, flex-shrink and flex-basis). Use minmax(0, 1fr) whenever a flexible column might contain wide or unbreakable content — main content columns, code, tables, carousels.

Rows

Everything works the same way for rows:

CSS
.app {
  display: grid;
  grid-template-rows: auto 1fr auto;   /* header, content, footer */
  min-height: 100dvh;
}

Remember that fr rows only have free space to share if the grid has a definite height — here min-height: 100dvh. Without it, the grid is just as tall as its content and 1fr behaves like auto.

Track sizing cheat sheet

You want Use
a fixed column 15rem
equal flexible columns repeat(3, 1fr)
flexible columns that never overflow repeat(3, minmax(0, 1fr))
a column sized to its content auto or max-content
a column with a minimum minmax(12rem, 1fr)
a content-sized column with a cap fit-content(20rem)
header/content/footer rows auto 1fr auto

DevTools tip: the grid overlay can show each track’s size. When a column is unexpectedly wide, look for an fr track whose content has a large minimum size — then switch it to minmax(0, 1fr).

Exercise

A dashboard layout

Build a .dashboard grid with three columns: a 14rem sidebar, a main column that takes twice as much of the remaining space as the third column, and a third column that is never narrower than 12rem. Rows: an auto-height header row, and a content row that fills the rest of a 100dvh-tall dashboard. The main column contains a long code line — make sure it scrolls inside its own box instead of stretching the column and overflowing the page.

HTML
<div class="dashboard">
  <header class="top">Dashboard</header>
  <nav class="side">Menu</nav>
  <main class="main"><pre>const result = courses.filter((course) => course.level === 'advanced' && course.lessons.length > 10).map((course) => course.title);</pre></main>
  <aside class="extra">Stats</aside>
</div>

<style>
  * { box-sizing: border-box; }
  body { margin: 0; }
  .dashboard > * { background: #eaf0fc; padding: 1rem; }
  .top { grid-column: 1 / -1; }
  /* your code here */
</style>

Summary

  • fr shares the free space left after fixed tracks and gaps, in proportion to each track’s fr value.
  • auto tracks size to their content; min-content and max-content size to the smallest and largest content sizes.
  • minmax(min, max) sets a range for a track, such as minmax(12rem, 1fr).
  • 1fr really means minmax(auto, 1fr), so wide content can stretch it; use minmax(0, 1fr) to prevent overflow.
  • Unlike percentages, fr tracks account for gaps automatically and never cause overflow by themselves.