I have an unordered list like in the example demo
li {
display: block;
border: 1px solid lightCoral;
list-style: none;
padding: 4px 6px;
margin: 5px;
width: 150px;
min-width: 120px;
max-width: 250px;
}
li:last-child{
width: 200px;
}
<ul>
<li> first item </li>
<li> second item very long content </li>
<li> third item </li>
<li> This is 200px wide</li>
</ul>
I want the li
items to be at least 120px
wide and at most 250px
. If I don't set the width, they automatically set it to max-width
. But if I set it to 150px
like in the demo, then why doesn't the second one get its maximum allowed width, i.e. 250px
even if its content doesn't fit into a single line?
Is there something I am missing? Can this be done with pure CSS?
When to use which: min-width or max-width. If you are designing your website for smaller devices first then set your default CSS breakpoints with min-width and adjust for larger devices accordingly. Meanwhile, if you are designing for larger devices first then use max-width and then tune for smaller devices accordingly ...
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). 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 .
Using Min Width and Max WidthIf min-width value is larger than max-width , then the min-width value will be taken as the width for the element. Consider the following example. The initial width value is 100px , and adding on that, there is min-width and max-width values.
Change the maximum width. max-width overrides width , but min-width overrides max-width .
A div takes by default 100% of its parent's width. So it will take the max-width
.
To accomplish what you want, float them and clear both sides:
li {
display: block;
float: left;
clear: both;
border: 1px solid lightCoral;
list-style: none;
padding: 4px 6px;
margin: 5px;
min-width: 120px;
max-width: 250px;
}
li:last-child {
width: 200px;
}
<ul>
<li>first item</li>
<li>second item very long content</li>
<li>third item</li>
<li>This is 200px wide</li>
</ul>
The behaviour you want is possible if you wrap your <li>
content inside a <div>
container. You can then make the <div>
containers inline-block
along with width: auto;
so that they don't conform to having identical lengths and thus you get the bordered boxes around your list elements to be determined by their content as shown in the snippet below.
li {
list-style: none;
margin: 5px;
}
li > div {
display: inline-block;
border: 1px solid lightCoral;
width: auto;
padding: 4px 6px;
min-width: 120px;
max-width: 250px;
}
li:last-child > div{
width: 200px;
}
<ul>
<li><div> first item </div></li>
<li><div> second item very long content </div></li>
<li><div> third item </div></li>
<li><div> This is 200px wide</div></li>
</ul>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With