Retina Sprites

Updated June 3 2015

1. Make sure you have all png images in both the normal and the retina version.

2. Make sure the retina versions are exactly 2x bigger than the normal versions

3. Create 2 zip files. 1 for the normal images and 1 for the retina versions

4. Got to http://spritegen.website-performance.org/

5. Upload the normal zip file. Choose offset of 25px. Copy the css and download the file
Update: because of some Safari bug it may be better to use 50px for the normal and 100px for the @2x sprite

Drag the images to the generator. Choose padding of 5px. Choose a vertical layout. Click downloads and save the sprite and the stylesheet.

6. Upload the retina zip file. Choose offset of 50px. Do the same for the retina images, but choose a padding of 10px. Copy the css and download the file

Note 1:

Safari has a problem with repeating background images. Fortunately this can be easily solved by specifying a large enough horizontal offset value (configurable).

Note 2:
If the @2x sprite is larger than 2000px the generator creates a new column, which we do not want.

From the website: “Opera 9.0 and below have a bug which affects CSS background offsets less than -2042px. All values less than this are treated as -2042px exactly. With this option is selected CSS Sprite Generator creates a new column each time the vertical offset reaches -2000px.”

So it is best to uncheck the option ‘wrap columns for Opera bug’ (who uses Opera anyway? Let alone < 9.0).

7. You may want to optimize the downloaded sprites.

8. Add the css rules to _sprites.less (or something).

9. Change the stitches rule:

.stitches-sprite(@x: 0, @y: 0, @width: '', @height: '') {
  background-position: @x @y;
  width: @width;
  height: @height;
}

Also add an extra rule:

.sprite-backgroundsize(@img-w, @new-w, @sprite-width, @sprite-height){
 background-size: (@new-w/@img-w)*@sprite-width (@new-w/@img-w)*@sprite-height;
}

And this rule

.sprite-2x {
  background-image: url(../img/spritesheet_2x.png);
  background-repeat: no-repeat;
  display: block;
}

And remove the background sizes form the icon rules:
So this:

.sprite-icon-1 {
  .stitches-sprite(-5px, -175px, 24px, 24px);
}

becomes:

.sprite-icon-1 {
  .stitches-sprite(-5px, -175px);
}

 

10. Now you can add a background image like this:

General rule:

.icon {
  .sprite();
  @media
  (-webkit-min-device-pixel-ratio: 2),
  (min-resolution: 192dpi) {
    .sprite-2x();
  }
  background-size: //USE SIZE OF SPRITE HERE!
  height: 24px;
  width: 24px;
}

Specific icon:

icon-1 {
  .sprite-icon-1()
}

 

Done. If you ever need to change or add images to the sprites, you will only need to change the css rules in _sprites.less