Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does using image sprites make sense in HTTP/2?

The bundling of JS and CSS files won't be necessary in HTTP/2, but what about image sprites?

Looking at the demo it seems that it already works way faster than HTTP/1.1, but won't bundling images into sprites make it even faster? I mean, won't the PNG's optimization algorithms work better when all the data is in a single file?

like image 689
Victor Marchuk Avatar asked Aug 22 '15 21:08

Victor Marchuk


People also ask

Should I use image sprites?

A web page with many images can take a long time to load and generates multiple server requests. Using image sprites will reduce the number of server requests and save bandwidth.

What is the benefit of using image sprites?

The main advantage of using image sprite is to reduce the number of http requests that makes our site's load time faster. The images also load faster making switching from one image to another on some event much smoother. Image Sprite are a collection of images placed into a single image.

How do I use an image as a sprite tag?

You create a defined area with a <a> with display:block; or <div> and use overflow hidden; to hide overflow and position:relative; . Then you place your <img> image sprite inside absolutely positioned, which is possible since you positioned the parent. Then use :hover on the image to change position.

What is the purpose of a CSS sprite?

CSS Sprites are a collection of images that are combined into a single file that an HTML document can access. These images are then called into use within the HTML code to be displayed on the website.


2 Answers

It depends of your image(s) sizes and format. If the images are big enough, you won't gain much by using sprites, but for small images there are significant gains, even when using HTTP/2. What makes HTTP/2 better is that there is much less overhead for headers and potentially even less round-trips (provided the server implements PUSH). The question is, how small should your files be to consider bundling them?

For bitmaps, you make a good point in that PNG's optimization algorithm works in favor of sprites, specially if their size is small enough. For example, while the image in this article from Gabriel Bouvigne is 17.4 kb, slicing it produces 132 separate images totaling 135 kb. When you add a small but nonetheless existing additional overhead for transfer, it's close to a factor of ten. And yes, size still matters when the bandwidth between the server and client is limited.

Actually, this also reaches text resources, like javascript, css, and SVG files. If they are small enough and they don't change frequently, you may still consider bundling them together. For example, the Javascript in the ng folder of Angular's source takes 69.6 kb if transferred as separate, minified and gzipped js. If you concatenate them before gzipping, it is just 31.9 kb. The factor here however is just slightly above two, and it may not be as significant if HTTP/2 saves connection time and round-trips. That further balances with the possibility of caching resources separately.

As a final note, if your small images/icons are SVG vectors (and they should!), then they count as text resources. Also, SVG vectors tend to be a bit bigger, for example, Firefox's SVG icon is 15.7 kb when gzipped. At this size, the decision of linking to it vs inlining or bundling is a no-brainer, if the HTTP/2 goodies are working.

like image 105
dsign Avatar answered Oct 22 '22 13:10

dsign


I will agree with @dsign that it depends.

There's a trend towards inlining the types of small images that would normally be used as sprites within the CSS itself as data-urls. If you're inlining your images into CSS, just keep them as separate entries, this also has the advantage of per-image optimizations. Optimizations for one image in a sprite don't match others... with PNG, you can opt for potentially lossy conversion into an image at/under 256 colors with alpha transparency layers such as TinyPNG and pngquant. some images can go under 16-colors. When you are creating a combined sprite, these kinds of optimizations are more limited.

I'm leaning towards either simply using CSS inlining or keeping images separate. The positive is that tools like webpack (and others that people are moving towards) makes this a fairly trivial configuration matter for web applications.

If you're in a position to implement server-side push for images via HTTP/2, then keeping them separate more likely works in your favor.

like image 36
Tracker1 Avatar answered Oct 22 '22 14:10

Tracker1