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

前端 未结 6 820
天命终不由人
天命终不由人 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 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.

提交回复
热议问题