CSS Grid: sizing functions
Tags: css, tutorial, beginners
Published: 2022-05-23 11:34:27

Hello. Today I want to talk about CSS Grid sizing functions. This is an alternative way to define grid track size. Together with lengths, fr unit, and sizing keywords, the sizing function creates a comprehensive set of tools dedicated to creating the most challenging layouts. Let’s dive in.

This article is part of my CSS Grid introduction series. If you want to check out my previous posts, [here](https://dev.to/mateuszkirmuc/css-grid-tutorial-table-of-contents-1g4) you can find the whole table of contents.

---

## Fit-content function.

Fit-content is a function that takes one argument of type length or percentage and returns the calculated size of the grid track. Returned size can be equal to either min-content, max-content, passed argument, or available space.

Keep in mind that all of the examples contained in this article has the following CSS definition of grid container:

```
.container {
    width: 400px;
    height: 400px;
    grid-template-rows: 1fr;
}
```

When the argument of the fit-content function is smaller than the min-content size, then the size of the grid track will be equal to the min-content.

```
fit-content(argument) = min-content

when: 

argument < min-content
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6t0fx3e8adh89vsm7t6m.png)

When the argument is larger than the size of max-content and max-content size is smaller than the available space, the size of the grid track will be equal to max-content.

```
fit-content(argument) = max-content 

when: 

argument > max-content

and 

max-content < available space 
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plc6ggqheurj0887gjsv.png)

When the argument is smaller than available space and smaller than max-content, then the size of the grid track will be equal to the argument.

```
fit-content(argument) = argument

when: 

argument < available space

and

max-content > argument 
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5eddqic9ydzay3emqn7d.png)

Finally, when both the argument and max-content are larger than the available space, then the size of the grid track will be equal to the available space.

```
fit-content(argument) = available space

when:

argument > available space

and

max-content > available space 
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7u2c2o5o93338r0c4lxh.png)

## Minmax function.

Minmax is an additional function used for describing grid track size. This function returns the range of sizes that grid track can take depending on the layout. Minmax function accepts two arguments. Grid track size cannot be smaller than the first argument, and larger than the second argument.

The first argument can be one of the following types: length, percentage, or sizing keyword (min-content, max-content, auto). This argument defines the minimum size that a track can take in any possible layout. What’s important, the calculated size of the track can be smaller than the min-content.

The same data types apply to the second argument with an additional flex type (fr unit). This argument defines the maximum size that a track can take in any possible layout. Track size can be as large as the calculated size of the second argument when this size is smaller than the available space. Otherwise, the maximum size of the track will be equal to the available space.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4hh06dwjh0fv6adka2h5.gif)

When the second argument size is smaller than the available space, the maximum track size is equal to the argument.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o6txqon6jtlo6ie5so8e.gif)

Notice that track size can shrink only to the width described by the first argument (50px). The minimum size here is still less than min-content.
 

---

Thank you for reading this short article. If you want to read more content like this you can follow my dev.to or [twitter](https://twitter.com/MateuszKirmuc) account. Also, feel free to give me any form of feedback. I'd love to read any comments from you. See you soon in my next article!

---

PS. If you would like to support my work, I will be grateful for a cup of coffee. Thank you. ❤️ 

<a href="https://www.buymeacoffee.com/mateuszkirmuc" target="_blank"><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wovao2myccggp2830esp.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>