Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

max() — a responsive lower bound

Select the largest compatible value to keep a fluid size above a chosen floor, while accounting for other layout constraints.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

max(value, ...)
width: max(120px, 50%);

max() compares one or more comma-separated calculations and returns the largest. With width: max(120px, 50%), a box takes half its containing block, but the selected width never falls below 120 pixels. The percentage depends on its containing block, not automatically on the viewport.

Arguments may contain arithmetic without an extra calc(), but their types must be compatible with one another and with the property. Lengths and percentages can be combined where the property permits it; 10px and 1s cannot be compared as a width. A minimum enforced by choosing the largest value can overflow a parent narrower than that minimum.

The function supplies a property value; it does not bypass the layout algorithm. In the example, adding max-width constrains the final width despite the selected floor. Borders, padding, intrinsic sizing, and other layout rules also matter. Choose a deliberate overflow policy instead of assuming the function always makes a component fit.

Examples

const parent = document.createElement('div');
const child = document.createElement('div');
parent.append(child);
document.body.append(parent);
child.style.width = 'max(120px, 50%)';
for (const width of [400, 200, 80]) {
  parent.style.width = width + 'px';
  console.log(width + ':', getComputedStyle(child).width);
}
child.style.maxWidth = '100%';
console.log('with max-width:', getComputedStyle(child).width);
console.log('mixed length/time:', CSS.supports('width', 'max(10px, 1s)'));
parent.remove();

Result

400: 200px
200: 120px
80: 120px
with max-width: 80px
mixed length/time: false

Actual computed widths are 200, 120, and 120 pixels for parents of 400, 200, and 80 pixels. The separate maximum-width constraint then reduces the last result to 80 pixels. Empty block boxes avoid unrelated intrinsic-content constraints.

Sources