北城已荒凉 2009-01-29 18:25 采纳率: 0%
浏览 542
已采纳

什么时候使用 IMG vs. CSS 背景图片?

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.

转载于:https://stackoverflow.com/questions/492809/when-to-use-img-vs-css-background-image

  • 写回答

28条回答 默认 最新

  • bug^君 2009-09-23 23:58
    关注

    Proper uses of IMG

    1. Use IMG if you intend to have people print your page and you want the image to be included by default. —JayTee
    2. Use IMG (with alt 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.

    Pragmatic uses of IMG

    1. 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
    2. Use IMG if you rely on browser scaling to render an image in proportion to text size.
    3. Use IMG for multiple overlay images in IE6.
    4. Use IMG with a z-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.
    5. Using img instead of background-image can dramatically improve performance of animations over a background.

    When to use CSS background-image

    1. Use CSS background images if the image is not part of the content. —sanchothefat
    2. Use CSS background images when doing image-replacement of text eg. paragraphs/headers. —sanchothefat
    3. 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
    4. Use background-image if you need to improve download times, as with CSS sprites.
    5. Use background-image if you need for only a portion of the image to be visible, as with CSS sprites.
    6. Use background-image with background-size:cover in order to stretch a background image to fill its entire window.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(27条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示