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
Published 8 years ago
Updated 6 years ago

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.

egghead
egghead
~ 3 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today