Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which way to load a huge image (canvas vs img vs background-image)?

What I want

Currently I have a png image of 4000x4000. After using tinypng.org it became just a 288KB file.

Now I want the fastes way to load the image, place it in the DOM and be able to draw a lot of canvas' on it.

I tested some and the results stunned me.

What I tested

I made 3 tests and check only the load speed.

  • (423ms) <canvas>
  • (138ms) <img>
  • (501ms) CSS background-image

The <img> tag is the fastest.

Question

So, is it bad-practice to use the <img> tag to display a huge (background) image and use some dirty CSS to be able to draw canvas on it?

Or is it better to use canvas in my case and don't worry about the longer load time?

like image 821
Ron van der Heijden Avatar asked Feb 21 '13 09:02

Ron van der Heijden


People also ask

Which is better IMG or background image?

It is widely known that using the image tag is better SEO practice than using background images because of the screen reader implications.

What is the difference between background image and IMG tag?

#The difference between HTML images <img> and CSS background images. The HTML <img> element is for images that are part of the content, while CSS background images are purely decorative.

When should we apply background image and when should we use IMG element in HTML document?

1. CONTENT : Use an <img> tag if it is content related and not just being used as a design element, while backgroung image has nothing to do with the content and is purely a design element.

Should I use IMG or DIV?

Background on a div is for decorative purposes. If the image itself is the content, use an img tag.


1 Answers

Great question! This is related: When to use IMG vs. CSS background-image?

From that article, if people are intended to print your page you wouldn't use <img> - as this would appear on the print. The same would apply to <canvas>, making background-image the logical solution.

How is your background image added through CSS? Is it inline or through its own stylesheet? If it's using its own stylesheet, have you tried compressing the CSS before testing the speed?

This is also related, I suppose: Do Images Load Faster in HTML or CSS?

like image 118
James Donnelly Avatar answered Nov 10 '22 18:11

James Donnelly