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.

On this page

Example

#
CSS
.container {
  width: min(100% - 2rem, 480px);
  margin-inline: auto;
}
Preview

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

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

Common uses

#
  • min(100%, 220px)

    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%.
  • min(100% - 40px, 180px)

    180px or 100% - 40px

    An expression in an argument, without writing 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.
  • min(26ch, 100%)

    26ch

    A comfortable line length. The paragraph is at most 26ch wide, and in a narrower frame it simply fills it instead of overflowing.
  • minmax(min(90px, 100%), 1fr)

    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)

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%.
.demo { width: min(100%, 220px); }

min(100% - 40px, 180px)

An expression in an argument, without writing 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%)

A comfortable line length. The paragraph is at most 26ch wide, and in a narrower frame it simply fills it instead of overflowing.
.demo { width: min(26ch, 100%); margin: 0; }

minmax(min(90px, 100%), 1fr)

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.
.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, and max() a lower one. The result of min() never exceeds the smallest of its arguments.
  • The expression width: min(100%, 600px) replaces the pair width: 100% and max-width: 600px.
  • In grids with repeat(auto-fill, …), write the column minimum as min(200px, 100%). On a very narrow screen the column won’t overflow its parent.

Related entries

#

Practise it in lessons

#

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