Skip to content


Faster Page Loads With Image Concatenation

[[innerindex]]

Introduction

When designing web applications, icons and images are used to enhance the user experience, give visual cues, and simply look sexy. For complex web apps, the quantity and resulting latency of icons and images used can greatly impact page load times...and developers, in most cases, generally try to reduce page load time with a sweet web app rather than increase it.

To reduce latency in my apps, I use Image Concatenation! Coupled with a bit of CSS magic, performance improves and life is great.

Image Concatenation...WTF?

The basic idea is this: Change X number of image downloads to 1 image download by making 1 big image that contains the X images.

As an example, lets say we have these icons:

help settings maximize shade grow close

While those are pretty tiny in size, on page load the user must still deal with downloading 6 individual images. Instead, we can simply concatenate those images using whatever image software that suits your fancy. Like so:

icons

Displaying Concatenated Images

Now that we have our sweet concatenated image we can't simply drop that image into an image tag and call it good...that'll display all icons at once. So...how do we display it?

To display the concatenated image, you need to place that image as a background on an appropriately sized DOM element using CSS to adjust the background positioning to make the appropriate image display. Now, when I say a DOM element...it could really be anything as you can plop a background image on any element with CSS pretty easily:

CSS:
  1. .whatever{
  2.   background: url('/path/to/image.png');
  3. }

Even though you can use any element, only a few make sense. I've seen spans and divs used to accomplish this task, but styling those elements to work cross-browser is flaky at best and really churns my stomach. The element that his handled pretty universally is the img tag.

Yes...yes...I know, I just said don't place the concatenated image in an img tag. I still hold to that statement. You place the concatenated image as a background on the img tag. So what goes in the src of the img tag? Our old friend the 1 pixel by 1 pixel transparent gif, of course! This way, the image will still act as an image, can be styled like an image, and be super easy to update via CSS.

An Example

Lets say we have the following concatenated image: sidebar_icons that we want displayed in the sidebar with the appropriate links.

Lets set up the HTML first using transparent.gif (a transparent 1x1 pixel image) as the src of the image tags:

HTML:
  1. ...
  2. <ul id="sidebar">
  3.   <li><a href="index.html" title="Home"><img src="images/transparent.gif" class="icon icon-home" alt="Home"/>Home</a></li>
  4.   <li><a href="search.html" title="Search"><img src="images/transparent.gif" class="icon icon-search" alt="Search"/>Search</a></li>
  5.   <li><a href="bookmarks.html" title="Bookmarks"><img src="images/transparent.gif" class="icon icon-bookmarks" alt="Bookmarks"/>Bookmarks</a></li>
  6. </ul>
  7. ...

Note the class names on the images...those will be used in the CSS as such:

CSS:
  1. #sidebar img.icon{
  2.   background: url('/path/to/concatenated/image.gif') no-repeat;
  3.   height: 16px;
  4.   margin-right: 3px;
  5.   vertical-align: middle;
  6.   width: 16px;
  7. }
  8.  
  9. #sidebar img.icon-home{
  10.   background-position: 0px 0px;
  11. }
  12.  
  13.  
  14. #sidebar img.icon-search{
  15.   background-position: -16px 0px;
  16. }
  17.  
  18.  
  19. #sidebar img.icon-bookmarks{
  20.   background-position: -32px 0px;
  21. }

That's really all there is to it. The icon class allows us to set the same image background and dimensions for all icons in the sidebar. The individual classes allow us to change the image that actually shows through.

More CSS Magic

Since CSS is being used to display the appropriate image, you can do your typical property manipulation as normal (e.g. :hover, set transparencies, etc).

A little trick I picked up at Zimbra's presentation (the same presentation I learned the benefit of image concatenation) at The Ajax Experience last October was how to reduce the number of images used by representing inactive icons with CSS transparency rather than using individual images for inactive icons. Like so:

CSS:
  1. #sidebar img.inactive{
  2.   filter:alpha(opacity=50);
  3.   -moz-opacity: 0.5;
  4.   opacity: 0.5;
  5. }

If you wanted to make the icons partially transparent all the time but on mouse over make them 0% transparent, you can do so like so:

CSS:
  1. #sidebar img.icon{
  2.   filter:alpha(opacity=50);
  3.   -moz-opacity: 0.5;
  4.   opacity: 0.5;
  5. }
  6.  
  7. #sidebar img.icon:hover{
  8.   filter:alpha(opacity=100);
  9.   -moz-opacity:1.0;
  10.   opacity:1.0;
  11. }

Basically, whatever CSS magic you can weave can be done.

Additional Benefits

If you are a developer working in a painful development environment, the above methods are extremely valuable. Oh...and by painful I mean: any change to the base document structure requires a restart of the application. I've been doing a lot of development in an XSLT/Java environment recently and this method has saved me countless restarts :)

Now, if I decide an icon should change from a "house" to a "moon" (for some strange reason) I can make that change easily in CSS without needing to change the XSLT...thus no restart needed. Beautiful.

Heck...I use this CSS method for a number of non-concatenated images as well just so I can make general image changes with ease.

What Not To Do

When using the above image concatenation methods, it is important to note that concatenating similar images is a good plan. Avoid concatenating images of vastly different sizes because the wasted space on the concatenated image increases the amount of data being downloaded which can counter-act what we are attempting to reduce by concatenation.

Drawbacks

Update: This section was added a couple of hours after posting as per someone's suggestion.
Using this method can affect the printability of your page as background images assigned with CSS don't show in print too well :)

Conclusion

Image concatenation can be a beautiful thing when dealing with large numbers of icons/images. But, as with anything, weigh its benefit. Thus far, I'm pretty happy with the results!

Posted in Blog, Technology.

Tagged with , , , , , , , , , .


106 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. holiday says

    hi

    thanks your site

  2. klipler says

    thank

  3. +18 says

    thanksss

  4. oyun siteleri says

    thankssssssss

  5. 二宮沙樹 says

    Very nice article! Thanks for this!
    doumo_arigatou

  6. iyiegitim says

    thankss so much.

  7. chat says

    thanx for nice share

  8. xXKronosXx says

    Thanks a lot, you just made my day! I was searching for this information for a long time and finally I found it here! Very good job!

  9. mIRC says

    http://www.mirc.net.in thanx!

  10. Christine Rossiter says

    I would like to see if you know of any Luminis Developers currently looking for a contract. We do have a 12 month contract available with our direct client in Oklahoma.

    Essential Job Duties:
    -Create custom channels and other channel development, such as a “feedback channel”, “search” and “oZONE help.”
    -Help install Google analytics in the portal.
    -Cross-train internal staff on Luminis
    -Help with Load Balancer issues as well as any other system issues we are experiencing.

    Please feel free to forward my information to anyone who may be interested.
    Thank you,
    Christine Rossiter
    # 708-492-3449
    crossiter@thesheffieldgroup.com

  11. Graphic Designing says

    Nice information…

  12. DVD says

    read through these but the ones

  13. アダルトDVD says

    iho historickeho kontextu.

  14. graphic design colleges says

    Great post I think people often pass over images, but spending some time on image optimization can really increase site loading speeds by a lot!

  15. revizyon says

    [...] done. They’re not difficult enough to warrant to their own posts but still super useful. BorkWeb ? Faster Page Loads With Image ConcatenationDoes having a lot of small images on your web app increase latency? Can this trick really counter [...]

    With

  16. komedi videolari says

    ty dyde

  17. Andrew says

    Interesting idea, I may try this one out!

  18. 天川るる says

    Perfect !! Now i go and use it in my site !

1 2

Continuing the Discussion

  1. Faster Page Loads With Image Concatenation linked to this post on April 26, 2007

    [...] piece on Faster Page Loads With Image Concatenation goes into why and how: For complex web apps, the quantity and resulting latency of icons and [...]

  2. Faster Page Loads With Image Concatenation » Webdesign Archive linked to this post on April 26, 2007

    [...] to reduce latency in web pages using Image Concatenation! [go] Related resources Some web directories of royalty free imagesAn improved CSS shadow [...]

  3. Life is grand » Image concatenation and favicons linked to this post on April 26, 2007

    [...] Image concatenation has been around for awhile now but it is nice to have a fully explained article aimed at how to use the technique in web interfaces. [...]

  4. CommaDot.com » Blog Archive » Concatenation of Images linked to this post on April 26, 2007

    [...] article about using concatenation of images.  I have been doing this for a while.  For me, a major trick is to make sure that the images [...]

  5. What I accidently learnt about programming » Image Concatenation and best fit algorithms EQUALS web server performance linked to this post on April 26, 2007

    [...] you’re new here, you may want to subscribe to my RSS feed. Thanks for visiting!I was reading about Image Concatenation in CSS today. The concept allows for all the individual images on a website to be displayed from a [...]

  6. Faster Page Loads With Image Concatenation » D’ Technology Weblog: Technology News & Reviews linked to this post on April 26, 2007

    [...] View: Full post [...]

  7. links for 2007-04-27 at James A. Arconati linked to this post on April 26, 2007

    [...] BorkWeb » Faster Page Loads With Image Concatenation When designing web applications, icons and images are used to enhance the user experience, give visual cues, and simply look sexy. For complex web apps, the quantity and resulting latency of icons and images used can greatly impact page load times…and d (tags: images web-developer reference web-developer/tools graphics web-developer/css) About [...]

  8. Daily Links Posting » links for 2007-04-27 linked to this post on April 27, 2007

    [...] BorkWeb » Faster Page Loads With Image Concatenation (tags: tips css) [...]

  9. Lightening load times with image concatenation | antiarc.net linked to this post on April 27, 2007

    [...] has a good article about a relatively old and yet, underused image technique. It’s a good read, and certainly [...]

  10. Domester.NET Blog » Faster Page Loads With Image Concatenation linked to this post on April 27, 2007

    [...] read more | digg story [...]

  11. Devlounge | Friday Focus #28 linked to this post on April 27, 2007

    [...] Faster Page loads with Image Concatenation Web applications tend to use a lot of images to help a user navigate the interface, and more [...]

  12. Joe Levi: All that ‘blog are not lost… » Blog Archive » Faster Page Loads With Image Concatenation linked to this post on April 27, 2007

    [...] read more | digg story [...]

  13. Bildīšu bariņš « j. linked to this post on April 27, 2007

    [...] Šeit pastāstīts, kā uzlabot lapu ielādes laiku, lietojot un izmantojot attēlu savienošanu (image concatenation). [...]

  14. links for 2007-04-27 « toonz linked to this post on April 27, 2007

    [...] BorkWeb » Faster Page Loads With Image Concatenation (tags: css icons) [...]

  15. TechMount » Archive » Daily Friction #234 linked to this post on April 29, 2007

    [...] Faster page loads with image concatenation – When designing web applications, icons and images are used to enhance the user experience, give visual cues, and simply look sexy. For complex web apps, the quantity and resulting latency of icons and images used can greatly impact page load times…and developers, in most cases, generally try to reduce page load time with a sweet web app rather than increase it. [...]

  16. Faster Page Loads With Image Concatenation · Style Grind linked to this post on April 30, 2007

    [...] “When designing web applications, icons and images are used to enhance the user experience, give visual cues, and simply look sexy. For complex web apps, the quantity and resulting latency of icons and images used can greatly impact page load times…” [...]

  17. hugetto.homelinux.com » Best CSS tools of the month linked to this post on April 30, 2007

    [...] Faster Page Loads With Image Concatenation [...]

  18. Faster Page Loads With Image Concatenation at Network Solutions Articles - Utropicmedia linked to this post on May 2, 2007

    [...] based on thoughts he heard from Zimbra when they presented at The Ajax Experience. His piece on Faster Page Loads With Image Concatenation goes into why and how: For complex web apps, the quantity and resulting latency of icons and images [...]

  19. holotone.net linked to this post on May 3, 2007

    [...] BorkWeb » Faster Page Loads With Image Concatenation – To reduce latency in my apps, I use Image Concatenation – Coupled with a bit of CSS magic, performance improves and life is great. [...]

  20. Blogvaria » Faster page loads using CSS linked to this post on May 4, 2007

    [...] read more | digg story Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages. [...]

  21. Top 10: mis sitios favoritos de abril « dot press linked to this post on May 7, 2007

    [...] BorkWeb » Faster Page Loads With Image Concatenation [...]

  22. All in a days work… linked to this post on July 23, 2007

    [...] Faster Page Loads With Image Concatenation Change X nbr of img DLs to 1 img DL by making 1 big img that contains the X imgs. To display the concatenated img, you need to place that img as a bg on an appropriately sized DOM element using CSS to adjust the bg positioning to make the appropriate img Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages. [...]

  23. Learning CSS linked to this post on November 10, 2007

    [...] Image Concentration - Speed up your sites images with some fancy CSS trickery. [...]

  24. Best CSS tools of the 2008,HOT CSS TOOLS - DAYUP linked to this post on June 10, 2008

    [...] “With new technologies flooding the web,different tools are available with which a web designer can showcase his creativity and craft. But despite putting in the effort and sweat, numerous websites with fantastic creativity fail to get noticed. Also, the surge in designing is driving designers to dig for creativity, color schemes and layouts.” Faster Page Loads With Image Concatenation [...]

  25. Faster Page Loads With Image Concatenation linked to this post on August 30, 2008

    [...] read more | digg story [...]

  26. The Mystery Of CSS Sprites: Techniques, Tools And Tutorials 於囧 linked to this post on April 27, 2009

    [...] Faster Page Loads With Image Concatenation For complex web apps, the quantity and resulting latency of icons and images used can greatly affect page load times. And developers usually try to reduce, rather than increase, page load times for their sweet Web apps. [...]

  27. The Mystery Of CSS Sprites: Techniques, Tools And Tutorials linked to this post on April 27, 2009

    [...] Faster Page Loads With Image Concatenation For complex web apps, the quantity and resulting latency of icons and images used can greatly affect page load times. And developers usually try to reduce, rather than increase, page load times for their sweet Web apps. [...]

  28. Tagz | "BorkWeb » Faster Page Loads With Image Concatenation" | Comments linked to this post on May 16, 2009

    [...] [upmod] [downmod] BorkWeb » Faster Page Loads With Image Concatenation (borkweb.com) 0 points posted 10 months, 1 week ago by jeethu tags webdesign image [...]

  29. The Mystery Of CSS Sprites: Techniques, Tools And Tutorials | CSS | Smashing Magazine linked to this post on May 23, 2009

    [...] Faster Page Loads With Image Concatenation For complex web apps, the quantity and resulting latency of icons and images used can greatly affect page load times. And developers usually try to reduce, rather than increase, page load times for their sweet Web apps. [...]

  30. CSS Sprites: CSS图片合并技术详解 | 帕兰映像 linked to this post on September 6, 2009

    [...] Faster Page Loads With Image Concatenation 对于复杂的网站,减少图片请求数量可以减轻服务器负担,这是许多站长所希望看到的。 [...]

  31. ZODIAC » CSS Sprites(CSS图像拼合技术)教程、工具集合【转】 linked to this post on September 8, 2009

    [...] Faster Page Loads With Image Concatenation 对于复杂的网站,减少图片请求数量可以减轻服务器负担,这是许多站长所希望看到的。 [...]

  32. The Mystery Of CSS Sprites: Techniques, Tools And Tutorials | 9Tricks.Com - Tips - Tricks - Tutorials linked to this post on October 2, 2009

    [...] Faster Page Loads With Image Concatenation For complex web apps, the quantity and resulting latency of icons and images used can greatly affect page load times. And developers usually try to reduce, rather than increase, page load times for their sweet Web apps. [...]

  33. Sneller laden van webpagina’s met image concatenation | Mindbus linked to this post on November 10, 2009

    [...] Omdat deze knoppen allemaal kleine plaatjes zijn, wordt het laden van de pagina vertraagd doordat elk plaatje afzonderlijk geladen moet worden. Mathhew heeft een manier gevonden om alle plaatjes als één plaatje in te laden en dan met behulp van css steeds het goede deel te laten zien. Hij noemt dat image concatenation. [...]

  34. 解密CSS Sprites:技巧、工具和教程 linked to this post on November 22, 2009

    [...] Faster Page Loads With Image Concatenation 对于复杂的网站,减少图片请求数量可以减轻服务器负担,这是许多站长所希望看到的。 [...]

  35. Answer My Searches » Blog Archive » Mini Searches with Answers linked to this post on November 25, 2009

    [...] done. They’re not difficult enough to warrant to their own posts but still super useful. BorkWeb ? Faster Page Loads With Image ConcatenationDoes having a lot of small images on your web app increase latency? Can this trick really counter [...]

  36. 行骏-IT Player » Blog Archive » 解密CSS Sprites:技巧、工具和教程 linked to this post on December 25, 2009

    [...] Faster Page Loads With Image Concatenation 对于复杂的网站,减少图片请求数量可以减轻服务器负担,这是许多站长所希望看到的。 [...]



Some HTML is OK

or, reply to this post via trackback.