fr

fr is the acronym for Fractional Unit.

Fractional Unit

The fr unit in CSS Grid is a flexible unit that represents a fraction of the available space within a grid container. Unlike fixed units like pixels or percentages, fr dynamically distributes space based on the number of fractional units assigned to each column or row. This makes it an essential tool for creating fluid, adaptable grid layouts without relying on media queries.

Origins of fr

The fr unit was introduced with CSS Grid Layout to simplify dividing space within a container. Unlike older methods that relied on percentages or fixed widths, fr automatically adjusts to available space while maintaining proportional relationships between elements. This eliminates the need for manual calculations and ensures layouts remain responsive.

For example, consider a 3-column grid:

.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

Each column receives an equal share of the available space, effectively creating a three-column layout without specifying pixel values.

Use Cases for fr

  • Creating Equal Columns Without Fixed Widths: .grid { display: grid; grid-template-columns: 1fr 1fr 1fr; } This creates three equal-width columns that expand and shrink based on the container size.
  • Proportional Distribution of Space: .grid { display: grid; grid-template-columns: 2fr 1fr; } The first column gets twice the space of the second, maintaining this ratio regardless of screen size.
  • Mixing fr With Fixed and Min/Max Units: .grid { display: grid; grid-template-columns: 200px 1fr 2fr; } This layout keeps the first column fixed at 200px, while the remaining space is distributed in a 1:2 ratio between the last two columns.

Difference Between fr and Other Units

  • fr vs. px: Pixels are fixed, while fr units are flexible.
  • fr vs. %: Percentages divide space relative to the parent but can lead to inconsistent results when used with gaps and auto layouts.
  • fr vs. auto: The auto value lets an element grow based on its content, while fr distributes space based on the available container width.

The fr unit is a powerful, intuitive way to manage grid layouts in CSS. By allowing space to be distributed dynamically, it reduces the need for media queries and manual adjustments, making responsive design easier and more efficient. Whether creating equal columns, proportionally scaled layouts, or flexible structures, fr simplifies layout management in modern web design.

Back to top button
Close

Adblock Detected

We rely on ads and sponsorships to keep Martech Zone free. Please consider disabling your ad blocker—or support us with an affordable, ad-free annual membership ($10 US):

Sign Up For An Annual Membership