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

前端 未结 6 761
天命终不由人
天命终不由人 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: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!

提交回复
热议问题