Using CSS Sprites

How to Use Dynamic CSS Content to Improve Your Web Pages

© Nicholas Anderegg

Oct 3, 2009
CSS from a Style Sheet Using CSS Sprites, Nick Anderegg
CSS Sprites are a great tool that can be utilized by almost any website to improve appearance, loading time, and efficiency.

CSS Sprites are a common tool that web designers can use to improve their sites. They can be implemented very easily and, once they are on the site, they will improve the sites appearance, drastically reduce the loading time of the page, and improve the site's usability. This CSS trick is a must-have for any modern web designer.

How do CSS Sprites work?

CSS sprites work by splicing together multiple images (like different states of a navigation button) in one larger image and then displaying different sections of the image at the necessary times. This technique is most commonly used to display changing images like navigation buttons and images that utilize rollovers.

This technique, which uses simple CSS scripting, is much more efficient than the old way of doing things, which used multiple images and JavaScript to control which one was displayed. In the style sheet, the element is given various :hover and :active properties, similar to a link. The properties define which part of the image is displayed when a site user hovers or clicks on the image.

Preparing Images for CSS Sprites

Preparing the images that are needed for a CSS Sprite is a relatively simple task. This can be accomplished by taking the various states of of the image and placing them together in one file. Using GIMP or Photoshop, a new file should be created that is the width of all the states combined. The various states should then be placed into the image side-by-side, in the order that they will be retrieved (i.e. normal, hover, active).

One very important thing to keep in mind is that each state should be the same width. If the normal state is one width and the hover and active states are another, it will produce some unwanted results, like the the :hover state showing half of the normal state and half of the hover state.

Writing the CSS

Once the master images have been created, they are ready to be added to the page. One thing to remember is that, although these are images that are being added to the page, traditional <img></img> tags should not be used. Instead, where the image is to be placed, put a <div></div> tag with the id set as the name of the element. For example, the tag for a CSS sprite for a home navigation button might be <div id=”homelink”>&nbsp;</div>. This <div></div> can then be positioned just like any other page element.

The style sheet should contain the values for the three different states of each image. Typically, the CSS for any sprite will look like this:

#homelink{

background: url('images/homesprite.png') 0 0 no-repeat;

width: 100px;

height: 50px;

position: absolute;

left:20px;

top: 7px;

}

#homelink:hover {

background-position: -100px;

}

#homelink:active {

background-position: -200px;

}

The width of the width and height should be set to the width and height of each individual state in the master image, and it should only be set in the normal state, ensuring that all states show up consistently. In the :hover and :active states, the background position should be changed to reflected the change in position of the state. In the above example, each state is 100 pixels width, therefore, the :hover state moves part of the image that is shown 100 pixels to the left, and the :active state 200 pixels. For images containing states of a different size, the background position should be changed by the width of the states.

Practical Applications of CSS Sprites

The list of things that can be done with CSS sprites is endless. They can be used for navigation buttons, dynamic effects, or even just reducing the number of images that is loaded on a website. When using them as navigation buttons, they can be turned into links simply by placing <a></a> tags around the containing <div></div> tag.

When this technique is being used to place all the images on a site into one master image, be sure to change not only the background-position property of each image, but also the width and height properties, unless of course, all the images in the master image are the same width and height.


The copyright of the article Using CSS Sprites in Website Design is owned by Nicholas Anderegg. Permission to republish Using CSS Sprites in print or online must be granted by the author in writing.


CSS from a Style Sheet Using CSS Sprites, Nick Anderegg
A CSS Sprite Master Image from a Navigation Bar, Nick Anderegg
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Comments
Oct 9, 2009 3:07 PM
Guest :
how about the menus for this site? is this created using css as well or it has a combination script that is added ?

is there any referrence site for that kind of menus ?

thanks
Oct 9, 2009 3:17 PM
Nicholas Anderegg :
This site does, in fact, use a similar system for the menus. As you can see here: http://graphics.suite101.com/nav_open.gif, this site uses a two state sprite to control the background images of the major menu items.

Another example of very complex CSS Sprites can be found at: http://static.ak.fbcdn.net/rsrc.php/z7F5Y/hash/dyqxyvrx.png This image is the master for Facebook's chat system and it has many, many separate pieces.
2 Comments