minmax()

Term side of the Card.
Topic: Mathematics
Unit title: minmax()
Categories: Function
Explanation side of the Card

Explanation: The math function used in grid layouts to define a size range for grid tracks. It specifies a minimum and maximum size for rows or columns, allowing for flexible layouts that adapt to varying amounts of content or viewport sizes.

Browser compatibility:
  • The chrome browser supports this feature starting from version 57.
  • The edge browser supports this feature starting from version 16.
  • The firefox browser supports this feature starting from version 52.
  • The safari browser supports this feature starting from version 10.1.
Availability: 

Example

Wrongcss
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, 300px);
}
Rightcss
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(300px, 100%), 1fr));
}

Columns now stretch to fill the row, and min() stops a column from overflowing screens narrower than '300px'.

Common mistakes

  • A grid column overflows because 1fr has an automatic minimum of the content width.
  • Cards overflow narrow screens because the minmax() minimum is wider than the viewport.

Key points

  • Flexible columns that hold long content use minmax(0, 1fr) instead of 1fr.
  • Responsive card grids use repeat(auto-fill, minmax(200px, 1fr)) instead of media queries for each column count.
  • fr is not used as the minimum value, which is invalid.

Interview questions

What is the difference between auto-fill and auto-fit with minmax()?

auto-fill keeps empty tracks when items run out. auto-fit collapses them, so the items stretch to fill the row.

Why does 1fr not shrink below the content width?

1fr means minmax(auto, 1fr), so the minimum is the content size. Use minmax(0, 1fr) to let it shrink.

Can I use minmax() in flexbox?

No. minmax() only works in grid track sizes like grid-template-columns and grid-auto-rows.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy