简体   繁体   English

为什么图像使用显示:内联但行为类似于内联块元素

[英]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 exactly in the same way. img元素的内联块和内联块之间是否有任何区别,从我可以看到它的行为方式完全相同。

IMG is an Inline & Replaced element. IMG是一个内联替换元素。

A replaced element is any element whose appearance and dimensions are defined by an external resource. 替换元素是其外观和尺寸由外部资源定义的任何元素。

As per W3C 按照W3C

The IMG element has no content; IMG元素没有内容; 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. 它通常由src属性指定的图像replaced inline ,例外是左侧或右侧对齐的图像“浮动”在线外。

Check this link for more http://reference.sitepoint.com/css/replacedelements 查看此链接以获取更多信息http://reference.sitepoint.com/css/replacedelements

The default browser stylesheets were initially created using CSS1 for HTML3.2 , so inline-block was not available or necessary. 默认的浏览器样式表最初是使用CSS1 for HTML3.2创建的,因此内联块不可用或不必要。 There's no difference between them for image elements. 它们之间对于图像元素没有区别。

References 参考

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. 对于第二部分,即使在CSS中指定了width和height属性,某些浏览器(如Firefox)也会将无图像img标记呈现为跨度。

You can try it out yourself with this HTML code: 您可以使用此HTML代码自行尝试:

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

And corresponding CSS: 和相应的CSS:

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

Or see the difference in rendering effect with this Demo on JsFiddle . 或者在JsFiddle看到这个Demo的渲染效果的差异。

内联块允许您使用框模型样式(例如赋予其尺寸)来操纵对象的外观,但允许您使对象保持内联对齐,如文本。

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 . Inline-block是一个较新的CSS2声明, 并未在IE 6/7中完全实现

它只是一个支持维度属性的内联元素: 嵌入式内容 - img元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM