Sub topic 2
Sizing Tracks with fr Units and gap
Prerequisites
This chapter assumes you know the box model, basic CSS selectors, and lengths in px, rem, and %. It also assumes you can turn an element into a grid container with display: grid and set columns with grid-template-columns.
Tracks and the fr unit
A track is one row or one column of a grid. You can size a track with a fixed length such as 200px, or with the fr unit. One fr is a share of the free space. Free space is the container's width minus the fixed tracks and minus the gaps.
<div class="layout">
<div>Fixed</div>
<div>1fr</div>
<div>2fr</div>
</div>.layout {
display: grid;
grid-template-columns: 200px 1fr 2fr;
gap: 20px;
width: 1000px;
}The browser works out the widths in this order:
- Take the container width: 1000px.
- Subtract the fixed track (200px) and the two gaps (2 × 20px). That leaves 760px of free space.
- Split the 760px into 3 shares, because 1fr + 2fr = 3 shares.
| Track | Size | Result |
|---|---|---|
| First | 200px | 200px |
| Second | 1fr | about 253.3px |
| Third | 2fr | about 506.7px |
The numbers before fr are ratios, not percentages. 1fr 2fr and 5fr 10fr give the same layout. Fractions such as 0.5fr also work, but a total below 1 leaves some free space unused.
Spacing with gap
gap sets the space between tracks. It is shorthand for row-gap and column-gap. One value sets both. Two values set the row gap first, then the column gap.
.layout {
gap: 1rem; /* rows and columns */
}
.layout.wide {
gap: 0.5rem 2rem; /* row-gap column-gap */
}The gap appears only between tracks, never at the outer edges of the container. This is the main reason to prefer gap over item margins. Margins add space on every side, so you would need extra rules to remove the outer space. Gap also keeps the spacing consistent when items wrap onto new rows.
Common errors
A column grows past its share
A plain 1fr is not a hard limit. Its minimum size is the width of its content. A long URL, a wide <pre> block, or a large image can force the column wider than its share. The layout then overflows or distorts the other columns.
The fix is to set the minimum to zero with minmax(0, 1fr):
.layout {
display: grid;
grid-template-columns: minmax(0, 1fr) minmax(0, 2fr);
gap: 1rem;
}
.layout pre {
overflow-x: auto;
}The next chapters cover minmax() in detail. For now, remember that minmax(0, 1fr) means "share the space evenly, even if the content is wide." Pair it with overflow-x: auto so the content scrolls inside its own track.
Percentages plus gap overflow
.broken {
display: grid;
grid-template-columns: 50% 50%;
gap: 1rem;
}The two columns already fill 100% of the width, so the gap pushes the grid past its container. Use 1fr 1fr instead. The fr unit accounts for the gap automatically.
fr rows that do nothing
Rows work with fr too, but only when the container has a definite height. If the height is auto, there is no free space to share, and each row is only as tall as its content.
.shell {
display: grid;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
}Here min-height gives the grid free space to distribute. The middle row takes all of it, which pushes the last row to the bottom of the viewport.
Exercise
A grid is 900px wide with grid-template-columns: 150px 1fr 2fr; gap: 30px;. How wide is each of the three columns?
Show answer
The two gaps take 60px, and the fixed column takes 150px, so the free space is 900 − 150 − 60 = 690px. That splits into 3 shares of 230px. The columns are 150px, 230px (1fr), and 460px (2fr).
Exercise
A two-column layout uses grid-template-columns: 1fr 1fr. One column contains a very long unbroken string and the columns become unequal. What change keeps them equal?
Show answer
Use grid-template-columns: minmax(0, 1fr) minmax(0, 1fr). A plain 1fr has a minimum of auto, which is the content width. Setting the minimum to 0 lets both tracks shrink to an equal share. Add overflow-wrap: anywhere or overflow-x: auto on the content so it does not spill out.