Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

fit-content() — content size with a cap

`fit-content()` sizes a grid track between its automatic minimum and max-content size, with the supplied argument limiting growth.

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

Syntax

fit-content( <length-percentage [0,∞]> )

fit-content() sizes a grid track between its automatic minimum and max-content size, with the supplied argument limiting growth.

The example uses fit-content(60px): short content occupies 22.00px, breakable content stops at 60.00px, and an unbreakable word forces a 109.97px minimum. Separate CSS Sizing 4 form width: fit-content(70px) returns false in this Chromium.

Grammar: fit-content( <length-percentage [0,∞]> ).

For a Grid track the result is the maximum of the auto minimum and the minimum of the limit and max-content; the limit therefore cannot squeeze the track below its minimum contribution, often min-content. The argument must be a non-negative length or percentage. Do not confuse the function with the argumentless fit-content keyword or the newer width use, whose support must be checked separately.

Examples

const measure=text=>{const grid=document.createElement('div');grid.style.cssText='display:grid;grid-template-columns:fit-content(60px);width:300px';const item=document.createElement('div');item.style.cssText='font:10px/1 monospace';item.textContent=text;grid.append(item);document.body.append(grid);const width=item.getBoundingClientRect().width;grid.remove();return width};
const short=measure('AAAA');
const capped=measure('AAAA AAAA AAAA AAAA');
const minimumFloor=measure('AAAAAAAAAAAAAAAAAAAA');
const sizingValue=CSS.supports('width','fit-content(70px)');
console.log('short='+short.toFixed(2)+'; capped='+capped.toFixed(2)+'; minimum-floor='+minimumFloor.toFixed(2)+'; sizing-value='+sizingValue);

Result

short=22.00; capped=60.00; minimum-floor=109.97; sizing-value=false

`fit-content()` sizes a grid track between its automatic minimum and max-content size, with the supplied argument limiting growth.

Sources