min(), max(), and clamp()
min(<value-list>)
: selects the smallest (most negative) value from a list of comma-separated expressionsmax(<value-list>)
: selects the largest (most positive) value from a list of comma-separated expressionsclamp(<min>, <ideal>, <max>)
: clamps a value between an upper and lower bound, based on a set ideal value
.min {
width: min(50%, 400px);
}
.max {
width: max(50%, 400px);
}
p {
width: clamp(45ch, 50%, 75ch);
}
p {
font-size: clamp(1.5rem, 5vw, 3rem);
}
For example, in the case of: min(1rem, 50%, 10vw)
, the browser calculates which of these relative units is the smallest, and uses that value as the actual value.
The max()
function selects the largest value from a list of comma-separated expressions.
When using a calculation inside of a
min()
,max()
, orclamp()
function, you can remove the call tocalc()
. For example, writingfont-size: max(calc(0.5vw - 1em), 2rem)
would be the same asfont-size: max(0.5vw - 1em, 2rem)
.
The CSS math functions, min()
, max()
, and clamp()
are very powerful, well supported, and could be just what you're looking for to help you build responsive UIs.
References
- min(), max(), and clamp(): three logical CSS functions to use today (opens in a new tab)
- CSS values and units - Learn web development | MDN (opens in a new tab)
- CSS Values and Units Module Level 4 (opens in a new tab)
- Using max() for an inner-element max-width | CSS-Tricks (opens in a new tab)
- Everything I Learned About min(), max(), clamp() In CSS (opens in a new tab)
- Practical Uses of CSS Math Functions: calc, clamp, min, max | Modern CSS Solutions (opens in a new tab)