Skip to content Skip to sidebar Skip to footer

Issue With Css Min-width And Max-width

I have an unordered list like in the example demo 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

Solution 1:

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: 4px6px;
  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>

Solution 2:

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: 4px6px;
  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>

Solution 3:

The display: table property can be used to achieve the effect you want. If you do not set width, the element extends along the length of the content, otherwise the sum of the width of the margin, border, padding, width.

li {
    display: table;
    border: 1px solid lightCoral;
    list-style: none;
    padding: 4px6px;
    margin: 5px;
    width: auto;
    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>

Solution 4:

That's because you set the width to 150px. If you remove the width attribute or give the auto value to it then it will work as expected. Of course, all the items will get the width of the longest of them: updated demo

li {
    display: block;
    border: 1px solid lightCoral;
    list-style: none;
    padding: 4px6px;
    margin: 5px;
    width: auto;
    min-width: 120px;
    max-width: 250px;
}

Solution 5:

This is not a problem, is the normal behaviour of display: block elements. If you need that the width be the content's width you can make a display:inline-block, and float:left; clear:both to avoid the stack problem

li {
  display: inline-block;
  border: 1px solid lightCoral;
  list-style: none;
  padding: 4px6px;
  margin: 5px;
  width: auto;
  min-width: 120px;
  max-width: 250px;
  clear:both;
  float:left;
}
    
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>

Post a Comment for "Issue With Css Min-width And Max-width"