Course contentsShow
← Web Development
Lesson 333 of 2,9826. CSS GridFree lesson

Constraining Track Sizes with minmax()

Define flexible tracks with minimum and maximum size constraints using the minmax() function.

Constraining Track Sizes with minmax()

You'll learn how to create grid tracks that are flexible but stay within specific size boundaries.

The Core Idea

The minmax() function lets you define a grid track that can grow or shrink, but never falls below a minimum size or exceeds a maximum size. Think of it like a spring with stops at both ends—it can stretch and compress within a range, but there are hard limits.

While fr units distribute space freely and auto sizes to content, sometimes you need more control. minmax() takes two arguments: a minimum value and a maximum value.

How It Works

The syntax is straightforward:

.grid-container {
  display: grid;
  grid-template-columns: minmax(200px, 1fr) minmax(150px, 300px);
}

In this example:

  • The first column will never shrink below 200px, but can grow to take up available space (1fr)
  • The second column stays between 150px and 300px, no matter how much space exists

You can mix minmax() with other sizing methods:

.grid-container {
  display: grid;
  grid-template-columns: 100px minmax(300px, 2fr) 1fr;
  gap: 20px;
}

Here the middle column is guaranteed at least 300px but can grow up to twice the size of the last column.

Practical Use Case

This is perfect for responsive layouts where you want columns to adapt, but never become too cramped or too wide—like a sidebar that needs at least 250px for readability but shouldn't exceed 400px even on ultra-wide screens.

Key Takeaway: minmax() gives you flexible grid tracks with safety constraints, ensuring your layout remains functional across all screen sizes.