Why does image use display: inline but behaves like an inline-block element

青春壹個敷衍的年華 提交于 2019-12-04 07:15:19

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

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

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:

<img alt='no image' src='about:blank'><br>
<img alt='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.

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.

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.

It's simply an inline element that supports dimension attributes: Embedded content - the img element.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!