Sub topic 3
Flexible tracks with the fr unit
This chapter assumes you can write a grid container with display: grid and define columns with grid-template-columns. It also assumes you know px, %, and rem units. Here you will learn how the fr unit divides space, how it differs from percentages, and which errors come up most often.
What fr means
The fr unit is a fraction of the free space in a grid container. Free space is the container's size along an axis, minus the tracks that have a fixed size, minus the gaps.
.container {
display: grid;
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: auto auto;
gap: 16px;
}Suppose the container is 1000px wide. The first column takes 200px. The two gaps between the three columns take 32px. That leaves 768px, which the two 1fr columns share equally, so each is 384px.
Fractions are proportions
An fr value is a share of the total number of fractions, not an absolute size. Two columns declared as 2fr 1fr split the free space into three parts. The first column gets two parts and the second gets one.
.layout {
display: grid;
grid-template-columns: 2fr 1fr;
gap: 1rem;
}In a 900px container with a 1rem (16px) gap, the free space is 884px. The first column is about 589px and the second is about 295px.
fr versus percentages
Percentages ignore the gap. Two columns of 50% plus a gap are wider than the container, so they overflow. Two 1fr columns subtract the gap first, so they fit.
.overflows {
display: grid;
grid-template-columns: 50% 50%;
gap: 20px;
}
.fits {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
}How the browser resolves fr tracks
The browser sizes tracks in a fixed order, which explains the results above.
Common errors
- A
1frcolumn grows wider than expected. A bare1fris shorthand forminmax(auto, 1fr). Theautominimum means the track will not shrink below its content's minimum size. A long unbreakable string or a wide image can push the track past its share. The fix is to writeminmax(0, 1fr), which allows the track to shrink below its content. frhas no effect on rows. In a container with no set height, the free space along the block axis is zero. Rows sized infrthen collapse to their content. Give the container a height, such asmin-height: 100vh, and the rows will share the space.- Mixing
frwithauto. Tracks sizedautoare measured by their content. Thefrtracks then share only what remains.
.safe {
display: grid;
grid-template-columns: minmax(0, 1fr) minmax(0, 2fr);
gap: 1rem;
}Exercise
A grid container is 800px wide and has grid-template-columns: 100px 1fr 3fr; gap: 20px;. How wide is each of the two fr columns?
Show answer
The two gaps take 40px, and the fixed column takes 100px, which leaves 660px of free space. That space is divided into four parts of 165px. The 1fr column is 165px and the 3fr column is 495px.
Exercise
Why might a 1fr column stay wider than its siblings when it holds a long unbreakable URL, and how do you stop it?
Show answer
A bare 1fr has an automatic minimum equal to the content's minimum size, so the track cannot shrink below the URL's width. Use minmax(0, 1fr) to remove that minimum.