1

I have an unordered list, listaAuto, which is filled with <li> items, which contain an img for each.

The problem is that if I use the <li> property display: inline;, they won't expand to contain the images. Instead, if I display them horizontally, they are no mistakes.

I already tried using overflow: auto, height: auto, clear: both: and other solutions, but I still can't find the way.

HTML:

<body>
    <div>
        <ul id="listaAuto">
            <li><img src="example.png" width="121.33" height="75.92"/></li>
            <li><img src="example.png" width="121.33" height="75.92"/></li>
        </ul>
    </div>
</body>

CSS:

#listaAuto, li{
    list-style: none;
    padding: 5px;
}

li{
    clear: both;
    display: inline;
    border: 1px solid black;
}

Current output:

Current output

UrbiJr
  • 133
  • 1
  • 2
  • 20

5 Answers5

4

Use inline-block for the li items:

https://jsfiddle.net/y86263mq/

Based on another Stackoverflow answer:

Inline elements:

  1. respect left & right margins and padding, but not top & bottom
  2. cannot have a width and height set
  3. allow other elements to sit to their left and right.
  4. see very important side notes on this [here][1].

Block elements:

  1. respect all of those
  2. force a line break after the block element

Inline-block elements:

  1. allow other elements to sit to their left and right
  2. respect top & bottom margins and padding
  3. respect height and width

Simple explaination

inline-block elements can have the heights defined. In this case, you're not explicitly setting the heights but the contents are.

Andre Pena
  • 56,650
  • 48
  • 196
  • 243
2

Add display:inline-block for li and remove the unnecessary clear:both. Refer this fiddle https://jsfiddle.net/wsmzhn1h/

Gayathri Mohan
  • 161
  • 1
  • 10
1

Adding float: left; and removing clear:both; on the li should do the trick. https://jsfiddle.net/y8sxhv1z/2/

Kappa
  • 193
  • 1
  • 2
  • 13
1

You may try this.

li{
display: inline-block;
}

li img{
max-width: 100%;
}
0

#listaAuto, li{
    list-style: none;
    padding: 5px;
}

li{
 width: auto;
 float: left;
    display: block;
    border: 1px solid black;
}
<body>
    <div>
        <ul id="listaAuto">
            <li><img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.svg?v=2bb144720a66" width="121.33" height="75.92"/></li>
            <li><img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.svg?v=2bb144720a66" width="121.33" height="75.92"/></li>
        </ul>
    </div>
</body>
Cedric Ipkiss
  • 5,662
  • 2
  • 43
  • 72