Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inconsistency for displaying <li><img /></li>

I have a problem with very simple HTML markup, which is rendered different in Chrome and Firefox. I'm wondering whether it is a bug in one of them.

The code is as simple as:

<ul>
    <li>
        <img />
    </li>
</ul>

The problem is that in Chrome the <li /> element has some padding at the top, but only if its content is an image. There is no problem with e.g. text.

Example fiddle: https://jsfiddle.net/8c4rujvu/1/

img {
  display: block;
  width: 500px;
}
li {
  border: 1px solid #f00;
}
<ul>
  <li>
    <img src="http://www.w3schools.com/css/trolltunga.jpg">
  </li>
  <li>
    <img src="http://www.w3schools.com/css/trolltunga.jpg">
  </li>
  <li>Some text</li>
</ul>

This is how it displays in Firefox (50.0.2): Firefox (50.0.2)

And in Chrome (55.0.2883.75 m): Chrome (55.0.2883.75 m)

What seems to be a problem here?

like image 287
Krzysztof Sowa Avatar asked Dec 07 '16 07:12

Krzysztof Sowa


People also ask

Can you put IMG in Li?

Firstly, you need to create the List Item using a li list item tag. Within that List Item tag, you can place your image. You add an image within the Image tag img. Hope this helps explain it a bit better.

How do I force an image to fit in a div?

To auto-resize an image or a video to fit in a div container use object-fit property. It is used to specify how an image or video fits in the container. object-fit property: This property is used to specify how an image or video resize and fit the container.

Can you put an image in a div?

1) Create a DIV tag with a unique ID; 2) Place the image into a background:url style element of a DIV tag; 3) Set the height and width properties of the DIV tag to that of the selected image.


2 Answers

This issue can simply fix float:left property too , check this fiddle

https://jsfiddle.net/9wp619xz/

check with the bug details Fixing Google Chrome compatibility bugs in websites - FAQ

https://www.chromium.org/Home/chromecompatfaq

img {
  float:left;
  width: 500px;
}
li {
  border: 1px solid #f00;
  float:left;
  width:100%;
}
<ul>
  <li>
    <img src="http://www.w3schools.com/css/trolltunga.jpg">
  </li>
  <li>
    <img src="http://www.w3schools.com/css/trolltunga.jpg">
  </li>
  <li>Some text</li>
</ul>
like image 27
Jishnu V S Avatar answered Oct 20 '22 00:10

Jishnu V S


This is due the default browser / user agent styling difference for display: list-item.

As a fix, you can use inline-block and vertical-align:top (or even just vertical-align: top) for the img to get common behaviour - see demo below:

img {
  display: inline-block;
  vertical-align: top;
  width: 500px;
}
li {
  border: 1px solid #f00;
}
<ul>
  <li>
    <img src="http://www.w3schools.com/css/trolltunga.jpg">
  </li>
  <li>
    <img src="http://www.w3schools.com/css/trolltunga.jpg">
  </li>
  <li>
    Some text
  </li>
</ul>

A related question you may want to look at: Why alignment mark list is different on WebKit when using :before height?

Why this happens?

Given that other browsers do not agree with chrome on this, this clearly looks like a bug and it is. See this open bug documented in Chromium Bug Tracker:

Table inside list item rendered at wrong position(Example URL: http://jsfiddle.net/P8Ua7/)

See excerpts from one of the comments in the bug:

Not limited to tables. Putting a flexbox inside a list-item gives the same result. It also happens if you have replaced content displayed as block.

The OP has an image (which is an inline replaced element) displayed as block!

Here is another bug you may want to check out.

like image 144
kukkuz Avatar answered Oct 20 '22 00:10

kukkuz