CKTIS / CSS
min() — a responsive upper bound
Choose the smaller compatible value to cap a fluid width without confusing the cap with a minimum size.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
min(value, ...)
width: min(100% - 20px, 300px);min() returns the smallest of one or more comma-separated calculations. The declaration width: min(100% - 20px, 300px) leaves 20 pixels relative to the containing block and caps the selected width at 300 pixels. Despite its name, this is a way to impose an upper bound, not a minimum.
Arithmetic can appear directly in an argument without an extra calc(). Keep whitespace around addition and subtraction, and use compatible types. A width may combine lengths with percentages, but comparing 10px with 1s is invalid. The percentage needs a reference size; this example supplies a definite parent width.
Other constraints still apply after the function selects its value. Here min-width can raise the final width beyond the calculated result and make a narrow parent overflow. Content, padding, and the chosen box model can also change the final occupied space. For both a lower and an upper design limit, consider a bounded expression rather than relying on the function name.
Examples
const parent = document.createElement('div');
const child = document.createElement('div');
parent.append(child);
document.body.append(parent);
child.style.width = 'min(100% - 20px, 300px)';
for (const width of [600, 240, 80]) {
parent.style.width = width + 'px';
console.log(width + ':', getComputedStyle(child).width);
}
child.style.minWidth = '100px';
console.log('with min-width:', getComputedStyle(child).width);
console.log('mixed length/time:', CSS.supports('width', 'min(10px, 1s)'));
parent.remove();Result
600: 300px 240: 220px 80: 60px with min-width: 100px mixed length/time: false
The browser computes 300, 220, and 60 pixels for parents of 600, 240, and 80 pixels. Adding a minimum width of 100 pixels changes the last result to 100. This is a real layout measurement, not a JavaScript reimplementation of CSS minimum selection.