Book Image

Instant PageSpeed Optimization

By : Sanjeev Jaiswal
Book Image

Instant PageSpeed Optimization

By: Sanjeev Jaiswal

Overview of this book

PageSpeed is an important aspect of web design and site management. It is a Google measure of how well the site performs to technical measurements. PageSpeed can be measured using Google's own tool or a browser plugin. It is used to score sites in indices, and is important from a UI view as it forms a large part of the success of your site.Instant PageSpeed Optimization is a practical, hands-on guide that provides you with a number of clear, step-by-step exercises, which will help you to take advantage of the real power that is behind web optimization techniques, and give you a good grounding in using it in your websites.The book explores topics like HTML standards used for optimization, minifying scripts, and taking care of images, and solves the common errors that users do unknowingly. It will take you through a number of clear, practical recipes that will help you to take advantage of all the possible technologies to make your websitess efficient, fast, and accurate. You will not only learn basic standards to optimize your websites, but you will also learn some advanced level methods such as Apache handling, Flush methods, making AJAX cacheable, setting up browser caches, and reducing image size using CSS sprites. If you want to take advantage of all the necessary methods to make your website faster and efficient, then this book is a must-have.
Table of Contents (7 chapters)

Setting up CSS sprites (Advanced)


I have already shown how to minimize the file size by combining all images in the Minimizing HTTP requests (Simple) recipe. CSS sprites are a kind of advanced version of image maps that uses the CSS technique to handle different images for different purposes from the same big image file. Users usually think that combining all images into one would make the file's size much larger but it is not like that. It will either be the same or smaller than the sum of all individual file sizes.

Getting ready

This tutorial doesn't concentrate on how to make beautiful images and combining all images into one; so, I assume you have the combined image to play with CSS sprites. Almost all major websites (Yahoo!, Google, Amazon, IMBD, and so on) use this technique. If your website deals with a lot of images, you must consider using CSS sprites.

The PageSpeed plugin under Firefox suggests that we combine these images and use CSS sprites, as shown in following screenshot:

How to do it...

  1. You should consider wisely which images should be cached and how. You might have various .png and .gif images; try to combine them all in one. If you have smaller images like icons, try to combine them all into one because it would not just reduce the download size and file size but would reduce the number of HTTP requests.

  2. Think about images that are cacheable as discussed under the Adding an Expires or Cache-Control header (Simple) recipe. Cacheable images would not have to be downloaded again and again. We can achieve this goal either by doing all the mentioned steps manually by using image-editing tools, such as Adobe Photoshop or logo manager, or we can use online sprite-making tool.

  3. If you have made a combined image using Photoshop, before writing CSS code for it, you should find out the height and width of each image using ruler tool in Photoshop, so that you can use its dimension at CSS scripts to make it work properly.

  4. You may use MeasureIt (https://addons.mozilla.org/en-us/firefox/addon/measureit/), a Firefox add-on, to find out the exact height and width of the specified chunk of an image, as shown in following screenshot:

  5. Let say you have created an image that consists of three small images for each option for three different events in various colors, that is, Home, About, and Contact Us are in the color green at the beginning, but once you hover on any of these three options, say About, it would turn red and when you click on it, it would turn blue. These are not just text but the images which make your website very attractive.

  6. Let's assume, you have the following image, which is the combination of two different images and you need to write the CSS sprite code for it, so that it should only show one image at a time. At first, it should show the first button but after clicking on it, it should show the second button. Let's assume the height of each button is 50px and the width of each button is 300px and image name is sprites.png, which is at the same place where the CSS script is. This would look like the following screenshot:

    Here is the CSS code for it is:

    ul#button {
        height: 50px;
        list-style: none;
        margin: 0 auto;
        width: 300px;
    }
    
    ul#button li#buttonOne a {
        background: url("sprites.png") no-repeat scroll 0 -50px transparent;
        width: 300px;
    }
    
    ul#button li#buttonOne a:active {
        background-position: 0 -50px;
    }

    However, you don't have to go through all such things, such as writing CSS sprites codes, calculating the height and width of each button, and combining all images into one. SpriteMe (http://spriteme.org/) is an online tool that finds all the images from the web page and combines it into one, re-computes CSS background positions, and generate a CSS sprite for you, which you can use in your web page directly.

    Here is the suggested CSS sprite image and suggested CSS code for the packtpub.com home page:

  7. Click on make all to combine all the listed images and then click on export CSS to use the modified code. Don't forget to remove the comments and minify before using it on the production site.

There's more...

If you wish to make a combined image sprite using CSS, you should visit the csssprites page (http://csssprites.com/). If you are going to add or remove a few images time and again, you better have the image in the .psd format and play with it. It would save your time; it will be easy to maintain CSS codes for it too. After seeing the CSS code for image sprites, you would clearly notice that, it is best suited for icons or one-time used images on the web page. CSS sprites cannot be used if the image is repeated for more than one time on that web page. Use it wisely and yes, note that the Opera browser has problems with it.

Don't use images that use more than 256 color palettes and avoid .jpg images for image sprites. Always prefer .gif or .png having a transparent background, if possible.