Skip to content Skip to sidebar Skip to footer

Why Does Image Use Display: Inline But Behaves Like An Inline-block Element

Why is the default display style for image inline instead of inline-block? Is there any difference between inline and inline-block for img elements, from what I can see it behaves

Solution 1:

IMG is an Inline & Replaced element.

A replaced element is any element whose appearance and dimensions are defined by an external resource.

As per W3C

The IMG element has no content; it is usually replaced inline by the image designated by the src attribute, the exception being for left or right-aligned images that are "floated" out of line.

Check this link for more http://reference.sitepoint.com/css/replacedelements

Solution 2:

The default browser stylesheets were initially created using CSS1 for HTML3.2, so inline-block was not available or necessary. There's no difference between them for image elements.

References

Solution 3:

The first part of your question is already answered, so I shall not repeat.

For the second part, some browsers like Firefox renders a no-image img tag as a span even when width and height attributes are specified in CSS.

You can try it out yourself with this HTML code:

<imgalt='no image'src='about:blank'><br><imgalt='no image'src='about:blank'id=iblock>

And corresponding CSS:

img {
    height: 100px;
    width: 100px;
    background: cyan;
}
#iblock {
    display: inline-block;
}

Or see the difference in rendering effect with this Demo on JsFiddle.

Solution 4:

Inline-block allows you to manipulate the object's appearance with box-model styling (such as giving it dimensions), but allows you to keep the object aligned inline, like text.

Solution 5:

Inline block is the same as inline, except for it allows you to adjust block properties such as padding and margin. By default, images are supposed to semantically flow with text like a diagram in a news article, that is why all the original attributes are to do with aligning the image with the text flow.

Inline-block is a newer CSS2 declaration, and not fully implemented in IE 6/7.

Post a Comment for "Why Does Image Use Display: Inline But Behaves Like An Inline-block Element"