何时使用 IMG 与 CSS 背景图像? [英] When to use IMG vs. CSS background-image?
本文介绍了何时使用 IMG 与 CSS 背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在什么情况下使用 HTML IMG
标签来显示图像比使用 CSS background-image
标签更合适,反之亦然?
In what situations is it more appropriate to use an HTML IMG
tag to display an image, as opposed to a CSS background-image
, and vice-versa?
因素可能包括可访问性、浏览器支持、动态内容或任何类型的技术限制或可用性原则.
Factors may include accessibility, browser support, dynamic content, or any kind of technical limits or usability principles.
推荐答案
IMG的正确使用
- 如果您打算使用
IMG
人们打印你的页面希望默认包含图像.—JayTee - 当图像具有重要语义时使用
IMG
(带有alt
文本),例如 警告图标.这可确保图像的含义可以在所有用户代理(包括屏幕阅读器)中传达.
- Use
IMG
if you intend to have people print your page and you want the image to be included by default. —JayTee - Use
IMG
(withalt
text) when the image has an important semantic meaning, such as a warning icon. This ensures that the meaning of the image can be communicated in all user-agents, including screen readers.
IMG 的实际应用
- 使用
IMG
加上 alt 属性,如果图像是内容的一部分这样的作为徽标或图表或人物(真人,不是库存照片人物).—sanchothefat - 如果您依赖浏览器缩放以与文本大小成比例呈现图像,请使用
IMG
. - 使用
IMG
IE6 中的多个叠加图像. 使用IMG
和z-index
按顺序拉伸背景图像以填满整个窗口.
注意,CSS3 background-size 不再如此;见下面的#6.- 使用
img
而不是background-image
可以显着提高背景动画的性能.
- Use
IMG
plus alt attribute if the image is part of the content such as a logo or diagram or person (real person, not stock photo people). —sanchothefat - Use
IMG
if you rely on browser scaling to render an image in proportion to text size. - Use
IMG
for multiple overlay images in IE6. UseIMG
with az-index
in order to stretch a background image to fill its entire window.
Note, this is no longer true with CSS3 background-size; see #6 below.- Using
img
instead ofbackground-image
can dramatically improve performance of animations over a background.
何时使用 CSS background-image
- 使用 CSS 背景图片,如果图片 不是内容的一部分.—sanchothefat
- 在以下情况下使用 CSS 背景图片做图像替换文本例如.段落/标题.—sanchothefat
- 如果您打算使用
background-image
人们打印你的页面不希望默认包含图像.—JayTee - 如果您需要缩短下载时间,请使用
background-image
,如使用 CSS 精灵. - 如果您只需要显示图像的一部分(如 CSS 精灵),请使用
background-image
. - 使用
background-image
和background-size:cover
来拉伸背景图像以填充其整个窗口.
- Use CSS background images if the image is not part of the content. —sanchothefat
- Use CSS background images when doing image-replacement of text eg. paragraphs/headers. —sanchothefat
- Use
background-image
if you intend to have people print your page and you do not want the image to be included by default. —JayTee - Use
background-image
if you need to improve download times, as with CSS sprites. - Use
background-image
if you need for only a portion of the image to be visible, as with CSS sprites. - Use
background-image
withbackground-size:cover
in order to stretch a background image to fill its entire window.
这篇关于何时使用 IMG 与 CSS 背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文