Do Images Load Faster in HTML or CSS?

前端 未结 5 552
生来不讨喜
生来不讨喜 2020-12-08 07:45

If I load an image using this html on my sidebar



        
5条回答
  •  猫巷女王i
    2020-12-08 08:26

    This can easily be verified using Firebug (under Net), Chrome Developer Tools (under Network), Fiddler or any other HTTP sniffer you prefer.

    If you put the image in CSS as background-image, the image will only get downloaded when that class is actually used and visible. If you put it in an img it'll be downloaded immediately and will block rendering even if it's invisible.

    In the end they are both as fast if you are counting the speed at which the image loads per se. The real question is if the perceived performance is better as the order at which the image gets downloaded might be different depending on where you place your element.

    I would worry more about the other aspects though. Having the image as a background-image means:

    • The image is not a content
    • It is not printable
    • You can use a sprite to reduce the number of HTTP requests (thus improving performance)
    • It is slower to animate background-image than img

提交回复
热议问题