CSS Reference ยท Functions
The fit-content() function
Reference for the CSS fit-content() function: sizing a grid track to its content but never beyond a limit, how it differs from minmax(), with examples.
The fit-content() function sizes a grid track to fit its content, but stops growing at a limit you choose. A track given fit-content(250px) is as wide as its content wants to be (up to 250px) and never narrower than the content's smallest size, as long as that fits.
Use it for columns such as sidebars, labels or captions: short content gives a compact column, long content stops at the limit and wraps.
.page { display: grid; grid-template-columns: fit-content(220px) 1fr; }
Try it
Syntax
fit-content( <length-percentage> )
For a grid track this behaves like min(max-content, max(auto, <limit>)): the track grows with its content up to the limit, and not below its automatic minimum.
- Where it works: as a track size in
grid-template-columns,grid-template-rows,grid-auto-columnsandgrid-auto-rows. - Inherited: not applicable.
- Animatable: no.
Values
| Argument | Description |
|---|---|
<length-percentage> |
The limit. A percentage refers to the grid container's size in that axis; if that size depends on the content, the percentage is treated as if the limit were max-content. |
Usage notes
- Not a range. It has a single argument. For a different minimum use
minmax(<min>, <max>). - Difference from
minmax(auto, 250px): withfit-content(250px)the track is no larger than the content needs.minmax(auto, 250px)may stretch to 250px when there is room, even if the content is short (for example withjustify-content: stretch). - Mix with
1fr. The flexible column absorbs the leftover space, which is the typical sidebar-plus-content pattern. - Nothing else changes. Content longer than the limit wraps, or overflows if it contains an unbreakable string, so set
overflow-wrapwhere needed. - Sizing keyword. A
fit-contentkeyword (and function) also exists for properties likewidth, but the grid-track function is the form this page covers; check a compatibility table before using it forwidthin production.
Browser support
Supported for grid tracks in all current browsers; check a compatibility table for the width and height forms.
Related
minmax()โ a size range for tracksrepeat()grid-template-columns- Lesson: Grid tracks