Fit-content - Value CSS
Summary of characteristics of the fit-content
value
The fit-content
value.
The fit-content
value determines an optimal dimension by considering the content of the element and the available space.
Basically, fit-content
chooses the best size between auto
, min-content
, and max-content
.
The exact formula is:
min(max-content, max(min-content, <place-disponible>))
See also the explanations for the max-content
and min-content
values and the fit-content()
function.
The latter performs the same calculation as the fit-content
value but allows you to set up the available space.
The fit-content
value can be used with the following properties:
block-size
: Defines the dimension of an element following the direction of the blocks.flex-basis
: Determines the default dimension of an element contained in a flex-box.height
: Sets the height of the element.inline-size
: Sets the dimension of the element in the direction of the lines.max-height
: Sets a limit for the maximum height of the element.max-width
: Sets the maximum width of the element.min-height
: Sets the minimum height of the element.min-width
: Sets the minimum width of the element.width
: Sets the width of the element.
Examples of using fit-content
.
Using fit-content
to set the width of an element.
The three blocks below have their width set to fit-content
.
We can see that their width is adjusted to the content, but without exceeding the available space, in this case the width of the page.
Using fit-content
to set the width of columns in a grid.
The fit-content
value cannot be used to size columns in a grid.
On the other hand, the fit-content()
function can be, probably because the function allows you to set a maximum value.
Using fit-content
to set the width of elements in a flex-box.
All elements in the flex-box below have a width defined by fit-content
. In addition, the flex-grow
and flex-shrink
properties
have been set to 0
.
We can see that fit-content
behaves like max-content
, i.e. the elements are sized according to their contents,
without adding line breaks, and without taking into account the available width.
Using fit-content
in an array.
The fit-content
value is not effective for defining the dimensions of an array or cells. The latter are already defined by something
which looks like fit-content
.
Browser compatibility.
fit-content
to size the width of any element with properties like width
, grid-template-columns
, etc.fit-content
to size the height of any element with properties such as height
, grid-template-rows
, etc.fit-content()
function to size elements of a flexbox.fit-content
value
used with
width
fit-content
value
used with
height
fit-content
value
used with
flex-basis
Browsers on computers :
Mobile browsers :
Outdated or marginal browsers :

Internet Explorer

QQ Browser

Safari sur IOS

Firefox pour Androïd

Opéra Mobile

Baidu Browser

KaiOS Browser

UC Browser pour Androïd

Samsung Internet

Chrome

Edge

Androïd Brower

Chrome pour Androïd

Opéra

Safari

Firefox

Opéra mini
fit-content
value historic.
-
CSS Box Sizing Module Level 3
First description of themax-content
value.September 27, 2012Working Draft.
See also, concerning the sizing.
The fit-content
value and everything related to block sizing is described in the CSS Box Sizing Module module.
Properties:










