"Mastering CSS Grid: minmax with grid-template-columns"

Mastering CSS Grid: Exploring `grid-template-columns` with `minmax()`

In the realm of web design, CSS Grid has emerged as a powerful tool for creating two-dimensional layouts with ease and flexibility. One of the most versatile properties of CSS Grid is `grid-template-columns`, which allows you to define the size and behavior of grid columns. Today, we're going to delve into a lesser-known but incredibly useful function of `grid-template-columns`: the `minmax()` function.

Understanding `minmax()` in CSS Grid

The `minmax()` function in CSS Grid is a content-based sizing system that allows you to set minimum and maximum sizes for your grid columns. It's particularly useful when you want to create responsive layouts that adapt to different screen sizes or when you need to ensure that your content has a certain amount of space while preventing it from taking over the entire layout.

Here's the basic syntax of `minmax()`:

CSS Grid Cheatsheet – Complete Guide to Grid Layout
CSS Grid Cheatsheet – Complete Guide to Grid Layout

`minmax(min-content, max-content)`

The `min-content` value represents the smallest possible size for the content, while `max-content` represents the largest possible size. This allows you to set a range within which the content will resize, based on the available space.

Setting Minimum and Maximum Sizes with `minmax()`

Let's say you want to create a grid with three columns. You want the first column to take up at least 200 pixels but no more than 400 pixels, the second column to take up at least 300 pixels but no more than 500 pixels, and the third column to take up the remaining space. You can achieve this with the following `grid-template-columns` value:

`grid-template-columns: minmax(200px, 400px) minmax(300px, 500px) 1fr;`

In this example, `1fr` is a fraction unit that represents a fraction of the available space. In this case, the third column will take up the remaining space after the first two columns have reached their maximum sizes.

Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes

Responsive Design with `minmax()`

One of the most powerful aspects of `minmax()` is its ability to create responsive layouts. By setting different minimum and maximum sizes for different screen sizes, you can ensure that your layout adapts to the available space.

Here's an example of how you might use media queries to create a responsive layout with `minmax()`:

`@media (min-width: 600px) {
  .grid-container {
    grid-template-columns: minmax(200px, 1fr) minmax(300px, 2fr);
  }
}

@media (min-width: 900px) {
  .grid-container {
    grid-template-columns: minmax(200px, 1fr) minmax(300px, 1fr) minmax(400px, 1fr);
  }
}`

In this example, the grid layout changes based on the screen width. When the screen is less than 600 pixels wide, the grid has one column that takes up the full width of the screen. When the screen is at least 600 pixels wide, the grid has two columns: one that takes up a minimum of 200 pixels and a maximum of half the available space, and one that takes up a minimum of 300 pixels and a maximum of twice the available space. When the screen is at least 900 pixels wide, the grid has three columns, each with a minimum and maximum size of 400 pixels.

the different types of web pages are shown in red and black, with text below them
the different types of web pages are shown in red and black, with text below them

Comparing `minmax()` with `fr` Units

You might be wondering how `minmax()` compares to using `fr` units to size your grid columns. While `fr` units are a great way to create flexible layouts, they don't offer the same level of control as `minmax()`. With `fr` units, you're essentially telling the browser to distribute the available space equally among the columns. With `minmax()`, you have more control over how the space is distributed, as you can set minimum and maximum sizes for each column.

Here's a table summarizing the differences between `minmax()` and `fr` units:

Property minmax() fr units
Size control Allows you to set minimum and maximum sizes for each column Distributes space equally among columns
Responsiveness Can create responsive layouts by setting different minimum and maximum sizes for different screen sizes Less control over responsiveness, as space is always distributed equally
Flexibility Offers more flexibility in terms of sizing and behavior More limited in terms of sizing and behavior

Conclusion

In this article, we've explored the `minmax()` function in CSS Grid and seen how it can be used to create flexible, responsive layouts with precise control over the size and behavior of grid columns. Whether you're creating a simple two-column layout or a complex, multi-column grid, `minmax()` is a powerful tool that can help you achieve the results you want. So go ahead, experiment with `minmax()`, and watch as your layouts come to life with ease and elegance.

CSS
CSS
Learn CSS Grid Column and CSS Grid Row
Learn CSS Grid Column and CSS Grid Row
🚀 CSS Grid vs. Flexbox: When to Use Each? 🎯  Choosing between CSS Grid and Flexbox can be tricky,
🚀 CSS Grid vs. Flexbox: When to Use Each? 🎯 Choosing between CSS Grid and Flexbox can be tricky,
CSS Grid template
CSS Grid template
Min and Max Width/Height in CSS
Min and Max Width/Height in CSS
CSS equal height columns
CSS equal height columns
css grid layout cheat sheet
css grid layout cheat sheet
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
Grid Template Areas — TutorialBrain
Grid Template Areas — TutorialBrain
CSS Grid vs Flexbox: Which Layout System Should You Use?
CSS Grid vs Flexbox: Which Layout System Should You Use?
CSS Grid Layout Cheat Sheet – Align & Justify Options
CSS Grid Layout Cheat Sheet – Align & Justify Options
Flexbox vs Grid – When to Use Which in CSS (2025 Guide)
Flexbox vs Grid – When to Use Which in CSS (2025 Guide)
Create A Grid In HTML & CSS (Simple Examples)
Create A Grid In HTML & CSS (Simple Examples)
css cheat sheet
css cheat sheet
Alamin on X
Alamin on X
the grid layout for css grid layout is shown in orange and white, with different sizes
the grid layout for css grid layout is shown in orange and white, with different sizes
Css Grid Layout
Css Grid Layout
the css flexbox cheat sheet is shown in yellow and red, as well as black
the css flexbox cheat sheet is shown in yellow and red, as well as black
Mosaic Layouts with CSS Grid
Mosaic Layouts with CSS Grid
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
an image of a screen with the text response grid layout
an image of a screen with the text response grid layout
How to create an image gallery with CSS Grid
How to create an image gallery with CSS Grid