Mixing fr Units with Fixed Sizes
You'll learn how to combine flexible (fr) and fixed units in a single grid layout for more predictable, hybrid designs.
The Core Idea
While fr units let grid tracks share leftover space flexibly, you often need some columns or rows to stay a fixed size. CSS Grid lets you mix both approaches in the same grid-template-columns or grid-template-rows definition. Fixed units (like px, rem, or em) are allocated first, then any remaining space is divided among the fr tracks.
Think of it like dividing a pizza: if you promise one friend exactly 2 slices (fixed size), the rest of your friends split what's left (fractional units).
How It Works
When you write something like:
grid-template-columns: 200px 1fr 2fr;
The browser:
- Allocates 200px to the first column (fixed)
- Calculates remaining space (container width minus 200px)
- Divides that leftover space into 3 parts (1 + 2 = 3)
- Assigns 1 part to column 2 and 2 parts to column 3
This pattern is incredibly powerful for layouts like sidebars (fixed width) with flexible content areas, or grids where certain columns must accommodate specific content sizes.
Example Pattern
.grid {
display: grid;
grid-template-columns: 250px 1fr 1fr;
gap: 20px;
}
Here, the first column stays exactly 250px wide (perfect for a sidebar), while the remaining two columns equally share whatever space is left.
Key Takeaway: Mixing fr units with fixed sizes (px, rem) gives you precise control over some tracks while keeping others flexible—fixed values are subtracted first, then fr units divide the remainder.