Tracks and the fr unit
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:
.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 |
/* 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:
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:
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:
.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
frtrack whose content has a large minimum size — then switch it tominmax(0, 1fr).
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.
<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><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; }
.dashboard {
display: grid;
grid-template-columns: 14rem minmax(0, 2fr) minmax(12rem, 1fr);
grid-template-rows: auto 1fr;
min-height: 100dvh;
gap: 1rem;
}
.main pre {
overflow-x: auto; /* the code scrolls inside its box */
}
</style>Summary
frshares the free space left after fixed tracks and gaps, in proportion to each track’s fr value.autotracks size to their content;min-contentandmax-contentsize to the smallest and largest content sizes.minmax(min, max)sets a range for a track, such asminmax(12rem, 1fr).1frreally meansminmax(auto, 1fr), so wide content can stretch it; useminmax(0, 1fr)to prevent overflow.- Unlike percentages, fr tracks account for gaps automatically and never cause overflow by themselves.