A strongly graphical web page will usually feature most of its navigation as icons. There are several ways of creating those icons: looking for an appropriate Unicode symbol should be your first go-to, but if no symbol exists, you will usually have to create the icon in PhotoShop or another bitmap editor.
To add interactivity we have to swap the initial image with a second image, usually on mouse rollover. The simplest way to do that is to place the first image as a background, and on hover, swap it for a second. This technique is known as CSS Sprites.
Our first image, multimedia.png, looks like this:
This is our second image, multimedia-hover.png, with a glow effect:
(For a real website I would make the images significantly smaller and cropped far closer: here they are exaggerated for the purposes of illustration).
This is our basic HTML:
<ul id=nav>
<li><a href=# id=multimedia></a></li>
<li><a href=# id=storage></a></li>
</ul>
To this, we add this CSS:
ul#nav li { display: inline; }
a#multimedia { display: block; width: 171px; height: 123px;
background-image: url(multimedia.png); }
a#multimedia:hover { background-image: url(multimedia-hover.png); }
There are two conditions that must be met for CSS Sprites to work correctly:
Both images must be exactly the same size. Trying to swap images of different sizes will result in the apparent motion of the icons on mouseover, “jumping as they move between states. This takes careful design.
The
atag must be given aheightandwidththat exactly matches the size of the initial image. This means that theatag must be set todisplay: blockorinline-block, or be provided with enoughpaddingto see through its window to view the background image.
But wait! We can improve on this: if the window of the aelement is only large enough to see one of the image states, why don’t we merge the images together, and show just one half or the other usingbackground-position? By doing so, we load just one image, rather than two, and reduce the number of HTTP requests, which is one of the central techniques to speeding up the load time of a web page. We also eliminate the possibility of seeing a flash as the hover image is loaded for the first time.Our HTML is unchanged, but the CSS changes to this:
a#multimedia { display: block; width: 171px; height: 123px; background-image: url(multimedia.png); }a#multimedia:hover { background-position: bottom left; }
JQuery UI CSS Sprite icon panel You can take this much further and create an entire panel of icons in a single image, shifting it around to show different icons for different links using
background-position, significantly saving load time.CSS Sprites are a useful technique, but have several disadvantages that tend to keep me from using them in sites:
If the images don’t load for any reason, your users are lost. Unlike real images that use an
<img>tag, background images do not have a text alternative. If they don’t load, nothing shows.Lack of
alttext reduces semantics and SEO. Search engines will still follow the link, but they won’t be able to able to understand its context, as there is no text within the link itself.Changes to the design can mean a lot of rework to an icon panel. Especially if the icons change in size (although this could be mitigated by using
background-size).
Pro CSS3 Animation, Apress, 2013