You may already be familiar with several types of responsive units such as percentages (%
), em
s and rem
s. CSS Grid introduced a new relative sizing unit — fr
, like fraction.
By using the fr
unit, we can define the size of columns and rows as a fraction of the grid’s length and width. This unit was specifically created for use in CSS Grid. Using fr
makes it easier to prevent grid items from overflowing the boundaries of the grid. Consider the code below:
.grid { display: grid; width: 1000px; height: 400px; grid-template: 2fr 1fr 1fr / 1fr 3fr 1fr; }
In this example, the grid will have three rows and three columns. The rows are splitting up the available 400 pixels of height into four parts. The first row gets two of those parts, the second row gets one, and the third row gets one. Therefore the first row is 200 pixels tall, and the second and third rows are 100 pixels tall.
Each column’s width is a fraction of the available space. In this case, the available space is split into five parts. The first column gets one-fifth of the space, the second column gets three-fifths, and the last column gets one-fifth. Since the total width is 1000 pixels, this means that the columns will have widths of 200 pixels, 600 pixels, and 200 pixels respectively.
It is possible to use fr
with other units as well. When this happens, each fr
represents a fraction of the available space.
.grid { display: grid; width: 100px; grid-template-columns: 1fr 60px 1fr; }
In this example, 60 pixels are taken up by the second column. Therefore the first and third columns have 40 available to split between them. Since each gets one fraction of the total, they both end up being 20 pixels wide.
Instructions
Change your rows so that each row will take up the exact same fraction of the available space.
Change the grid so the middle column is still 50% of the grid, but the first column takes up three fourths of the remaining space and the last column takes up one fourth.