Sub topic 3
Flexible tracks with the fr unit
This chapter assumes you can already write display: grid and define columns with grid-template-columns and rows with grid-template-rows. It also assumes you know px, %, and rem. Here you will learn how the fr unit divides space, how to calculate the result by hand, and which situations make fr tracks behave in surprising ways.
What fr means
The fr unit is a fraction of the free space in the grid container. Free space is the space left in the container after the grid subtracts every non-flexible track and every gap. A track sized 1fr gets one share of that leftover space, and a track sized 2fr gets two shares.
.container {
display: grid;
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: auto auto;
gap: 16px;
}The first column is fixed at 200px. The other two columns split whatever remains, and each gets half of it.
How the browser calculates fr tracks
The calculation has four steps:
- Take the container's inner width (content box).
- Subtract the fixed tracks, such as
200pxor10rem. - Subtract all gaps. There is one fewer gap than there are tracks.
- Divide the remainder by the sum of the
frvalues. Each track gets itsfrvalue times that quotient.
Here is a worked example. The container is 1000px wide, and the columns and gap are set like this:
.container {
display: grid;
grid-template-columns: 200px 1fr 2fr;
column-gap: 16px;
}- Three columns have two gaps, so the gaps take 2 × 16 = 32px.
- Free space is 1000 − 200 − 32 = 768px.
- The
frvalues sum to 3, so onefris 768 ÷ 3 = 256px. - The columns are 200px, 256px, and 512px.
- Check: 200 + 256 + 512 + 32 = 1000.
Compare this with percentages. If you wrote 200px 33% 66%, the percentages would resolve against the full container width and ignore the gaps. The tracks would overflow by 32px. With fr, the gaps are subtracted for you.
Fractional and unequal values
The fr value does not have to be an integer. 1.5fr and 0.5fr are valid. Only the ratios matter when the values add up to 1 or more. 1fr 2fr and 0.5fr 1fr produce the same layout.
There is one exception. If the sum of all fr values in the track list is less than 1, the tracks use only that fraction of the free space, and the rest stays empty.
.narrow {
display: grid;
width: 1000px;
grid-template-columns: 0.25fr 0.25fr;
}Each column is 250px wide. The remaining 500px is unused. This behavior is rarely what you want, so check that your fr values add up to at least 1 when you intend to fill the container.
Mixing fr with other units
You can mix fr with any other length. Fixed and content-sized tracks are sized first, and fr tracks then share what is left.
.app {
display: grid;
grid-template-columns: auto 1fr 12rem;
gap: 1rem;
}In this grid:
- The first column is as wide as its widest item.
- The last column is 12rem.
- The middle column takes the rest.
A common mistake is to size every column with fr when one of them should hug its content. Use auto or max-content for that column instead.
The minimum-size trap
The 1fr unit is not a simple "share of space with a minimum of zero". A bare 1fr track behaves like minmax(auto, 1fr). The auto minimum means a track will not shrink below the minimum content size of its items. A long unbroken string, a wide image, or a <pre> block can force the track wider than its share, and the grid overflows its container.
This page shows the problem and the fix. You can save it as an HTML file and open it in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>fr minimum size</title>
<style>
.row {
display: grid;
width: 400px;
gap: 8px;
margin-bottom: 1rem;
outline: 1px solid #999;
}
.broken { grid-template-columns: 1fr 1fr; }
.fixed { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
.row > div { background: #cde; overflow-wrap: anywhere; }
.row > pre { background: #edc; margin: 0; overflow: auto; }
</style>
</head>
<body>
<div class="row broken">
<div>Short</div>
<pre>const aVeryLongLineOfCodeThatCannotWrapAtAll = true;</pre>
</div>
<div class="row fixed">
<div>Short</div>
<pre>const aVeryLongLineOfCodeThatCannotWrapAtAll = true;</pre>
</div>
</body>
</html>In the first grid, the <pre> forces its column to the width of the code line, and the grid spills out of its 400px box. In the second grid, minmax(0, 1fr) lets both columns shrink to equal widths, and the <pre> scrolls inside its own cell.
fr in rows
Rows can use fr too, but only when the grid container has a definite height. If the container's height is auto, there is no free space to divide. In that case each fr row is sized to fit its content, as auto would be.
.page {
display: grid;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
}Because min-height sets a lower bound, the container grows to at least the viewport height. The middle row then absorbs the extra space, and the header and footer stay at their content height. This keeps a footer at the bottom of short pages.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Columns are unequal even though all are `1fr` | An item has a large minimum content size | Use `minmax(0, 1fr)` |
| Grid is wider than its parent | Percentages combined with `gap` | Replace the percentages with `fr` |
| Rows do not stretch to fill the page | Container height is `auto` | Set `height` or `min-height` |
| Space remains empty on the right | `fr` values sum to less than 1 | Increase the values so they total at least 1 |
| Last column is too narrow | Fixed tracks plus gaps use most of the width | Use `auto` or `minmax()` for the fixed tracks |
Summary
frdivides the space left after fixed tracks and gaps are removed.- The width of one
fris the free space divided by the sum of thefrvalues. - Values below a total of 1 leave some space unused.
- A bare
1frhas anautominimum, so wide content can stretch a track.minmax(0, 1fr)removes that minimum. frrows need a definite container height to have free space to share.
Exercise
A grid container is 900px wide and has grid-template-columns: 100px 1fr 2fr; column-gap: 20px;. How wide is each of the three columns? Then explain which change would stop a <pre> with a very long line from stretching the 1fr column past its share.
Show answer
There are two gaps, which take 40px. Free space is 900 − 100 − 40 = 760px. The fr values sum to 3, so one fr is about 253.33px. The columns are 100px, about 253.33px, and about 506.67px. To stop the <pre> from stretching the column, change 1fr to minmax(0, 1fr) (and 2fr to minmax(0, 2fr) if the wide content may appear there). This removes the auto minimum, so the track can shrink below the content's minimum size.
Exercise
A page uses display: grid; grid-template-rows: auto 1fr auto; on a <div> with no height set. The footer sits right below the content on a short page instead of at the bottom of the window. What single declaration fixes it, and why does it work?
Show answer
Add min-height: 100vh to the container. Without a definite height there is no free space, so the 1fr row sizes to its content. With min-height: 100vh the container is at least as tall as the viewport, and the 1fr row takes the extra space, which pushes the footer down.