John M. Astell ♦ Portfolio

CSS Sprites

This website uses CSS sprites for the logos that appear on the top left of each page. There are several different logos, each with a different picture. Rather than having each logo in a separate file, all seven are in a single file. Each page's HTML has a container for the logo, and CSS is used to display the correct logo (the "sprite") for the page. The benefits for this approach are:

Here's the image in the logo sprite file:
Logos Sprite
(The red border around the image is added with CSS to show the bounds of the image. It is not part of the image.)

The dimensions of each logo are 305 pixels by 110 pixels. A one pixel line of whitespace separates the logos, so the overall image is 305x776. This separator isn't necessary for CSS sprites but it makes the master image more human readable and thus more maintainable over time.

The logo container in the page's HTML is the same height as a logo, and CSS is used to display the overall image as a background image in the container. By correctly positioning the overall image in the container, the desired logo appears in the container while the rest of the image is outside the container and is not seen. The upper left corner of the overall image is the origin (x=0,y=0). So, displaying a desired logo just requires specifying its origin: the top logo is x=0,y=0 while the bottom logo is x=0,y=-666.

Finally, the HTML container for the image is actually wider than the width of a logo (in case I create an extra-wide logo someday). CSS will by default repeat the background image to fill the available space. I don't want this, so I specify no-repeat. Here's the CSS resulting CSS:

div#logo {
  background-image: url('media/logos/jma_logos_sprite.png');
  background-position: 0px -555px;
  background-repeat: no-repeat;
}

And this is the logo that is displayed:

In the above box, the background color initially is set to gold to match the logo's base color, and the background repeat initially is set to be non-repeating. (The background color is set in the CSS but not in div#logo, in case you're wondering why it's not in the code listing above.) Press the Toggle Background Color button to toggle it between gold and white. Press the Toggle Repeat On/Off to see how repeat works.

There are ways to further reduce the file size of the image for this demo. The text part of each logo is always the same and in the same position. It is possible to construct each logo from two images: an icon image and a text image. That way, the text would only have to be present once in the master image. Further, since each logo is on the same gold color, the white separator and some gold edging could be deleted, so that the gold edge area on the bottom of one logo could also be used as the gold edge area on the top of the next logo. That is, rather than the top logo starting at x=0,y=0 and the second logo at x=0,y=-111, the second logo could start at perhaps x=0,y=-100. Note, however, that this will make the master image harder to maintain over time, as each logo would no longer be evident at a glance.

CSS3 Demos

These demos show examples of the utility of CSS3. The CSS3 for the demos and the JavaScript/jQuery code that manipulates the CSS is in the page's HTML file (rather than in separately loaded files), so that you can just look at the page's source to see everything of importance.

This table is used to test if your browser supports certain CSS3 pseudo-classes and pseudo-elements. It should be hidden.