---
title: Spriting with Compass
layout: tutorial
crumb: Spriting
classnames:
- tutorial
---
# Spriting with Compass
Spriting has never been easier with Compass. You place the sprite images to be in a folder,
import them into your stylesheet, and then you can use the sprite in your selectors in one
of several convenient ways.
## Setup
For this tutorial, let's imagine that in your project's image folder there are four icons:
* `public/images/icon/new.png`
* `public/images/icon/edit.png`
* `public/images/icon/save.png`
* `public/images/icon/delete.png`
Each is an icon that is 32px square.
## Basic Usage
The simplest way to use these icon sprites is to let compass give you a class for each sprite:
@import "icon/*.png";
@include all-icon-sprites;
And you'll get the following CSS output:
.icon-sprite,
.icon-delete,
.icon-edit,
.icon-new,
.icon-save { background: url('/images/icon-34fe0604ab.png') no-repeat; }
.icon-delete { background-position: 0 0; }
.icon-edit { background-position: 0 -32px; }
.icon-new { background-position: 0 -64px; }
.icon-save { background-position: 0 -96px; }
You can now apply the `icon-XXX` classes to your markup as needed.
Let's go over what happened there. The import statement told compass to [generate a
stylesheet that is customized for your sprites](https://gist.github.com/729507). This
stylesheet is [magic](#magic-imports), it is not written to disk, and it can be customized
by setting configuration variables before you import it. See the section below on
[Customization Options](#customization-options). The goal of this stylesheet is to provide a
simple naming convention for your sprites so that you they are easy to remember and use. You
should never have to care what the is name of the generated sprite map, nor where a sprite
is located within it.
## Selector Control
If you want control over what selectors are generated, it is easy to do. In this example,
this is done by using the magic `icon-sprite` mixin. Note that the mixin's name is dependent
on the name of the folder in which you've placed your icons.
@import "icon/*.png";
.actions {
.new { @include icon-sprite(new); }
.edit { @include icon-sprite(edit); }
.save { @include icon-sprite(save); }
.delete { @include icon-sprite(delete); }
}
And your stylesheet will compile to:
.icon-sprite,
.actions .new,
.actions .edit,
.actions .save,
.actions .delete { background: url('/images/icon-34fe0604ab.png') no-repeat; }
.actions .new { background-position: 0 -64px; }
.actions .edit { background-position: 0 -32px; }
.actions .save { background-position: 0 -96px; }
.actions .delete { background-position: 0 0; }
## Magic Imports
As noted above, compass will magically create sprite stylesheets for you. Some people like
magic, some people are scared by it, and others are curious about how the magic works. If
you would like to avoid the magic, you can use compass to generate an import for you. On the
command line:
compass sprite "public/images/icon/*.png"
This will create file using your project's preferred syntax, or you can specify the
output filename using the `-f` option and the syntax will be inferred from the extension.
If you do this, you'll need to remember to regenerate the import whenever you rename, add,
or remove sprites.
Using the magic imports is recommended for most situations. But there are times when you
might want to avoid it. For instance, if your sprite map has more than about 20 to 30
sprites, you may find that hand crafting the import will speed up compilation times. See
the section on [performance considerations](#performance) for more details.
## Magic Selectors
If you want to add selectors for your sprites, it's easy todo by adding `_active` `_target` or `_hover` to the file name, In the example below we have a sprite directory that looks like:
* `link/icon.png`
* `link/icon_hover.png`
* `link/icon_active.png`
* `link/icon_target.png`
Now in our sass file we add:
@import "link/*.png";
@include link_sprite(icon);
And your stylesheet will compile to:
.link-icon {
background-position: 0 0;
}
.link-icon:hover, .link-icon_hover, .link-icon-hover {
background-position: 0 -20px;
}
.link-icon:target, .link-icon_target, .link-icon-target {
background-position: 0 -30px;
}
.link-icon:active, .link-icon_active, .link-icon-active {
background-position: 0 -10px;
}
## Customization Options
### Options per Sprite Map
When constructing the sprite map, the entire sprite map and it's associated stylesheet
can be configured in the following ways. Each option is specified by setting a [configuration
variable](/help/tutorials/configurable-variables/) before importing the sprite. The variables
are named according to the name of the folder containing the sprites. In the examples below
the sprites were contained within a folder called `icon`.
* `$