How to Improve Website Performance Using CSS Sprites
CSS Sprites is a technique by which we can combine multiple images in a single big image and position the various parts of this big image with our elements using pure CSS attributes. The primary objective behind using CSS Sprites is to decrease the number of HTTP requests and thus decreasing the request stress on server. The idea behind CSS Sprites is to consolidate multiple images into one sprite and then selectively display portions of this sprite with positioning. It starts by creating a larger CSS Sprite by grouping more than one images (usually icons) together and aligning them in proper grid style to aid the positioning. Then it is aligning used a proper positioning values on the webpages to show their respective images. This way it decreases the number of HTTP requests and thus increases the overall speed of webpages.
Sometimes back, we used the favourite slicing technique to break down a larger image into smaller slices (parts) so as to make pages load faster, but all this technique was only fooling our eyes as the page looked like loading faster, while in reality it was not. Each of those images was fed up as a separate request to the server and would increase the time of loading actually.
According to Yahoo UI blog, the resultant sprite created by joining smaller images is always smaller in size than the combined contributed parts. It is because we have to use a single color table for the sprite instead of individual color tables for the contributed ones. Another advantage of using sprites is that this way we save the overhead required by the images.
Reduce File Overhead using CSS Sprites
Imagine a site that has 10 sliced buttons for the main navigation and each button has an accompanying graphic for the hover state. A total of 20 files would have to be downloaded just for the navigation. That’s 19 files of unnecessary file overhead. If there were 10,000 fresh visits to the web site 200,000 downloads would potentially occur. I say potentially because depending on how the web page was created the hover state buttons may require a “hover” to occur before they are downloaded. Either way at least 100,000 downloads would be required, reducing the number of available connections to the server and littering the pipe with unnecessary file overhead. If a CSS Sprite were used only 10,000 downloads would be required and considerably less bandwidth would’ve been used, allowing the site to make the most of the bandwidth allotted by their hosting provider.Reduce Connection Overhead using CSS Sprites
When you visit a web site an HTTP connection is established between you and the web server. Once connected, your web browser requests the document you asked for (an HTML home page, perhaps) and after receiving the file it is read to determine what else needs to be downloaded and how to visually render the page. How CSS Sprites Help in Connection Overhead Notice in the CSS below how the anchor tag does not get a background-image, but each individual class does.
#nav li a {background:none no-repeat left center}
#nav li a.item1 {background-image:url('../img/image1.gif')}
#nav li a:hover.item1 {background-image:url('../img/image1_over.gif')}
#nav li a.item2 {background-image:url('../img/image2.gif')}
#nav li a:hover.item2 {background-image:url('../img/image2_over.gif')}
#nav li a {background-image:url('../img/image_nav.gif')}
#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;}