CSS Reference ยท Functions
The minmax() function
Reference for the CSS minmax() function: giving grid tracks a minimum and maximum size, the minmax(0, 1fr) fix, syntax rules and live examples.
The minmax() function defines a size range for a grid track. The track is never smaller than the first argument and, when there is spare room, grows up to the second argument. It is valid in grid track lists (grid-template-columns, grid-template-rows, grid-auto-columns, grid-auto-rows) and is also what makes repeat(auto-fill, ...) useful.
CSS
.layout { display: grid; grid-template-columns: minmax(200px, 300px) minmax(0, 1fr); }
Try it
Syntax
CSS
minmax( <min>, <max> ) /* <min>: <length-percentage> | min-content | max-content | auto */ /* <max>: <length-percentage> | <flex> (such as 1fr) | min-content | max-content | auto */
- Where it works: inside grid track lists, and inside
repeat(). - Inherited: not applicable.
- Animatable: not on its own; track lists interpolate only when their structure matches.
Values
| Argument | Description |
|---|---|
<length-percentage> |
A fixed or relative size. A percentage refers to the grid container. |
fr unit (max only) |
Share the leftover space. An fr value is not allowed as the minimum. |
min-content |
The smallest size that avoids overflowing the content (the longest unbreakable word). |
max-content |
The size the content would take without any wrapping. |
auto |
As a maximum, same as max-content; as a minimum, the item's automatic minimum size. |
Usage notes
- If max is smaller than min, the max is ignored and the track is simply the min size.
minmax(0, 1fr)versus1fr. Plain1fris reallyminmax(auto, 1fr), so a column will not shrink below its content (a long word, a wide<pre>or an image).minmax(0, 1fr)lets the column shrink to nothing, and overflowing content is then handled by your ownoverflowrules.- Responsive grids without media queries:
repeat(auto-fit, minmax(220px, 1fr))makes as many columns as fit at 220px or wider. - Small screens:
minmax(220px, 1fr)can overflow a container narrower than 220px.minmax(min(220px, 100%), 1fr)avoids that. - Rows:
minmax(100px, auto)is the classic "at least 100px, grow with content" row, used withgrid-auto-rows.
Browser support
Supported in all current browsers wherever CSS Grid is supported.
Related
repeat()โ combine withminmax()for fluid gridsfit-content()โ clamp a track to its contentgrid-template-columnsgrid-auto-rows- Lesson: Grid tracks