The fit-content() CSS function clamps a given size to an available size according to the formula min(maximum size, max(minimum size, argument)).

Try it

The function can be used as a track size in CSS Grid properties, where the maximum size is defined by max-content and the minimum size by auto, which is calculated similar to auto (i.e., minmax(auto, max-content)), except that the track size is clamped at argument if it is greater than the auto minimum.

See the grid-template-columns page for more information on the max-content and auto keywords.

The fit-content() function can also be used as laid out box size for width, height, min-width, min-height, max-width and max-height, where the maximum and minimum sizes refer to the content size.


The fit-content() function accepts a <length> or a <percentage> as an argument.

/* <length> values */

/* <percentage> value */



An absolute length.


A percentage relative to the available space in the given axis.

In grid properties it is relative to the inline size of the grid container in column tracks and to the block size of the grid container for row tracks. Otherwise it is relative to the available inline size or block size of the laid out box depending on the writing mode.


Sizing grid columns with fit-content


<div id="container">
  <div>Item as wide as the content.</div>
    Item with more text in it. Because the contents of it are wider than the
    maximum width, it is clamped at 300 pixels.
  <div>Flexible item</div>


#container {
  display: grid;
  grid-template-columns: fit-content(300px) fit-content(300px) 1fr;
  grid-gap: 5px;
  box-sizing: border-box;
  height: 200px;
  width: 100%;
  background-color: #8cffa0;
  padding: 10px;

#container > div {
  background-color: #8ca0ff;
  padding: 5px;



CSS Box Sizing Module Level 4
# sizing-values

Browser compatibility

BCD tables only load in the browser

BCD tables only load in the browser

See also