CSS Reference ยท Units and Values
Intrinsic sizing keywords
Reference for the intrinsic sizing keywords min-content, max-content and fit-content: what each measures, where they work and how they differ from auto.
Most sizes in CSS are decided by the container ("extrinsic" sizing). The intrinsic sizing keywords let the content decide instead. They go in width, height, min-width, max-width and their logical equivalents, and in grid track lists.
min-contentis the smallest width at which the content does not overflow: for text, the width of the longest unbreakable word.max-contentis the width the content would take if it never wrapped: the full text on one line.fit-contentismax-content, but never more than the available space and never less thanmin-content.
.label { width: max-content; } .card { width: fit-content; max-width: 30ch; }
Try it
Syntax
width: min-content | max-content | fit-content | fit-content( <length-percentage> );
The keyword form of fit-content is the same as fit-content(stretch) in the spec. The function form clamps against a custom limit:
fit-content(<length-percentage>) = min(max-content, max(min-content, <length-percentage>))
Values
| Value | Description |
|---|---|
min-content |
The narrowest size that avoids overflow, with every soft wrap opportunity used. |
max-content |
The widest size: no soft wrapping. Long text can overflow its container. |
fit-content |
max-content, limited by the space the container offers, and not below min-content. |
fit-content(<length>) |
The same formula with your own limit instead of the available space. |
Usage notes
fit-contentis a "shrink-wrap" box. It is the usual choice for badges, tooltips and centered dialogs that should hug their text but wrap when space runs out.max-contentcan overflow. Pair it withmax-width: 100%if long content might exceed the container.- Images and replaced elements use their intrinsic dimensions (or aspect ratio) for these keywords.
- Grid tracks.
min-content,max-contentandfit-content()are valid track sizes; seegrid-template-columns. - Different from
auto. A block'swidth: autostretches to the container;fit-contentshrink-wraps. - Percentages cannot be mixed in without care.
fit-content(50%)resolves against the container's size, which must be definite. - Heights. On the block axis these keywords usually behave like
auto, because height already follows content.
Browser support
The three keywords are supported in current browsers; older versions needed vendor prefixes, so check a compatibility table when you must support old browsers.
Related
fit-content()โ the function formwidthโ where these keywords are used mostgrid-template-columnsโ track sizing- Lesson: Width and Height