01What is Sprite CSS Generator?
A sprite sheet packs many small icons into one image, and CSS shows one icon at a time by sliding the image around behind a fixed-size box. This tool writes that CSS for a sheet you already have. It does not build or pack the image. You tell it where each icon sits, and it does the background-position arithmetic. Start with the Sprite Image URL and a CSS Class Prefix. Then, for each icon, give a name, its X and Y offset in pixels from the sheet's top-left corner, and its width and height. The output begins with one shared rule for the prefix class. That rule sets background-image to your sheet, background-repeat to no-repeat and display to inline-block. After it comes one rule per icon with a width, a height and a background-position. The position is the negative of the icon's coordinates, because the sheet has to move left and up to bring that icon into the box. This follows the background-position property in the W3C CSS Backgrounds and Borders spec. A zero offset is written as a bare 0. Here is a quick example. Load the Grid Layout preset with four 24px icons in a 2 by 2 grid. The stop icon sits at x 0, y 24. Its rule reads width 24px, height 24px and background-position: 0 -24px. Forward sits at 24, 24 and gets -24px -24px. Names are cleaned so selectors stay valid. In icon names, anything that isn't a letter, digit or hyphen becomes a hyphen, and the result is lowercased. The prefix is cleaned too, but it keeps its case and underscores. The URL is wrapped in double quotes, so spaces in a path are safe. A second panel shows the matching HTML, one span per icon carrying both classes. Each panel has its own copy button. There are limits to keep in mind. The tool never loads your image, so it can't tell you if a coordinate is off by a pixel. It doesn't add background-size, so Retina sheets need that line added by hand. Everything is string building in the page, and your sheet URL is never fetched.
Front-end developers maintaining a site that already ships a PNG sprite sheet and need correct background-position rules for each icon. The tool runs entirely in your browser, so your data never leaves your device.
02How to use Sprite CSS Generator
- Enter the Sprite Image URL and a CSS Class Prefix.
- For each icon, type a name and set X, Y, W and H in pixels.
- Click Add Icon for another entry, or the trash icon to remove one.
- Copy the Generated CSS and the HTML Usage snippet with the copy icon on each panel.
- Use a preset or Reset to start again from a known layout.
03Worked examples
- Input:
Sprite URL sprites.png, prefix icon; icon stop, x 0, y 24, 24x24 - Result:
.icon-stop { width: 24px; height: 24px; background-position: 0 -24px; } HTML: <span class="icon icon-stop"></span> - The position is the negative of x and y. A zero offset is written as 0 without px.
- Input:
Prefix icon; icon forward, x 24, y 24, 24x24 - Result:
.icon-forward { width: 24px; height: 24px; background-position: -24px -24px; } - From the Grid Layout preset, bottom-right icon of the 2 by 2 grid.
- Input:
Sprite URL sprites.png, prefix icon (shared rule) - Result:
.icon { background-image: url("sprites.png"); background-repeat: no-repeat; display: inline-block; } - One shared rule comes before the per-icon rules.
- Input:
Sprite URL img/ui sprite.png, prefix "my icons"; icon "Arrow Left!", x 48, y 16, 16x16 - Result:
.my-icons { background-image: url("img/ui sprite.png"); ... } .my-icons-arrow-left- { width: 16px; height: 16px; background-position: -48px -16px; } - Trimmed. Spaces and punctuation become hyphens and the icon name is lowercased. The quoted URL keeps the space safe.
- Input:
Prefix UI_Icon; icon Arrow_Up, x 0, y 0, 16x16 - Result:
.UI_Icon-arrow-up { width: 16px; height: 16px; background-position: 0 0; } - The prefix keeps case and underscores, while the icon name does not.
04Common use cases
- Writing classes for an icon sprite exported from a design tool along with a list of coordinates
- Adding a new icon to a legacy PNG sprite on an older site without redoing the old rules by hand
- Getting one class per frame from a game or animation sprite sheet laid out on a grid
- Double-checking the sign and order of background-position values for an icon at a given offset
- Producing matching span markup for a style guide page that lists every icon
05Tips
- Leave a pixel or two of transparent padding between icons on the sheet. At fractional zoom levels browsers can sample neighbouring pixels and draw thin lines at the icon's edge.
- For a 2x sheet, keep coordinates in CSS pixels and add background-size set to half the image's pixel size on the shared class.
- Icon names lose underscores and case, but the prefix keeps them. A prefix UI_Icon with an icon Arrow_Up gives .UI_Icon-arrow-up, so match that in your markup.
- Give every icon a name. An empty name falls back to an internal id such as 2 or new-4, which makes for an unreadable class.
06Related concepts
CSS sprites, background-position, HTTP/2, Image optimization.
Frequently Asked Questions
What is Sprite CSS Generator?
Sprite CSS Generator is a free, browser-based developer tool on AIDToolStack. Generate CSS sprite sheets with background-position declarations. It requires no installation or sign-up and works entirely in your browser.
Is Sprite CSS Generator free to use?
Yes. No sign-up, no fees and no usage cap.
Does Sprite CSS Generator store my data?
No. It runs in your browser; nothing is uploaded.