CSS Image-Sprites

In-a-nutshell, CSS image-sprites are a way of showing many different images to a viewer of your Webpage—via a single actual image as a resource. While the idea of using CSS to create these sprites is somewhat new, the image-sprite concept has been around for quite awhile.

The image-sprite was originally invented as a method of quickly compositing several images together in two-dimensional video games using special hardware. However, as computer performance improved, this optimization became unnecessary, and the term evolved to refer specifically to the two dimensional images themselves that were integrated into a scene.[1]

CSS image-sprites borrow from the original concept. However, the main reason for using them is to reduce the number of HTTP requests for images referenced by a Webpage. Here's one process for creating an image-sprite in a nutshell:

  • Combine multiple images into a single larger image, with each sub-image at a specific grid-coordinate.
  • Use the single, combined-image to give a series of A elements a base background-image.
  • Then specify background-positions for each of the A elements via the pseudo-style, a:hover.

Accessible Image-Sprites Example

The many CSS image-sprite examples I've found on the Internet do not appear to be very user-friendly with respect to Web Accessibilty guidelines. Sure, they all seemed to please search engines by adding text within the requisite links. However, several of the examples set the display-property of the link-text containers to display: none. This might cause issues for those who choose to reconfigure browser image and/or color settings to make Webpages easier for them to read.

The following CSS image-sprite example allows for the link-text to stay visible, no matter what settings the user might have changed with repect to showing inline and/or background images. Even when images and/or colors are turned-off, these hyperlinks will still appear, and their functionality remain intact.

First, examine a use the above 5-button toolbar. Then, if you are using Internet Explorer *, open Tools/Internet Options and click on the 'Accessibility' button. Then check the 'Ignor Colors' checkbox. Click OK. Doing this will in effect, turn-off images on your Webpages. Notice that even after you do this, the above image-sprite example is still quite functional.

If you happen to be using Firefox *, open Tools/Options and click on the 'Content' tab, and click again on the 'Colors' button. Then uncheck the 'Allow pages to choose their own colors' checkbox. Click OK. Doing this will, similarly, turn-off images on your Webpages.


 

The Source-Code

Here are the key pieces of code that I used in order to build the above example via a single background-image.

XHTML ~
<div class="toolbar-buttons">
    <div id="homepage">
	    <a href="/page/show/default.htm" title="The Workingman's Web Homepage"><span>Home</span></a>
    </div>
    <div id="resume">
	    <a href="/page/show/andy-gillis-resume.htm" 
	    title="Andy Gillis' Resumé"><span>Resumé</span></a>
    </div>
    <div id="design">
	    <a href="/page/show/design.htm" 
	    title="Art, Design & Web Design by Andy Gillis"><span>Design</span></a>
    </div>
    <div id="development">
	    <a href="/page/show/development.htm" 
	    title="Web Development & Code Written by Andy Gillis"><span>Development</span></a>
    </div>
    <div id="contact">
	    <a href="mailto:agillis@att.net"><span>Contact</span></a>
    </div>
</div>
			        
CSS ~
<style type="text/css">

div.toolbar-buttons {
    position: relative;
    height: 80px;
    width: 500px;
    background-image: url(../images/buttons.jpg);
    background-repeat: no-repeat;
    border: 1px solid #333;
    border-left: 0;
    clear: both
}

div.toolbar-buttons div {
    float: left;
    position: relative;
    text-align: center;
    font: bold .92em verdana;
    clear: none
}

div.toolbar-buttons a span {
    position: relative;
    display: block;
    width: 100%;
    margin-top: 57px;
    padding-top: 3px;
    border-top: 1px solid #333;
}

div.toolbar-buttons a:link,
div.toolbar-buttons a:visited,
div.toolbar-buttons a:hover,
div.toolbar-buttons a:active,
div.toolbar-buttons a:focus {
    display: block;
    height: 80px;
    width: 99px;
    overflow: hidden;
    cursor: pointer;
    border-left: 1px solid #333;
    color: #000;
    text-decoration: none
}

div.toolbar-buttons a:hover {
    color: #1133aa;
    background-image: url(../images/buttons.jpg);
    background-repeat: no-repeat;
}

div#homepage a:hover {
    background-position: -1px -80px;
}

div#resume a:hover {
    background-position: -101px -80px;
}

div#design a:hover {
    background-position: -201px -80px;
}

div#development a:hover {
    background-position: -301px -80px;
}

div#contact a:hover {
    background-position: -401px -80px;
}

</style>
		            

Sources:
[1] Wikipedia: Computer Sprites