Use the minmax function to specify dynamically-sized tracks

Rory Smith
InstructorRory Smith
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 6 years ago

Using minmax() is an amazingly useful way to specify boundaries for the smallest and largest size a grid track can be. Let’s find out how to apply it.

[00:00] We have some mockup consisting of a container with two divs inside. Our container is display: grid with a 30px gap and taking up all the available height. Our grid items have a background-color. We want each of our divs to have their own column, so we're going to go down and set up some grid-template-columns. Let's say we wanted each one to be 100px wide. Now we have two 100px columns.

[00:25] Or we could also specify the minimum and maximum width of our columns using the minmax() function. Let's say we wanted them to be 100px at the very least and taking up all the available space at the most. We could say minmax(100px, auto) for the first column and the same for the second column. Now the columns take up all the available space, but they'll never get smaller than 100px.

[00:58] We could use a range of different width values in our minmax() functions. We could say that our first column should never be bigger than 200px. Now our second column is taking up the available space. We could use percentage values too. Now our first column never takes up less than 10 percent of the viewport.

[01:24] We could also use a flex value using the fr unit. We could say that our first column's max is 1fr and our second column's max is 4fr. Now they scale. Finally, we could use min-content and max-content. With this example, we've got some placeholder text in each of our columns. As we get further and further down on a small screen, we see some overflow in our first column.

[01:54] We could specify min-content as our minimum value in minmax(). Now we have no overflow because the minimum value is however much space is being taken up, in this case, by our text. If we were to use max-content, in this case the width of the div would be defined by however long the text is without it wrapping.

egghead
egghead
~ 9 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