Sub topic 4
min(): capping a value
This chapter assumes you know basic CSS syntax, the common length units (px, rem, %, vw), the box model, and how width and max-width behave. Earlier chapters covered calc() and its operator rules. Here we look at min() on its own.
What min() does
min() takes a comma-separated list of values and resolves to the smallest one. Because the result can never exceed any argument, min() works as an upper bound, or cap.
.box {
/* Never wider than 40rem, but shrinks with its container */
width: min(100%, 40rem);
}Suppose the container is 900px wide and 1rem is 16px:
100%is 900px and40remis 640px. The smaller value is 640px, so the box is 640px wide.- If the container shrinks to 400px,
100%is 400px. That is now smaller than 640px, so the box is 400px wide.
The browser doesn't pick the winner once. It re-evaluates the function whenever the inputs change, such as a resize, a rotation, or a zoom. A percentage can only be compared once the browser knows the size of the containing block. The comparison therefore happens at layout time, not when the stylesheet is parsed.
Syntax rules
min() follows the same rules as calc():
- It accepts one or more arguments, separated by commas. One argument is legal but pointless.
- Each argument can be a value, a math expression, or another math function. You don't need
calc()inside it. +and-need whitespace on both sides.*and/don't, but spaces keep them readable.- The arguments must have compatible types. You can compare lengths, percentages, and viewport units with each other. You can't compare a length with a bare number like
min(10px, 5).
.panel {
/* Three arguments: the smallest of the three wins */
width: min(100%, 90vw, 45rem);
/* Math is allowed directly inside the function */
max-height: min(100vh - 4rem, 50rem);
}Whitespace around - is a common source of silent failures:
.broken {
/* Invalid: "100%-2rem" is not parsed as a subtraction */
width: min(100%-2rem, 70rem);
}
.works {
width: min(100% - 2rem, 70rem);
}An invalid min() makes the whole declaration invalid, so the browser ignores it. The property then falls back to an earlier declaration or its default value. DevTools shows the declaration struck through.
min() versus max-width
For a width cap, min() and max-width are close relatives:
/* Version A */
.a {
width: 100%;
max-width: 40rem;
}
/* Version B */
.b {
width: min(100%, 40rem);
}Both produce the same used width in a normal block layout. The differences matter elsewhere.
| Situation | `max-width` | `min()` |
|---|---|---|
| Capping a width | Works well | Works well |
| Capping `padding`, `gap`, `font-size`, `margin` | No `max-` property exists | Works |
| Subtracting a gutter from the cap | Needs `calc()` and another property | `min(100% - 2rem, 70rem)` |
| Cascading with other width rules | A separate property can be overridden separately | One declaration, easier to read and override as a unit |
The main benefit is that min() can go in any property that accepts a length or percentage. Only some properties have a max- counterpart.
.card {
/* Padding grows with the viewport but never beyond 2rem */
padding: min(5vw, 2rem);
/* Gap capped the same way */
display: grid;
gap: min(3vw, 1.5rem);
}
h1 {
/* Font grows with the viewport but stops at 3rem */
font-size: min(8vw, 3rem);
}The h1 rule shows min() as half of fluid type. On a very narrow screen, 8vw can drop below a readable size. Chapter material on clamp() adds the matching lower bound. Until then, treat min() as an upper bound only.
A container that never overflows
The standard centered container uses min() with a gutter:
.container {
width: min(100% - 2rem, 70rem);
margin-inline: auto;
}This resolves in two steps:
100% - 2remis the container's available width minus a 1rem gutter on each side.min()compares that value with70remand picks the smaller one.
On a 360px screen with a 16px root size, the result is 328px, which leaves 16px on each side. On a 1600px screen the cap applies, so the container is 1120px wide and margin-inline: auto centers it. No media query is involved. A single declaration covers both cases and every width between them.
Compare that with the older pattern, which needs two rules and a padding workaround:
.container-old {
max-width: 70rem;
margin-inline: auto;
padding-inline: 1rem;
}The older pattern is also fine. min() is simply more direct when you want the gutter to appear only while the container is narrower than its cap.
Gotchas
Percentages need a known reference
A percentage resolves against a specific measurement, and that measurement depends on the property:
width,margin-inline, andpadding(on any side) resolve against the width of the containing block.heightresolves against the containing block's height, and only when that height is definite. If the parent's height depends on its content, the percentage can't resolve and the browser treatsheightasauto.
So height: min(100%, 30rem) often does nothing useful. For vertical caps, prefer viewport units such as min(100vh, 30rem). You can also give the parent an explicit height.
100vw includes the scrollbar
vw is measured against the viewport including any classic, always-visible scrollbar. A rule like width: min(100vw, 70rem) can therefore be wider than the visible area and cause a horizontal scrollbar. Use 100% when you mean "the container's width". Use vw only when you really want a viewport-relative amount.
Padding and border still add to the width
With the default box-sizing: content-box, a width: min(100%, 40rem) element that also has padding becomes wider than its container. Set box-sizing: border-box so the result of min() includes the padding and border:
*,
*::before,
*::after {
box-sizing: border-box;
}
.field {
width: min(100%, 30rem);
padding: 0.75rem 1rem;
border: 1px solid #888;
}The smallest value always wins
If one argument is much smaller than expected, it wins every time. min(100%, 0) is always 0. Negative values also win, because -1rem is smaller than any positive length. A gutter subtraction that goes negative, such as 100% - 2rem in a 20px-wide container, produces a negative width. Browsers clamp negative widths to zero. Check that the container is wide enough for the gutter you subtract.
Provide a fallback for old browsers
All current evergreen browsers support min(). If you must support an older browser, put a plain declaration first. A browser that doesn't understand min() drops the second declaration and keeps the first:
.container {
width: 90%;
max-width: 70rem;
width: min(100% - 2rem, 70rem);
margin-inline: auto;
}An @supports block works too:
@supports (width: min(10px, 5%)) {
.container {
width: min(100% - 2rem, 70rem);
}
}A complete example
This page uses min() for the container, an image cap, and a padding cap. Save it as an HTML file and resize the window to watch it adapt.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>min() demo</title>
<style>
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; }
.container {
width: min(100% - 2rem, 50rem);
margin-inline: auto;
}
.card {
padding: min(5vw, 2rem);
background: #eef2f7;
border-radius: 0.5rem;
}
.photo {
display: block;
width: min(100%, 400px);
height: 200px;
background: #8aa4c8;
}
</style>
</head>
<body>
<main class="container">
<h1>min() demo</h1>
<div class="card">
<p>The card padding follows 5vw until it reaches 2rem.</p>
<div class="photo"></div>
</div>
</main>
</body>
</html>Narrow the window below about 400px and the .photo box shrinks with the card instead of overflowing. Widen it and the box stops at 400px.
Key points
min()resolves to the smallest of its arguments, so it acts as an upper bound.- The comparison happens during layout, which lets it mix percentages with absolute and viewport-relative lengths.
- Math works inside the function without
calc(), and+and-need surrounding spaces. - It works in any length property, which makes it more flexible than
max-width. - Watch for
100vwoverflow, content-box padding, and percentages of undefined heights.
Exercise
A stylesheet sets .sidebar { width: min(100% - 3rem, 24rem); }, the root font size is 16px, and the sidebar's containing block is 500px wide. What width does the sidebar get? What width does it get when the containing block is 300px wide? Explain which argument wins in each case.
Show answer
At 500px, 100% - 3rem is 500 - 48 = 452px and 24rem is 384px. The smaller value is 384px, so the cap wins and the sidebar is 384px wide. At 300px, 100% - 3rem is 300 - 48 = 252px, which is smaller than 384px, so the sidebar is 252px wide and keeps a 24px gutter on each side.
Exercise
You want the horizontal padding of a banner to equal 4% of its containing block's width, but never more than 3rem. Write the declaration. Then explain why max-width could not do this job.
Show answer
padding-inline: min(4%, 3rem);. The max-width property only limits the width of the element. There is no max-padding property, so only a comparison function can cap padding.