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.

On this page

Example

#
CSS
.search {
  width: max(50%, 240px);
}
Preview

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

#
Syntax
max(<value>, <value> [, <value>]*)

Common uses

#
  • max(60%, 120px)

    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.
  • max(14px, 7cqi)

    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.
  • max(8px, 5cqi)

    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)

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.
.demo { width: max(60%, 120px); }

max(14px, 7cqi)

Text scales with the container width, but doesn’t drop below 14 px. Narrow the frame and watch the text shrink and then stop.
.demo { font-size: max(14px, 7cqi); }

max(8px, 5cqi)

The inner padding grows with the container, but never drops below 8 px, so the text won’t touch the tile’s edge.
.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 nesting max() and min().
  • For text sizes in vw, add a lower limit in rem, for example max(1rem, 2.5vw), so the letters don’t get too small on a phone.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.