Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<img /> vs background-image (css) in performance

I am building a site that is using a scrolling plugin that basically animates the scrolling. I am quite concern about performance as if I insert some images in the site, it looks quite choppy when scrolling/animating.

The main problem I can detect with images is the reflow/repaint issue, when the image doesn't have the correct dimensions and therefore is scaled (I have to deal with this, I know about the best practice).

With this statement in mind (images will be scaled). What should be better, image element or divs with those images as backgrounds as for performance?

I made this jsFiddles that in my chrome browser memory tool, show that the back-ground image option uses less memory.

<img />: http://jsfiddle.net/ek6Xn/

<img src="..." /> 

background-image: http://jsfiddle.net/ek6Xn/1/

<div></div> 
div {     background:url(...);     background-size:100% 100%; } 

References:

  1. Difference in performance between img tag elements vs divs with background images?
  2. When to use IMG vs. CSS background-image?
  3. http://www.html5rocks.com/en/tutorials/speed/unnecessary-paints/
  4. http://updates.html5rocks.com/2013/02/Profiling-Long-Paint-Times-with-DevTools-Continuous-Painting-Mode
like image 351
Alvaro Avatar asked Jun 25 '13 03:06

Alvaro


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 .

What is the difference between an HTML inline image and a CSS background image indicate the optimal conditions for use each?

Inline images come with their natural height, however, the container used for a background image requires some sort of height added to it for it to be even visual on the screen. Additionally, background images support several CSS properties that should always be placed with them.

Is it better to put images in HTML or CSS?

Summing up: if an image has meaning, in terms of your content, you should use an HTML image. If an image is purely decoration, you should use CSS background images.


1 Answers

Personally I would forget about performance in this instance and focus on what the image is:

1) Image = content

2) Background Image = design

The way I tend to think about this is, do I want the image to appear on all screen sizes, on all devices, with CSS turned either on or off? Do I want the image to be "indexed" by Google and Facebook. If the answer is yes to all or any of these questions, then usually the image is "content" and you should use an IMG tag.

If you want a different image to display at different screen sizes (or no image at all on certain devices), or if you're happy for the image to not appear on a print out, or you're happy for the image to not appear if CSS is turned off, then usually the image is "design" and you should use a background image.

like image 152
MrCarrot Avatar answered Sep 18 '22 06:09

MrCarrot