Difference in performance between img tag elements vs divs with background images?

前端 未结 6 762
天命终不由人
天命终不由人 2020-12-31 19:42

Are there any differences in performance or load/caching behavior when displaying images in img tags vs divs with image backgrounds?

My example:

相关标签:
6条回答
  • 2020-12-31 19:50

    AFAIK, browsers cache images the same whether they're in a DIV or an IMG. In any case, I think this one of those cases where specific performance is defined as an implementation detail internal to each rendering engine (and possibly the browsers built around them). As such, it's both out of our control as designers/developers and subject to change from browser to browser and version to version. In other words, I wouldn't spend too much time worrying about it.

    0 讨论(0)
  • 2020-12-31 19:52

    Another difference is with responsive layouts. If you have an element that is only shown at certain screen widths (ie, not on mobile phones), it will still load the image if it is specified in the html (using display:none for instance), but most all browsers now will NOT load the image if is a background-image specified in unused media query-CSS rules. A lot of early responsive layouts got criticized because they still used the same bandwidth as the full size sites.

    It is also useful with such designs because you can easily specify different images for different screen sizes. "Retina" displays on tablets and even laptops now won't look their best without 2x res graphics. So... even if you don't do such things now, it is probably a good practice to get into, because you might find yourself needing it soon!

    0 讨论(0)
  • 2020-12-31 20:02

    Technical differences yes, most notably you can set the width/height of an IMG tag and it will stretch the image, which can be useful in some situations.

    The main thing you've got to keep in mind though is the context of the image within the HTML document. If the image is content, say an image in a gallery, I'd use a IMG tag. If it is just part of the interface I might use a div instead.

    0 讨论(0)
  • 2020-12-31 20:07

    The main performance difference is using background images allows you to use CSS sprites. Having one image contain a large number of images from your page means the user only has to make one request instead of one for each image.

    0 讨论(0)
  • 2020-12-31 20:09

    The only difference I can conceive of it this:

    You can't scale images as backgrounds, although you can for img tags. This would open a scenario where background images loaded faster becuase it forces you to have the correct native size as opposed to downloading the entire image and scaling it. However, the converse could be true: given that you didn't care about image quality so much, you could deliver smaller imgs to your page and scale them up.

    I'd stick with whatever rendered cleaner (and more consistently -- IE/FF/Chrome/Safari/etc).

    0 讨论(0)
  • 2020-12-31 20:15

    I think by using background-image attribute in the div, the page layout gets loaded first and image present in the divs loaded later after the dom is loaded. so by using background-image the html layout is loaded faster on the web browser.

    0 讨论(0)
提交回复
热议问题