CSS Image Sprites

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 4 Examples
Performance

What You’ll Learn

CSS image sprites combine many small images into one file. You show individual icons with background-image and background-position — reducing HTTP requests and keeping UI graphics consistent.

01

Sprite sheet

One PNG file.

02

Position

Show a slice.

03

Size

Icon window.

04

Fewer requests

Faster loads.

05

Real sprite

Browser logos.

06

4 try-it

Live editors.

Introduction

CSS image sprites are a technique used to optimize web performance by combining multiple images into a single image file. This reduces the number of HTTP requests the browser makes, which can improve load times — especially on pages with many small icons.

Sprites are commonly used for icons, buttons, and other small graphics that appear repeatedly across a site. Instead of loading ten separate PNG files, you load one sprite and use CSS to reveal the correct icon.

What Are Image Sprites?

An image sprite is a single image file that contains multiple smaller images arranged in a grid or row. CSS controls which portion is visible by setting the element’s size and shifting the background with background-position.

Think of it like a window sliding over a larger photograph — the element’s width and height are the window size; background-position moves the photo behind the window.

💡
Beginner Tip

On this page we use the real /images/browser-image-sprite.png file (1774×887 px) with six browser logos. The same technique works for any icon set.

Why Use Image Sprites?

  • Reduced HTTP requests — One file download instead of dozens of tiny icon requests.
  • Improved performance — Fewer round trips can mean faster first paint on HTTP/1.1 connections.
  • Consistent design — All icons come from the same source file, keeping visual style aligned.
  • Cache efficiency — Browsers cache one sprite; every icon on the page benefits.

How to Create an Image Sprite

Combine individual images into one file using an image editor (Figma, Photoshop, GIMP) or an online sprite generator. Icons are usually aligned horizontally or vertically with consistent spacing.

Here is a simplified diagram of a horizontal sprite with three icons:

+--------------------+--------------------+--------------------+ | Icon 1 | Icon 2 | Icon 3 | +--------------------+--------------------+--------------------+

Our browser sprite arranges six logos in one row inside a 1774×887 pixel PNG:

Browser logo sprite sheet containing Chrome, Firefox, Safari, Edge, Internet Explorer, and Opera icons
The full sprite sheet — CSS reveals one logo at a time.

📝 Implementing Image Sprites in CSS

Use background-image for the sprite file, background-position to show the correct slice, and fixed width / height for the visible icon area.

sprite-base.css
/* Shared sprite setup */
.icon {
  --size: 48px;
  width: var(--size);
  height: var(--size);
  background-image: url("/images/browser-image-sprite.png");
  background-repeat: no-repeat;
  display: inline-block;
}

/* Chrome — shift background to logo center */
.icon-chrome {
  background-position: -4px -46px;
}

Key properties

PropertyRole in sprites
background-imageLoads the combined sprite file
background-positionShifts which part of the sprite is visible
background-sizeScales the sprite sheet (needed for responsive icons)
width / heightDefines the visible “window” size
background-repeatUsually no-repeat for icons

⚡ Quick Reference

StepAction
1. Create spriteCombine icons into one PNG (or SVG)
2. Base classSet background-image, size, no-repeat
3. Per-icon classOverride background-position only
4. Measure offsetsNote each icon’s x/y position in the sprite
5. Test in DevToolsTweak position values until the correct icon shows

🖼 Optimizing Image Sprites

When creating sprites, follow these practices:

  • Compress the sprite — Use tools like TinyPNG or Squoosh to shrink file size without visible quality loss.
  • Align images correctly — Consistent icon dimensions make background-position math predictable.
  • Choose the right format — PNG for icons with transparency; SVG for scalable single-file icons in modern projects.
  • Document coordinates — Keep a comment or map of each icon’s offset for maintainability.
  • Limit sprite size — Very large sprites can slow the initial download; split into themed sheets if needed.

👀 Live Preview

Three browser logos from the same sprite file — only background-position changes:

Chrome
Firefox
Safari

Examples Gallery

Four examples using /images/browser-image-sprite.png — from a single icon to a reusable sprite pattern for lists and toolbars.

🖼 Sprite Basics

Start with one icon, then show several from the same file.

Example 1 — Single Icon from Sprite

Display the Chrome logo by positioning the background over the sprite sheet.

sprite-single.css
.browser-icon {
  width: 48px;
  height: 48px;
  background: url("/images/browser-image-sprite.png") no-repeat -4px -46px;
  background-size: 288px 144px;
}
Try It Yourself

How It Works

The element is 48×48 px. Negative background-position values slide the large sprite left and up until the Chrome logo sits in the visible area.

Example 2 — Multiple Icons, One File

Share the same base styles; change only background-position per browser.

sprite-multi.css
.sprite { /* shared base */ }
.sprite-chrome  { background-position: /* Chrome offset */; }
.sprite-firefox { background-position: /* Firefox offset */; }
.sprite-safari  { background-position: /* Safari offset */; }
Try It Yourself

How It Works

One HTTP request loads all six browser logos. Each class shifts the background to a different coordinate.

🚀 Real-World UI

Navigation toolbars and lists — classic sprite use cases.

Example 3 — Navigation Icon Bar

A compact toolbar of sprite icons — the pattern used for old-school site navigation and social icon rows.

sprite-nav.html
<nav>
  <a href="#"><span class="nav-icon icon-chrome"></span></a>
  <a href="#"><span class="nav-icon icon-firefox"></span></a>
  <a href="#"><span class="nav-icon icon-edge"></span></a>
</nav>
Try It Yourself

How It Works

Empty <span> elements with fixed dimensions act as icon frames. Links wrap them for clickable navigation icons.

Example 4 — Reusable Sprite Pattern

Use CSS custom properties for coordinates — the maintainable approach used on this site’s browser compatibility sections.

sprite-pattern.css
.sprite--chrome { --cx: 172; --cy: 432; }
.sprite--firefox { --cx: 462; --cy: 445; }

.sprite--chrome, .sprite--firefox {
  background-position:
    calc(var(--size) / 2 - var(--cx) * var(--scale))
    calc(var(--size) / 2 - var(--cy) * var(--scale));
}
Try It Yourself

How It Works

Store each logo’s center coordinates as CSS variables. One formula calculates background-position for any icon size.

💬 Usage Tips

  • Use a base class — Share background-image, size, and repeat; override position per icon.
  • Measure in DevTools — Hover the sprite in the Styles panel to tweak offsets visually.
  • Match icon dimensions — Element width/height should equal the icon slice size.
  • Prefer SVG today — For new projects, inline SVG or icon fonts may be simpler than PNG sprites.
  • Cache the sprite — Set long cache headers; the sprite rarely changes.

⚠️ Common Pitfalls

  • Complex sprites — Huge sheets with hundreds of icons are hard to maintain and can slow initial load.
  • Overuse — Large hero images or rarely used graphics should stay as separate files.
  • Incorrect positioning — Wrong background-position shows the wrong slice of the sprite.
  • Forgetting background-size — When scaling icons, the sheet must scale proportionally too.
  • Missing alt text — Decorative sprite spans need aria-label or adjacent text for accessibility.

♿ Accessibility

  • Provide text alternatives — Use aria-label on icon-only links or include visible labels.
  • Do not rely on icons alone — Pair sprite icons with text in navigation when possible.
  • Sufficient touch targets — Make clickable sprite icons at least 44×44 px on mobile.
  • Focus styles — Links wrapping sprite icons still need visible :focus outlines.
  • High contrast — Ensure icons remain visible on all background colors.

🧠 How CSS Image Sprites Work

1

Combine images into one file

Designers export a single sprite sheet with every icon positioned at known coordinates.

Asset
2

Set background-image on element

A small empty element loads the full sprite as its background.

CSS
3

Shift with background-position

Negative offsets slide the sprite so only the desired icon appears in the element box.

Position
=

One request, many icons

Every icon on the page reuses the cached sprite file.

🖥 Browser Compatibility

CSS background sprites use background-image and background-position — supported in all browsers including IE 6+.

Baseline · Universal support

Background sprite technique

The sprite pattern itself works everywhere. This page demonstrates it with our browser logo sprite sheet.

100% CSS backgrounds
Google Chrome All versions
Full support
Mozilla Firefox All versions
Full support
Apple Safari All versions
Full support
Microsoft Edge All versions
Full support
Opera All modern versions
Full support
background-image + background-position 100% supported

Bottom line: CSS sprites are a universal, production-safe technique. Modern alternatives include SVG sprites and icon components, but background sprites remain fully supported.

🎉 Conclusion

CSS image sprites are a powerful technique for optimizing web performance by reducing HTTP requests. While HTTP/2 and SVG icons have reduced their necessity, sprites remain excellent for teaching background CSS and maintaining legacy icon sets.

By following best practices — compressing assets, documenting coordinates, and avoiding overly complex sheets — you can use sprites to enhance both speed and visual consistency.

💡 Best Practices

✅ Do

  • Use a shared base sprite class
  • Document icon coordinates
  • Compress PNG sprites
  • Add aria-label to icon-only elements
  • Consider SVG for new projects

❌ Don’t

  • Put huge images in sprites
  • Guess position values blindly
  • Forget background-size when scaling
  • Use sprites for one-off images
  • Skip focus styles on icon links

Key Takeaways

Knowledge Unlocked

Five things to remember about CSS image sprites

Combine, position, display — fewer requests, same icons.

5
Core concepts
pos 02

Position

Shift view.

CSS
wh 03

Size

Window.

Box
req 04

Fewer HTTP

Faster.

Perf
a11y 05

Labels

aria.

A11y

❓ Frequently Asked Questions

An image sprite is a single image file that contains multiple smaller images. CSS background-image and background-position show only the slice you need, so the browser downloads one file instead of many.
They reduced HTTP requests. Each separate image file used to require its own network round trip. Combining icons into one sprite improved load times on HTTP/1.1 sites.
Less critical with HTTP/2 multiplexing and SVG icons, but sprites still work well for repeated UI icons, legacy support, and teaching how background-position works.
background-image sets the sprite sheet, background-position shifts to the correct icon, background-size scales the sheet, and width/height define the visible window.
Incorrect background-position values. Even a few pixels off shows the wrong part of the image. Always match element size to the icon size in the sprite.

Practice in the Live Editor

Open the editor, adjust background-position, and slice icons from browser-image-sprite.png.

Try Example 1 →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful