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.
Example
#.search {
width: max(50%, 240px);
}<div class="stage" style="display: flex; flex-wrap: wrap; align-items: center; gap: 10px;">
<input class="search" type="search" placeholder="Search the docs" aria-label="Search the docs" style="padding: 8px 12px; border: 1px solid #3f3f46; border-radius: 10px; background: #18181b; color: inherit; font: inherit;">
<button type="button" style="padding: 8px 14px; border: 0; border-radius: 10px; background: #7c3aed; color: #fff; font: inherit;">Search</button>
</div>On a computer the field takes half the width. On a phone half is too little, so 240 px wins and the button wraps to the next line.
Definition and usage
#The max() function returns the largest of the comma-separated values. In practice it sets a lower bound: padding: max(16px, 5%) gives 5% of the parent’s width, but never less than 16 pixels, so on a narrow screen the content won’t stick to the edge.
As with min(), the name can be misleading: max() doesn’t set a maximum but a minimum. The expression width: max(50%, 300px) corresponds to the pair width: 50%; min-width: 300px;. There can be more than two arguments, and in each you can write expressions and use variables.
A good example is phones with a notch in the screen. With viewport-fit=cover in the viewport tag, padding-left: max(1rem, env(safe-area-inset-left)) keeps the usual spacing but increases it where the edge is covered by the camera cutout. For text, max() guards readability: font-size: max(1rem, 2vw) won’t let letters shrink below the base size.
Syntax
#max(<value>, <value> [, <value>]*)Common uses
#60%, in a narrow frame 120px
A lower limit on width. In a wide frame the tile is 60% of its width. Drag the bottom-right corner to the left: once 60% gets smaller than 120 px, the tile stops shrinking.7cqi, in a narrow frame 14px
Text scales with the container width, but doesn’t drop below 14 px. Narrow the frame and watch the text shrink and then stop.5cqi, in a narrow frame 8px
The inner padding grows with the container, but never drops below 8 px, so the text won’t touch the tile’s edge.
Common uses in practice
#max(60%, 120px)
.demo { width: max(60%, 120px); }max(14px, 7cqi)
.demo { font-size: max(14px, 7cqi); }max(8px, 5cqi)
.demo { padding: max(8px, 5cqi); }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
#- The result of
max()is always at least as large as each of its arguments, so the function works as a lower limit. - When you need a lower and an upper limit at once, use
clamp()instead of nestingmax()andmin(). - For text sizes in
vw, add a lower limit inrem, for examplemax(1rem, 2.5vw), so the letters don’t get too small on a phone.
Related entries
#- 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.
- 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.
- min-widthSets a minimum width the element won’t shrink below.
- paddingSets the inner space between the content and the border.
- UnitsAn overview of length units in CSS: from fixed pixels, through font-relative em and rem, to viewport, container, and grid units.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.