{ Gearing your tech needs }

How to improve your web performance using CSS-Sprites

by harshit on July 3, 2009 · 10 comments

CSS sprites is a technique to combine unlimited number of images into one to improve the performance of your web page.Have you ever seen the CSS technique where the “on” and “off” states of a button are contained within the same image and are activated by shifting the background-position? Think of CSS Sprites as an extension of that technique.With millions of users Google,Yahoo!,MySpace,YouTube and AOL do everything they can to improve the user experience. As all of them use CSS sprites to save numerous HTTP requests for their intricate interfaces.

What does css sprites does…

Group multiple images together (usually icons or decorative images) into one sprite
Evenly space these images, aligned into one or more lines
Set this sprite to the background image of an element (usually a list)
Position the sprite to display the appropriate image by shifting the X or Y position by a multiple of the spacing
Enjoy the increased speed and reduced HTTP requests

Create your own CSS Sprite…
Step1-Create an image (or collect it from anywhere)using photshop,fireworks,gimp etc.. depends on your choice.
sprite1
#nav li a.item1 {background-image:url('../img/image1.jpg')}
#nav li a:hover.item1 {background-image:url('../img/image1_over.jpg')}
#nav li a.item2 {background-image:url('../img/image2.jpg')}
#nav li a:hover.item2 {background-image:url('../img/image2_over.jpg')}
#nav li a.item3 {background-image:url('../img/image3.jpg')}
#nav li a:hover.item3 {background-image:url('../img/image3_over.jpg')}
#nav li a.item4 {background-image:url('../img/image4.jpg')}
#nav li a:hover.item4 {background-image:url('../img/image4_over.jpg')}
#nav li a.item5 {background-image:url('../img/image5.jpg')}
#nav li a:hover.item5 {background-image:url('../img/image5_over.jpg')}

As above image displays that img1,img1_over,img2,img2_over
it means that when background position changes image display changes
By using CSS Sprites we are able to lighten up this interface.We may use one single image instead of 6 images.We can combine them into one big image.Create a new image that is as wide as your widest image and and as tall as the combined height of all your images plus X pixels, where X is the number of images you have. Now place you images into this new image, left aligned, one on top of the other with one pixel of white space in between.sprite2
Now look at css after combining the images into one
#nav li a.item1 {background-position:0px 0px;}
#nav li a:hover.item1 {background-position:0px -72px;}
#nav li a.item2 {background-position:0px -143px;}
#nav li a:hover.item2 {background-position:0px -215px;}
#nav li a.item3 {background-position:0px -287px;}
#nav li a:hover.item3 {background-position:0px -359px;}
#nav li a.item4 {background-position:0px -431px;}
#nav li a:hover.item4 {background-position:0px -503px;}
#nav li a.item5 {background-position:0px -575px;}
#nav li a:hover.item5 {background-position:0px -647px;}

There is a single background-image applied to the anchor element itself, and the unique classes merely shift the background position with negative Y coordinates.

During this the HTTP Request time reduces and size of images reduces.This is done for only 6 images ,and if you are usimg it in your website then you will be able to find some dynamic changes.

CSS  Sprite Generator

Hey Bro, why not share this post :
  • StumbleUpon
  • Digg
  • Reddit
  • Technorati
  • Yahoo! Buzz
  • del.icio.us
  • Facebook
  • HackerNews
  • Twitter

Related posts:

  1. How to customize the Thesis Header in few easy steps Header is quite sensitive area of any layout as...

Subscribe Now

If you enjoyed this post, you will definitely enjoy our others. Subscribe to the feed to get instantly updated for those awesome posts soon to come.

Free Subscription

2 Tweets 7 Other Comments

{ 1 trackback }

How to optimize website loading time and performance
July 5, 2009 at 2:02 am

{ 9 comments… read them below or add one }

1 Harsh Agrawal July 5, 2009 at 11:38 pm

Hey Honey Very helpful post and worth sharing. Though I think I need to digg more into the detail of every step you explained. Earlier I used to think that plugin is the best way to optimize the blog, but I was wrong.. I like the idea of hosting images on sub domains..

This comment was originally posted on honeytech

2 Harsh Agrawal July 5, 2009 at 11:40 pm

Hey harshit
very useful tip. I think you are using the same trick on your blog. I have to implement it. Meanwhile I’m accessing your blog via mobile Internet connection and your load time is fast :) Good work mate…

3 harshit July 5, 2009 at 11:47 pm

@Harsh
yeah right nw i m wrking on it and found it very useful.
I think u got it very well……

4 Honey Singh July 6, 2009 at 3:48 am

@Harsh
Thanks.
Due to page length and time scope, i escaped few things in this posts and will include in upcoming posts.

This comment was originally posted on honeytech

5 Harsh Agrawal July 12, 2009 at 3:25 am

@Honey
Will be waiting eagerly for your upcoming post on wordpress optimization series.

This comment was originally posted on honeytech

6 Harsh Agrawal July 12, 2009 at 3:26 am

Meanwhile could you please enable the subscribe to comment feature, I want to subscribe to all the comments on this post, but I can’t find it.
Thanks ..

This comment was originally posted on honeytech

7 pligg.com July 15, 2009 at 11:07 pm

How to decrease website loading time and improve performance…

Internet users today are quite restless and rarely wait if your website is very heavy to load in their normal internet speed. Web site optimization is an important part of your ambition that decides the productivity of the websites. So avoid frustratin…

This comment was originally posted on honeytech

8 cogizz.com July 22, 2009 at 10:14 pm

Interesting ideas indeed! I was actually thinking about comments last night and this morning

This comment was originally posted on honeytech

9 inpencilmusic.com August 18, 2009 at 4:26 pm

Does shifting the CSS / Image files into a subfolder then require you to hardcode all those images to that folder? How else will the browser know where to look?

(new to this thanks)

This comment was originally posted on honeytech

Leave a Comment

Additional comments powered by BackType

Previous post:

Next post: