126

How are the min-content and max-content values calculated in CSS?

And what does intrinsic dimension mean?

Wes
  • 3,978
  • 4
  • 24
  • 45

2 Answers2

209

Note: "width" in this text refers to the "logical width", not CSS's width; that is, the values are also valid for CSS's height, if the language direction is vertical (like east-asian languages) or in flexbox or grid. min-content and max-content are valid values for width, height, min-width, min-height, max-width, max-height and even more properties (like flex-basis).

What are the intrinsic dimensions of a box?

CSS sizing level 3 introduced the concept of intrinsic dimensions - the opposite of extrinsic. An extrinsic dimension of a box is calculated on the box's parent box. For example width: 80% is said an extrinsic dimension as the width of the subject depends on the width of its containing box.

Contrarily to that, width: min-content is said intrinsic as the width of the box is calculated on the dimension of the contents that the box itself contains.

Intrinsic dimensions are properties of the box itself, extrinsic dimensions are only available if the box is placed in the document and in a context that permits these values to be calculated. For example, in CSS-flow (the classic CSS layout mode), as you probably know, height: 20% has only effect if height is defined in the parent element (i.e. it's inheritable); that is a case of an extrinsic dimension that is not calculable (when an extrinsic value is not available, CSS fallbacks to its intrinsic equivalent). Instead height: min-content is always calculable, as it is intrinsic to the box itself, and it is always the same regardless of the box's placement (or the box's absence) in the document.


The definition of min-content and max-content has changed many times over the years but the result always stayed the same, and it is pretty straightforward to grasp. They were originally requested by the community as keywords for width, whose computed value would match the widths of a box when the box is floating. And indeed, the definition of these two properties was originally based on the behavior of float; now they are more generically defined as follows:

min-content

https://www.w3.org/TR/css-sizing-3/#min-content

The smallest size a box could take that doesn’t lead to overflow that could be avoided by choosing a larger size.

In other words, the smallest width of a box where the box's contents don't overflow the box itself.

A good way to visualize this is using, in fact, float.

/* the computed width of #red in this example 
   equals to specifying #red { width: min-content } */
#blue        { width: 0px; }
#blue > #red { float: left; }

min-content

(GIF source: http://jsfiddle.net/6srop4zu/)

In the above GIF, the red box's min-content width equals the red box's width at the time the blue box's width is 0px (234px in the GIF, might be different in the jsfiddle).

As you can see, if the red box was made smaller, the word supercalifragilisticexpialidocious would overflow the red box, hence in this case the min-content equals the width of that particular word, as it is the one that takes the most space horizontally.

max-content

https://www.w3.org/TR/css-sizing-3/#max-content

A box’s “ideal” size in a given axis when given infinite available space. Usually this is the smallest size the box could take in that axis while still fitting around its contents, i.e. minimizing unfilled space while avoiding overflow.

/* the computed width of #red in this example
   equals to specifying #red { width: max-content } */

#blue        { width: 99999999999999999px; } /* ~infinite */
#blue > #red { float: left; }

max-content

(GIF source: http://jsfiddle.net/nktsrzvg/)

In the above GIF, the red box's max-content width equals the red box's width when the blue box's width is infinite (386px in the GIF, might be different in the jsfiddle).

Here, the red box fully takes advantage of the available space on the x axis in the blue box, but without wasting it. The contents are allowed to expand on the relevant axis without restrictions, and the red box wraps them and at the point of maximum expansion.


What about fit-content, stretch and others? These properties are currently being revisited and as such they are not stable (date: July 2018). They will be added here when they get a bit more mature (hopefully soon).

Wes
  • 3,978
  • 4
  • 24
  • 45
  • 2
    Very clear explanation. Just one more thing: how do they compare to `auto`? Is there any fundamental difference in design theory between them? – Jinghui Niu Jan 03 '19 at 05:29
  • 1
    Auto results in different things depending on the layout used. For example the behavior is different depending on the axis, if you are using block or flexbox or grid, etc. – Wes Jan 03 '19 at 13:30
  • 1
    @ Wes, But the documentation says `auto` is also intrinsic, only determined by its content? – Jinghui Niu Jan 04 '19 at 14:42
  • 6
    width:auto is not always intrinsic. For example it's calculated to the outer box's width (therefore extrinsic) if display:block – Wes Jan 08 '19 at 14:31
  • There seems to be one case where `width: min-content` does act according to its specification: when it contains flex items with `flex-shrink: 0`. I documented my findings [here](https://stackoverflow.com/q/65083319/242365). – cdauth Dec 01 '20 at 01:56
32

min-content:

The minimum width that a content (a group of words) needs. It means width of the biggest word in the content.

Example:

hi this is

biggestWordInTheContent

<---- min-content ---->

max-content:

The maximum possible width that a content (a group of words) can take. It means the width of the content when all the words arranged together in one line.

Example:

hi this is a content without considering any line breaks.

<---------------------- max-content ------------------->
Ahmad Mobaraki
  • 7,426
  • 5
  • 48
  • 69
  • 1
    This post would be improved by code implementations of your examples rather than just text in code formatting. – TylerH Jan 01 '22 at 15:18
  • 1
    The output on the first one doesn't look like that when using `min-content`, `hi this is` should not break as `biggestWordInTheContent` is longer than the the three words combined. The snippets could have shown this properly as well. – Yong Feb 10 '22 at 23:50
  • 2
    Nice catch @Yong! I edited the answer. About snippets, I approved it first but I It made the answer more complicated I believe, nothing too complicated to be in snippet, users easily can try it themselves. I want the answer to be minimal and understandable at the first glance!! – Ahmad Mobaraki Feb 11 '22 at 07:48
  • Yes @AhmadMobaraki , I think it was better 'simply' as you described it. It was more direct which is what one needs most of the time – Emeka Orji Mar 31 '22 at 22:27
  • Good answer, I just don't really understand why this is so. When would you ever want text to never ever wrap no matter how long, or be as big as the word length? Is this old or new CSS specifications? – gunslingor Jul 21 '22 at 17:38