在什么情况下使用HTML IMG标记来显示图像更合适,而不是CSS背景图像,反之亦然?

这些因素可能包括可访问性、浏览器支持、动态内容或任何类型的技术限制或可用性原则。


当前回答

Img是一个HTML标记是有原因的,因此应该使用它。用于引用或说明事物,如在文章中。

此外,如果图像有意义或必须是可点击的,img比css背景更好。对于所有其他情况,我认为,css背景可以使用。

尽管这是一个需要反复讨论的话题。

来自法国巴黎的学生

其他回答

对我来说,这是个非黑即白的决定。如果图像是内容的一部分,如logo、图表或人物(真实人物,而不是库存照片人物),则使用<img />标记加上alt属性。其他的都有CSS背景图片。

另一个使用CSS背景图像的时间是在做文本的图像替换时。段落/头。

Img是一个HTML标记是有原因的,因此应该使用它。用于引用或说明事物,如在文章中。

此外,如果图像有意义或必须是可点击的,img比css背景更好。对于所有其他情况,我认为,css背景可以使用。

尽管这是一个需要反复讨论的话题。

来自法国巴黎的学生

有些答案把情况复杂化了。这是一个非常简单的情况。

每次你想放一张图片的时候就回答这个问题:

这是内容的一部分还是设计的一部分?

如果你不能回答这个问题,你可能不知道你在做什么或你想做什么!

此外,不要仅仅因为你想要“打印机友好型”而考虑这两种技术。也不要从SEO的角度用CSS隐藏内容。如果您发现自己在CSS文件中管理内容,那么您是在搬起石头砸自己的腿。这只是什么是内容或不内容的一个微不足道的决定。其他方面都应该被忽略。

One more benefit from using the <IMG> tag is related to SEO - i.e. you can provide additional information about the image in the ALT attribute of the image tag, while there's no way to provide such information when specifying the image through CSS and in that case only the image file name may be indexed by search engines. The ALT attribute definitely gives the <IMG> tag SEO advantage over the CSS approach. That's why according to me it is better to specify the images you want to rank well in the image search results (e.g. Google Image Search) using the <IMG> tag.

以上回答只考虑设计方面。我在搜索引擎优化方面列出了它。

何时使用<img />

When Your Image need to be indexed by search engine If it has relation to content, including cards (click areas), but not related to design. Design is probably the most difficult thing to parse here because so it's all design right. I would say perhaps functional design (Cards, thumbnails, profile images, things you can click) vs Aesthetic design which is mostly used for sites appeal. List item If your image is not too small ( not iconic images ). Images where you can add alt and title attribute. Images from a webpage which you want to print using print media css

何时使用CSS背景图像

纯粹用于设计的图像。 与内容无关。 小图像,我们可以玩CSS3。 重复图片(在博客作者图标中,每篇文章都会重复日期图标等)。

因为我将基于这些原因使用它们。这些都是图像搜索引擎优化的良好实践。