Sub topic 2
Sizing Tracks with fr Units and gap
This chapter covers how a grid container divides its space into columns and rows, and how the fr unit and the gap property control that division. It assumes you know basic CSS selectors and the box model, and that you can read px, rem, and percentage lengths. It also assumes you have already seen display: grid and grid-template-columns once. The previous chapter introduced them, and this one goes deeper into how the sizes are computed.
Tracks: the rows and columns of a grid
A grid is made of tracks. A track is a single column or a single row. The lines between tracks are grid lines, and the space between two neighboring lines on one axis is one track. grid-template-columns defines the column tracks. grid-template-rows defines the row tracks.
Each value in the list is the size of one track. This rule creates three columns:
.layout {
display: grid;
grid-template-columns: 200px 200px 200px;
}You can mix units. Fixed lengths, percentages, and flexible units can all appear in the same list. The interesting part is what happens to the space the fixed tracks do not use.
The fr unit
The fr unit is a fraction of the free space in the grid container. Free space is the container's size on that axis, minus everything that has a fixed size: fixed-length tracks and gaps.
.layout {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
}The three values add up to 4 fractions. The middle column gets 2 of those 4 parts (50% of the free space), and each outer column gets 1 part (25% each). If the container is 800px wide, the columns are 200px, 400px, and 200px. If the container shrinks to 600px, they become 150px, 300px, and 150px.
Mixing fixed tracks and fr tracks
Fixed tracks are subtracted first. The remaining space is then divided among the fr tracks.
.page {
display: grid;
grid-template-columns: 220px 1fr;
}In a 1000px container, the first column is 220px and the second column gets the other 780px. This is the same two-column shape used by the sidebar and main layout later in this tutorial. The sidebar keeps a stable width, and the content takes whatever is left.
Percentages are not the same as fr
A percentage resolves against the container's full size. An fr resolves against the space that remains after fixed sizes and gaps are removed. That difference matters as soon as you add a gap.
/* Overflows: 50% + 50% + 1rem of gap is wider than the container */
.broken {
display: grid;
grid-template-columns: 50% 50%;
gap: 1rem;
}
/* Fits: the two fr tracks share what is left after the gap */
.works {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
}With percentages, the two columns already fill 100% of the container, so the gap pushes the grid past its edge. With fr, the gap is removed from the free space first, so nothing overflows.
The hidden minimum: fr is not a hard fraction
An fr track is not always exactly its share. A bare 1fr is shorthand for minmax(auto, 1fr). The minimum is auto, which means the track will not shrink below the minimum content size of its items. Long unbreakable content, such as a wide image, a long URL, or a <pre> block, can force a 1fr column to grow beyond its share.
.layout {
display: grid;
grid-template-columns: 1fr 1fr;
}If the left column holds a very long unbroken string, the columns will no longer be equal. There are two common fixes. One is to let the content shrink by setting min-width: 0 on the grid item, or overflow: hidden or overflow: auto. The other is to write the track as minmax(0, 1fr), which sets the minimum to zero explicitly.
.layout {
display: grid;
grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
}The next chapter covers minmax() fully. For now, remember that 1fr means "share the free space, but never go below the content's minimum size."
Fractional values and fr arithmetic
The number in front of fr does not have to be a whole number. 0.5fr and 1.5fr are valid. Only the ratios matter.
.layout {
display: grid;
grid-template-columns: 1fr 3fr; /* same ratio as 0.5fr 1.5fr */
}One subtle rule applies when the total number of fractions is less than 1. If you write 0.25fr 0.25fr, the tracks claim only half of the free space, and the rest stays empty. A total of 1 or more shares all of the free space. This rarely matters in practice, but it explains why 0.3fr alone does not fill the row.
Controlling space between tracks with gap
A gap (also called a gutter) is empty space between tracks. It appears only between tracks, never on the outer edges of the grid. The properties are:
column-gap: space between columns.row-gap: space between rows.gap: shorthand. One value sets both, and two values set row then column.
.layout {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
column-gap: 2rem;
row-gap: 1rem;
}
/* The same thing in shorthand: row gap first, column gap second */
.layout-short {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 1rem 2rem;
}The older names grid-gap, grid-column-gap, and grid-row-gap still work as aliases. Prefer the unprefixed gap in new code.
How gaps affect the arithmetic
Gaps are fixed-size and are removed from the free space before fr is computed. For three equal columns with a 2rem gap in an 800px container (taking 1rem as 16px), there are two gaps of 32px each, which is 64px. The free space is 736px, so each column is about 245.33px.
The following diagram shows the order of the calculation.
Gaps and spanning items
When an item spans several tracks, it also covers the gaps between them. A card that spans two columns is wider than two single columns by exactly one gap. This is useful for named areas, which the next chapters build on: an area that covers both columns of a two-column layout will visually line up with the combined width of the tracks and the gap between them.
A gap is not a margin
Gaps belong to the container, and margins belong to the item. Use gap for the spacing between items, because it never adds extra space at the edges and it does not need special cases for the first or last item. Margins on grid items are added inside their grid area, and they do not collapse with each other.
Rows and the implicit grid
Rows work the same way as columns, with one important difference in defaults. If you only define columns, the browser creates rows automatically as items need them. These automatic rows belong to the implicit grid, and their height comes from grid-auto-rows, which defaults to auto. An auto row is as tall as its tallest item.
.layout {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: minmax(100px, auto);
gap: 1rem;
}This keeps every automatic row at least 100px tall, and lets it grow when the content needs more room.
Using fr for rows only works when the container has a height that is not set by its content. Without a set height, the container is only as tall as its content, so there is no free space to share.
.app {
display: grid;
min-height: 100vh;
grid-template-rows: auto 1fr auto;
}This is the classic "sticky footer" shape. The first and last rows fit their content, and the middle row takes all the remaining height of the viewport. The min-height provides the free space that the 1fr row then claims.
A worked example
This example puts the pieces together. It uses a fixed sidebar column, a flexible main column, and a gap. The HTML is a plain container with two children.
<div class="shell">
<nav>Navigation</nav>
<section>Content</section>
</div>.shell {
display: grid;
grid-template-columns: 220px minmax(0, 1fr);
gap: 1.5rem;
}
.shell > nav,
.shell > section {
padding: 1rem;
background: #eef2f7;
}In a 1000px container, the gap is 24px (at a 16px root size), the nav is 220px, and the content column is 1000 − 220 − 24 = 756px. If the container narrows to 600px, the nav stays at 220px and the content column shrinks to 356px. The minmax(0, 1fr) form keeps long content from pushing the column wider than that.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Using `50% 50%` with a `gap` | The grid is wider than its container | Use `1fr 1fr` |
| Expecting `1fr` columns to be always equal | Wide content makes one column larger | Use `minmax(0, 1fr)` |
| Using `fr` rows in a container with no height | Rows collapse to content height | Set `min-height` or `height` |
| Writing `gap: 1rem 2rem` and expecting columns first | Row gap is 1rem, column gap is 2rem | Remember the order is row, then column |
| Using margins to space grid items | Spacing is uneven at the edges | Use `gap` on the container |
What comes next
You can now size columns and rows with fixed lengths and fr, and space them with gap. The next chapter names regions of the grid with grid-template-areas, and it reuses the 220px 1fr column pattern and the gap property from this chapter.
Exercise
A grid container is 900px wide. It has grid-template-columns: 100px 1fr 2fr; and gap: 20px;. How wide is each of the three columns?
Show answer
There are two gaps of 20px, which is 40px. The fixed track takes 100px. The free space is 900 − 100 − 40 = 760px, split 1:2 between the fr tracks. The first fr column is about 253.33px and the second is about 506.67px, so the columns are 100px, 253.33px, and 506.67px.
Exercise
A layout uses grid-template-columns: 1fr 1fr;, but one column is much wider than the other because it contains a long URL with no spaces. Give two ways to make the columns equal again.
Show answer
Change the tracks to minmax(0, 1fr) minmax(0, 1fr), which sets the minimum size to zero. Alternatively, set min-width: 0 on the grid item, or give the item overflow: hidden or overflow: auto, so its minimum content size no longer forces the column wider. Adding overflow-wrap: anywhere to the text also lets the URL break.
Exercise
Why does grid-template-columns: 50% 50%; gap: 1rem; overflow its container, while 1fr 1fr with the same gap does not?
Show answer
Percentages resolve against the full container width, so two 50% tracks already fill it, and the gap is added on top. The fr unit divides only the free space, which is the container width minus the gap, so the tracks and gap together fit exactly.