A sprite sheet is a single image that contains many smaller images packed together: a set of icons, the flags of every country, the frames of a character's walking animation. Instead of loading forty separate files, a web page or game loads one, and then shows only the part it needs at any moment.
The technique dates back to early video games and became a standard web performance trick in the 2000s. Today its role has changed, and it is fair to ask whether you still need it. This guide explains how sprites work in CSS and in game engines, when they are still the right tool, and how to generate a sheet with the matching CSS and JSON.
How a CSS sprite works
Imagine a PNG that is 200 px wide, holding four 48 px icons in a row with 2 px of padding between them. To show just the second icon, you create an element exactly the size of one icon and use the sheet as its background, shifted left so the second icon lines up with the element's window:
.icon {
display: inline-block;
background-image: url("icon-sprite.png");
background-repeat: no-repeat;
}
.icon-search {
width: 48px;
height: 48px;
background-position: -52px 0;
}
The negative background-position moves the sheet so the right part shows through. Everything outside the element's box is hidden. Each icon gets a class with its own size and offset, and in HTML you write <span class="icon icon-search"></span>.
How game sprite sheets work
Game engines and animation libraries use the same idea, but instead of CSS they read a JSON atlas: a data file that lists each frame's name and its rectangle on the sheet:
"walk-01": { "frame": { "x": 2, "y": 2, "w": 64, "h": 64 } }
Engines and libraries such as Phaser, PixiJS and many others can load an atlas in this "hash" style, originally popularised by TexturePacker. The engine then draws frames by name, and animations flip through them quickly. Packing everything into one texture also helps the graphics card, which prefers fewer, larger textures.
Are CSS sprites still worth it?
Sprites were invented largely because each HTTP request used to be expensive. Modern HTTP/2 and HTTP/3 connections load many small files in parallel over one connection, which removes much of that benefit. At the same time, icons have largely moved to SVG and icon fonts, which scale to any size and can be recoloured with CSS.
Sprites still make sense when:
- You have many small raster images (flags, emoji-style images, game tiles, product swatches) that are always used together.
- You are building HTML5 or canvas games, where atlases are the standard format.
- You support email templates or constrained environments where fewer files are simpler.
- You want to guarantee that a set of hover-state images is already loaded, so there is no flicker on first hover.
For interface icons on a modern website, an inline SVG or an SVG sprite with <use> is usually a better choice. The SVG Optimizer helps keep those files lean.
Layout options
How images are arranged on the sheet affects size and usability:
| Layout | Description | Good for |
|---|---|---|
| Packed | Tallest images first, filled into rows to make a roughly square sheet | Mixed-size icons, smallest sheet |
| Grid | Equal cells sized to the largest image, images centred | Animation frames, predictable maths |
| Row | All images in one horizontal line | Simple animations, CSS steps() animations |
| Column | All images stacked vertically | Vertical scrolling strips |
Padding between images prevents neighbouring icons from bleeding in when the browser scales the page or when an engine filters textures. Two pixels is a sensible minimum.
Build a sprite sheet in your browser
The Sprite Sheet Generator packs up to 200 images on your device and writes the CSS and JSON for you.
- Open Sprite Sheet Generator and drop your icons or frames onto the page. PNGs with transparency work best.
- Check the file names: they become the CSS class and JSON frame names, so
search.pngbecomes.icon-search. Rename files first if you want different names. - Choose a Layout: Packed, Grid, Row or Column. For a grid, set the number of Columns.
- Set the Padding between images (0-32 px).
- Enter a CSS class prefix (the default is "icon").
- Review the CSS and JSON tabs below the preview; each has copy and download buttons.
- Click Download sprite PNG, or Download PNG + CSS + JSON (ZIP) to get everything in one file.
The preview draws a thin outline around each image and shows the total sheet size. If the sheet would exceed browser canvas limits, the tool warns you instead of failing silently.
Other ways to make a sprite sheet
Build tools
Front-end build pipelines have plugins that generate sprites automatically from a folder whenever you build the site. That is ideal for a large project where icons change often.
TexturePacker and game tools
Dedicated packers offer trimming of transparent edges, rotation of frames to save space, multiple output formats and multi-resolution export. For a commercial game with hundreds of frames, they are worth using.
By hand
A small sheet of four or five icons can be assembled in any image editor, with positions measured manually. It works, but the arithmetic gets error-prone quickly.
Tips and pitfalls
Retina and high-density screens
For sharp icons on high-density screens, make the sheet at twice the display size and set background-size to half the sheet's pixel size. All the offsets must then be halved too.
Keep related images together
A sprite containing images that are only used on one rarely visited page makes every other page download them. Group sprites by where they are used.
Optimise the PNG
Sprite sheets compress well because icons share colours. Run the final PNG through Image Compressor, or convert it with PNG to WebP if your target browsers support WebP.
Animation frames must be the same size
For character animations, make every frame the same size and use the grid or row layout, so each frame sits at a predictable position.
Frequently asked questions
What is a sprite sheet?
It is one image that contains many smaller images. Code then displays only the part needed, using CSS background positions on the web or a JSON atlas in games.
How does background-position work with sprites?
The sheet is set as an element's background and shifted with negative offsets so the wanted image lines up with the element's box. Everything outside the box stays hidden.
Are CSS sprites still used?
Less than before, because HTTP/2 and SVG icons remove most of their original benefits. They remain useful for many small raster images, games and some email templates.
What JSON format does the generator produce?
It produces a frames-and-meta atlas in the widely supported "hash" style, listing each frame's x, y, width and height, plus the sheet's file name and size.
How much padding should a sprite sheet have?
At least 1-2 px between images, to stop neighbouring images bleeding in when the page is zoomed or textures are filtered.