π What Is fr in CSS Grid?
The fr (fraction) unit is a special CSS Grid unit that represents a portion of the remaining available space inside a grid container. It makes grid layouts flexible, balanced, and responsive without fixed widths.
Note
β Represents **flexible** space
β Automatically handles leftover space
π¦ How fr Works
Think of fr as assigning βweightsβ to columns or rows. The grid divides the leftover space between all fr units proportionally.
Basic fr example
grid-template-columns: 1fr 1fr 1fr;This creates 3 equal columns β each taking 1 portion of the free space.
π Example: Different Fractions
Proportional distribution
grid-template-columns: 1fr 2fr 1fr;| Column | Fraction | Space Share |
|---|---|---|
| Column 1 | 1fr | 25% |
| Column 2 | 2fr | 50% |
| Column 3 | 1fr | 25% |
π¦ How fr Calculates Space
Grid first calculates space used by:
- Fixed units (px, %, ch, etc.)
- min-content/max-content constraints
- flexible tracks defined with minmax()
Then leftover space β divided among fr units.
π§ͺ Practical Examples
1οΈβ£ Equal Columns
3 equal columns
.grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}2οΈβ£ Sidebar + Content Layout
Sidebar Layout
.layout {
display: grid;
grid-template-columns: 250px 1fr; /* fixed sidebar + flexible content */
}Sidebar stays 250px. Content takes remaining space.
3οΈβ£ Responsive Layout Using fr
Responsive columns
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
}Each card grows up to 1fr and wraps when needed.
4οΈβ£ Mixed Units (px + fr + %)
Mixed track sizing
.mixed-grid {
display: grid;
grid-template-columns: 200px 2fr 30% 1fr;
}Note
π Using fr with minmax()
minmax + fr
grid-template-columns: minmax(150px, 1fr) 2fr;Minimum width is protected, but fr handles flexible growth.
β οΈ Key Details You Must Know
- β fr distributes only unused space
- β fr tracks shrink only after fixed-size tracks resolve
- β fr ignores content size unless wrapped in minmax()
- β fr can cause overflow if min-content is larger than fr allocation
Note
π§ͺ Example β Fixing Overflow Issue
Prevent overflow
grid-template-columns: minmax(0, 1fr) minmax(0, 2fr);π― fr vs % vs auto
| Unit | Behavior | Best For |
|---|---|---|
| fr | Shares leftover space proportionally | Flexible column systems |
| % | Relative to parent width | Fluid layouts without content control |
| auto | Size based on content size | Content-driven columns |
π§ When to Use fr
- Building responsive grid layouts
- Creating flexible content areas
- Designing equal-height / equal-width cards
- Replacing complicated percentage calculations
- Building modern dashboard layouts
π₯ Summary
The fr unit is one of the most powerful features in CSS Grid. It simplifies layout calculations by dividing leftover space proportionally. Whether youβre building dashboards, cards, or full layouts, frgives you flexibility and clean responsive behavior.
Learn more βMDN Docs π