Refer to fractions of available space with the fr unit

Rory Smith
InstructorRory Smith
Share this video with your friends

Social Share Links

Send Tweet

There’s an unfamiliar fr unit pertinent to CSS Grid, which allows us to specify a fractional unit of space. Let’s see how to apply it.

[00:00] We have five boxes inside a container here. Let's set display: grid on our container. We'll make it take up all the available height. We'll make the boxes a little bit more visible. We'll set a background-color and a font-size. We'll split up our items a bit with a gutter.

[00:25] By default, all of our boxes take up the available space, so they're all equal heights. Let's say we wanted to make box a and box e smaller than the others. We'll set up some grid-template-rows. We're going to use the fr unit to tell each item how much space to take up.

[00:46] A will take up one fr unit. B, c, and d can all take up three frs. E can also take up one fr. A and e are smaller than the other boxes. This is a nice, easy way to denote that we want to use all the available space without having to rely on absolute values.

[01:06] Fr units refer to the available space. In this case, the available space is the entire vertical space. We can mix that up with values that aren't frs. For example, we could say that we want a to take up 20px, b to take up 30, and c to take up 40. Then let's say we wanted d to be a lot bigger than e. We could say 2fr 1fr.

[01:33] A, b, and c have absolute values, whereas d and e are simply taking up the available space because they have fr values. If we were to change d to 3fr, a, b, and c stayed in the same place, but d got bigger, and e moved down a bit. Fr values only affect the available space.