liveBook · Manning

Fraction unit

As explained in CSS in Depth, Second Edition

The CSS fr unit represents a fraction of the leftover space in a grid container after non-flexible track sizes are accounted for.

The CSS fr unit sizes a grid track as a fraction of the available space. Three 1fr tracks divide space equally, while larger values receive proportionally more. section 5.1

Equal and proportional columns

A basic grid can use grid-template-columns: 1fr 1fr 1fr for three equal columns. Changing the declaration to 2fr 1fr makes the first column twice as wide as the second. Fraction units also work for rows. section 5.1 section 5.2

Combine fraction units with other track sizes when the layout has fixed and flexible regions:

  • Use 300px 1fr for a fixed 300-pixel column followed by a column that fills the remaining space. section 5.1
  • Use repeat(3, 2fr 1fr) to repeat a proportional pattern across six tracks. section 5.2
  • Use a longer pattern such as 1fr repeat(3, 3fr) 1fr when edge tracks differ from the middle tracks. section 5.2
Listing 5.2 Laying out a basic grid
.grid {
  display: grid;           #1
  grid-template-columns: 1fr 1fr 1fr;      #2
  grid-template-rows: 1fr 1fr;      #3
  gap: 0.5em;     #4
}

.grid > * {
  background-color: darkgray;
  color: white;
  padding: 2em;
  border-radius: 0.5em;
}

Inspect the complete three-column, two-row grid declaration using equal fraction units. section 5.1

How CSS in Depth, Second Edition covers it

How deep it goes
Explains proportional sizing through worked layouts and mixed patterns, but stops short of browser sizing internals.
Examples
Uses equal three-column grids, fixed-plus-flexible columns, proportional page layouts, and repeated track patterns.
What it assumes
Introduces grid containers and grid items while applying grid-template-columns and grid-template-rows.
What you can do after
Build equal, proportional, mixed fixed-and-flexible, and repeated CSS Grid tracks with fr values.
How it connects
Connects CSS Grid with grid tracks, flexbox, fixed lengths, percentages, and the repeat() function.

Also explained in

Compare how 4 books explain fraction unit →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.