Easy Tutorial
❮ Css Mediatypes Css Rwd Grid ❯

CSS Image Sprites


Image Sprites

Image sprites are collections of images in a single file.

Web pages with many images can take a long time to load and generate multiple server requests.

Using image sprites reduces the number of server requests and saves bandwidth.


Image Sprites - Simple Example

Instead of using three separate images, we can use this single image ("img_navsprites.gif"):

With CSS, we can display only the part of the image we need.

In the following example, CSS specifies the part of the image "img_navsprites.gif" to display:

Example

Example Explanation:

This is the simplest way to use image sprites. Now, let's use links and hover effects.


Image Sprites - Creating a Navigation List

We want to use the sprites image ("img_navsprites.gif") to create a navigation list.

We will use an HTML list because it can be linked and also supports background images:

Example

Example Explanation:

Now, let's position and style each specific part:


Image Sprites - Hover Effect

Now, we want to add a hover effect to our navigation list.

| | :hover selector is used for displaying effects when hovering over elements. Tip: The :hover selector can be used on all elements. | | --- | --- |

Our new image ("img_navsprites_hover.gif") contains three navigation images and three hover images:

Since this is a single image instead of 6 separate image files, there will be no delay in loading when users hover over the images.

We add the hover effect with just three lines of code:

Example

Example Explanation:

❮ Css Mediatypes Css Rwd Grid ❯