fit-content() CSS Function
The fit-content() function sizes a grid track to fit its content, but never larger than a given maximum, such as fit-content(300px).
Description
The fit-content() CSS function sizes a grid track to fit its content, but never larger than a given maximum. It is used in grid-template-columns, grid-template-rows, grid-auto-columns and grid-auto-rows. A track defined with fit-content(200px) shrinks around short content, grows with longer content, and stops at 200px, after which its content wraps.
The track size is calculated as min(max-content, max(auto, limit)). In practice it behaves like auto up to the limit, but it does not stretch to fill leftover space the way an auto track can. The argument is a length or a percentage; percentages are resolved against the size of the grid container in the relevant axis. Note that the function form is different from the fit-content keyword used with properties such as width.
/* Sidebar sized to its content, up to 250px */
.layout {
display: grid;
grid-template-columns: fit-content(250px) 1fr;
gap: 1rem;
}
/* Labels column that never gets wider than 30% */
.form {
display: grid;
grid-template-columns: fit-content(30%) 1fr;
}
Interactive Demo
Syntax
fit-content() = fit-content( <length-percentage [0,∞]> )
Values
- <length-percentage>The maximum size of the track, as a non-negative <length> or a <percentage> of the grid container's size in that axis. The track is sized to its content but clamped to this limit.
Example
Browser Support
The following information will show you the current browser support for the CSS fit-content() function. Hover over a browser icon to see the version that first introduced support for this CSS function.
Desktop





Tablets & Mobile





