Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

My example:

I have a site with many overlapping images, some of which I will need to load dynamically with javascript. One issue is that I need to anchor the images to the right of the element, so that I can do a nice wipe-to-right effect. Because of this I was using a div with background image positioned right. Couldn't figure out how to do this with img but since divs are working for me I didn't know if this would matter...

like image 741
Yarin Avatar asked Sep 14 '10 18:09

Yarin


People also ask

Is it better to use background image or IMG tag?

Using img instead of background-image can dramatically improve performance of animations over a background.

Can I use IMG tag as background image?

The img could also use object-fit: cover; to replicate a background image with background-size: cover; . You can play with the absolute positioning properties top , right , bottom , left . In combination with setting the min-width and max-width to 100% instead of the width and height .

Can we use several background images for an element?

CSS allows you to add multiple background images for an element, through the background-image property. The different background images are separated by commas, and the images are stacked on top of each other, where the first image is closest to the viewer.


6 Answers

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.

like image 69
mr. w Avatar answered Oct 01 '22 22:10

mr. w


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.

like image 32
Ben Avatar answered Oct 01 '22 22:10

Ben


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!

like image 29
Michael Hussey Avatar answered Oct 01 '22 21:10

Michael Hussey


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.

like image 27
James Avatar answered Oct 01 '22 23:10

James


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).

like image 22
Brad Avatar answered Oct 01 '22 22:10

Brad


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.

like image 37
Paul Avatar answered Oct 01 '22 21:10

Paul