CSS Grid Track Sizing Algorithm
CSS Grid resolves column and row tracks in deterministic passes: fixed lengths lock first, intrinsic min-content/max-content bounds resolve next, and fr units divide only the remaining free space. Crucially, 1fr means minmax(auto, 1fr), not minmax(0, 1fr).
CSS Grid 4-Pass Track Sizing
Step through how the Grid engine resolves fixed, intrinsic, and fr tracks.
Fixed tracks (e.g., 240px) lock both base size and growth limit; 1fr starts as minmax(auto, 1fr).
grid-template-columns: 240px minmax(0, 1fr) 2fr;- track1
- 240px
- track2
- 0..inf
- track3
- 0..inf
Under the Hood
Grid sizing works like dividing a restaurant bill: fixed-price items come off the top, minimum cover charges (min-content) are reserved, and fr tracks split whatever cash remains on the table.
- Fixed Pass: Explicit
px,rem, and resolved%tracks lock immediately. - Intrinsic Pass:
auto,min-content, and1frtracks query their children's minimum content widths, just like Flexbox & Intrinsic Sizing. - Flexible (
fr) Pass: Leftover space divides proportionally acrossfrfactors;minmax(0, 1fr)bypasses the intrinsic minimum floor.
.app-shell {
display: grid;
/* Prevents wide <pre> blocks from blowing out the main column */
grid-template-columns: 260px minmax(0, 1fr);
gap: 1.5rem;
}To share parent tracks with nested card internals, pair this with CSS Subgrid.
Problem Genealogy
- 2011–2016: Multi-column layouts relied on float percentages,
calc(), and equal-height JS hacks. - 2017 (Grid L1): All major browsers shipped CSS Grid simultaneously, introducing
fr,minmax(), and two-dimensional track alignment.
Production Pitfalls
- The
1frgrid blowout: Writing1frinstead ofminmax(0, 1fr)allows unbreakable text or<pre>blocks to push the track wider than the viewport. auto-fillvsauto-fitconfusion:auto-fillpreserves empty ghost tracks at the end of a row;auto-fitcollapses empty tracks to0pxso existing items expand.
- When not to use Grid: Avoid Grid for simple inline wrapping chips or toolbars where items should size to their own content and wrap naturally, use Flexbox with
flex-wrap: wrap.
Connections & Logical Next Steps
Subgrid participates directly in the parent grid’s track-sizing algorithm.
Grid establishes a grid formatting context independent of block and float flow.
Grid sizes 2D tracks before placing items, whereas Flexbox distributes 1D space from item intrinsic sizes.
Independent nested grids size their own tracks, whereas subgrid inherits track definitions from the parent grid.
Mechanical Comparisons
Independent nested grids size their own tracks, whereas subgrid inherits track definitions from the parent grid.
Grid sizes 2D tracks before placing items, whereas Flexbox distributes 1D space from item intrinsic sizes.