In the past, I saw the next css and I was thinking if there is some actual difference between
min-width: 90px;
max-width: 90px;
and
using width
will simply specify fixed width over the element without paying attention to its content (so you can have overflow) :
div {
width: 80px;
border:2px solid red;
}
Using max-width
means that the element will have an upper bound for its width. So its width can be from 0 to max-width depending on its content.
div {
max-width: 300px;
border: 2px solid red;
}
.diff {
display: inline-block;
}
And min-width
specify lower bound for width. So the width of the element will vary from min-width to ... (it will depend on other style).
div {
min-width: 300px;
border: 2px solid red;
}
.diff {
display: inline-block;
min-height:50px;
}
So if you specify min-width
and max-width
, you will set up a lower and upper bound and if both are equal it will be the same as simply specifing a width
.
div {
min-width: 300px;
max-width: 300px;
border: 2px solid red;
}
.diff {
display: inline-block;
min-height:50px;
}
In some particular cases, width
will not give the same result as min-width
/max-width
like with Flexbox where we have the shrink feature that allow an element to shrink to fit its container
.box {
width:200px;
border:1px solid red;
display:flex;
margin:5px;
}
.box > div {
border:2px solid;
height:50px;
}
As you can see in the second case the element will not shrink because, unlike width
, min-width
will prevent this.
Another case is the use of resize
property:
div {
border: 2px solid;
height: 50px;
overflow: auto;
resize: both;
}
As you can see, we can resize the element defined by width
and not the one defined by min-width
/max-width
We should also note that min-width
/max-width
is more powerful than width
. Setting the 3 properties to different values will make min-width
the winner
.box {
border: 2px solid;
height: 50px;
width:100px;
min-width:200px;
max-width:150px;
}
This means that we can override a value set by width
using min-width
/max-width
but not the opposite