Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between CSS fit-content and max-content?

Tags:

css

width

I'm following this article https://css-tricks.com/almanac/properties/w/width/ to try to understand how this rules work.

I have this example:

*{margin:0; padding:0} .box{   background: lightgreen;   margin: 0 auto;   width: -webkit-fit-content;   width: -moz-fit-content;   width: fit-content; }
<div class="box">   <img src="https://tyrannyoftradition.files.wordpress.com/2012/05/cutest-kitten-hat-ever-13727-1238540322-17.jpg" alt="" />   <figure>Yes, put some text here that is wider than the image above to try some new rules</figure> </div>

The article says that fit-content can be used to center a div of unknown width with margin: x auto;

But if you change fit-content for max-content in this example, this is working anyway and they seem to behave always in the same way.

Does anyone know what is the difference between this two rules and in which cases should I use one or the other?

like image 591
Vandervals Avatar asked Jun 08 '15 08:06

Vandervals


People also ask

What is CSS fit-content?

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

What is Max-content in CSS?

The max-content sizing keyword represents the intrinsic maximum width or height of the content. For text content this means that the content will not wrap at all even if it causes overflows.

What is min-content and max-content in CSS?

max-content determines a size assuming that there is infinite available space. On the other hand, min-content defines an element's minimal possible size that does not lead to overflowing content.

What does fit-content mean?

The fit-content behaves as fit-content(stretch) . In practice this means that the box will use the available space, but never more than max-content . When used as laid out box size for width , height , min-width , min-height , max-width and max-height the maximum and minimum sizes refer to the content size.


2 Answers

fit-content uses max-content, unless available < max-content, then it uses available. Unless available < min-content, then it uses min-content.

like image 188
jiggunjer Avatar answered Oct 07 '22 06:10

jiggunjer


In a few words width: fit-content; means :

"Use the space you can (available) but never less than your min-content and never more than your max-content"

like image 31
Juanma Menendez Avatar answered Oct 07 '22 06:10

Juanma Menendez