CHAPTER 35

CSS Image Sprites

Learn the concept of CSS image sprites and how background positioning can display different parts of one image.

01. What is CSS Image Sprites?

An image sprite combines multiple small images into a single image file.
CSS background-position can display a specific section of the sprite.
Sprites were commonly used to reduce multiple image requests in older web development.

02. Syntax

.icon { background-image: url("sprite.png"); background-position: x y; width: value; height: value; }

03. Example

CSS / HTML Example AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>

<style>

.icon {

    width: 50px;

    height: 50px;

    background-image:
        url("sprite.png");

    background-repeat: no-repeat;

}

.icon-home {

    background-position: 0 0;

}

</style>

</head>

<body>

<div class="icon icon-home"></div>

</body>
</html>

04. Important Points

✓ Sprites use one image containing multiple graphics.
✓ background-position selects the required area.
✓ The container dimensions determine how much of the image is visible.
✓ Modern websites often use SVG icons and icon systems instead.

05. Common Mistakes

⚠ Using incorrect background positions.
⚠ Using sprites when SVG or modern icon systems are more appropriate.
⚠ Forgetting background-repeat: no-repeat.

06. Real World Use

Image sprites can be found in legacy interfaces, icon collections and older performance-focused websites.

Ready to Practice? 🚀

Open the AIFA CSS Editor and practice this chapter live.

Try It Yourself →
Practice Exercise

Practice Background Position

Create a small icon container and practice changing its background-position.