min()
Picks the smallest of the given values. It works as an upper limit, for example a width of 100% of the parent, but no more than 600px.
Example
#.container {
width: min(100% - 2rem, 480px);
margin-inline: auto;
}<div class="container stage">
<strong>Content column</strong>
<p style="margin: 4px 0 0; color: #a1a1aa;">On a wide screen it is 480 px wide and centred. On a narrow one it leaves 1rem of margin on each side.</p>
</div>On a computer 480px wins. On a phone 100% - 2rem is smaller, so the column fills the screen with 1rem of space on the sides.
Definition and usage
#The min() function takes any number of comma-separated values and returns the smallest. Its point is clear when you mix units: width: min(100%, 600px) means “the full width of the parent, but no more than 600 pixels”. On a phone 100% wins, on a wide screen 600px.
It is easy to get confused, because the name suggests a lower bound while the opposite is true: min() sets a maximum. If a value should grow only up to a certain ceiling, min() is the one. The expression width: min(100%, 600px) does the same as the pair width: 100%; max-width: 600px;, only in one declaration.
In the arguments you can write expressions without calc(), for example min(100% - 2rem, 60ch), and use variables. The function works in any property that accepts numbers, lengths, percentages, angles, or times: in width, padding, font-size, gap, and also inside minmax() in a grid.
Syntax
#min(<value>, <value> [, <value>]*)Common uses
#220px, in a narrow frame 100%
An upper limit on width. The frame is wider than 220 px, so the tile is 220 px. Drag the frame’s bottom-right corner to the left: once it gets narrower, the tile takes 100%.180px or 100% - 40px
An expression in an argument, without writingcalc(). In a wide frame the tile stops at 180 px, and in a narrow one it leaves 40 px of free space on the right.26ch
A comfortable line length. The paragraph is at most 26ch wide, and in a narrower frame it simply fills it instead of overflowing.columns from 90px
A safe grid. Columns are at least 90 px wide, but when the frame is narrower the minimum drops to 100% and nothing overflows the edge. Narrow the frame and the tiles collapse into one column.
Common uses in practice
#min(100%, 220px)
.demo { width: min(100%, 220px); }min(100% - 40px, 180px)
calc(). In a wide frame the tile stops at 180 px, and in a narrow one it leaves 40 px of free space on the right..demo { width: min(100% - 40px, 180px); }min(26ch, 100%)
.demo { width: min(26ch, 100%); margin: 0; }minmax(min(90px, 100%), 1fr)
.demo {
display: grid;
grid-template-columns:
repeat(auto-fill, minmax(min(90px, 100%), 1fr));
gap: 8px;
}Browser support
#Widely available · since 2020
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Remember:
min()sets an upper limit, andmax()a lower one. The result ofmin()never exceeds the smallest of its arguments. - The expression
width: min(100%, 600px)replaces the pairwidth: 100%andmax-width: 600px. - In grids with
repeat(auto-fill, …), write the column minimum asmin(200px, 100%). On a very narrow screen the column won’t overflow its parent.
Related entries
#- max()Picks the largest of the given values. It works as a lower limit, for example spacing of 5% of the width, but never less than 16px.
- clamp()Keeps a value between a minimum and a maximum, and lets it grow smoothly in between. The basis of fluid typography.
- calc()Computes a value from a mathematical expression and lets you combine different units, for example percentages with pixels.
- max-widthSets a maximum width the element won’t grow beyond.
- widthSets the width of an element.
- repeat()Repeats a pattern of grid columns or rows. With auto-fill or auto-fit and minmax() it creates a responsive grid without media queries.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.