I've spent the past 4 hours trying to find a way to create a sprite image with Compass and sass that also automatically scales each individual image for use with the background-size property.
Nothing I've found works, can't believe it's that difficult.
Does any one have a working example?
Edit: This is what I have so far
@mixin resize-sprite($map, $sprite, $percent) {
$spritePath: sprite-path($map);
$spriteWidth: image-width($spritePath);
$spriteHeight: image-height($spritePath);
$width: image-width(sprite-file($map, $sprite));
$height: image-height(sprite-file($map, $sprite));
@include background-size(ceil($spriteWidth * ($percent/100)) ceil($spriteHeight * ($percent/100)));
width: ceil($width*($percent/100));
height: ceil($height*($percent/100));
background-position: 0 floor(nth(sprite-position($map, $sprite), 2) * ($percent/100) );
}
@mixin resize-sprite-set($map, $percent, $only...) {
$name: sprite_map_name($map);
@each $sprite in sprite_names($map) {
@if length($only) == 0 or index($only, $sprite) != false {
.#{$name}-#{$sprite} {
@include resize-sprite($map, $sprite, $percent);
}
}
}
}
The mixin returns no errors.
$my-icons-spacing: 10px; // give some space to avoid little pixel size issues on resize
@import "my-icons/*.png";
$my-icons-sprite-dimensions: true;
@include all-my-icons-sprites;
// the fun part
.small-icons { // overriding all sprites
@include resize-sprite-set($my-icons-sprites, 40); // 40% sized
}
.some-part-of-my-site {
@include resize-sprite-set($my-icons-sprites, 40, logo, ok); // will create overrides only for sprites "logo" and "ok"
}
I get the following error message from the above implementation when I try to compile. Via Prepros App.
remove ../images/my-icons-s9e77ab1ef1.png
create ../images/my-icons-s9e77ab1ef1.png
error style.scss (Line 62 of _mixins.scss: Undefined mixin 'resize-sprite-set'.)
identical ../css/style.css
I've also done some research on this. This gist is what I came up with: https://gist.github.com/apauly/7917906
Update:
The solution depends on three key-parts:
0. Grab the dimensions for both, the complete sprite and the single icon:
1. Consider a div displaying a sprite as its background. Set
background-size: 100%;
to make sure, that the background sprite covers the full width of the div. If one would usewidth: 100%;
, the result would be something like this:So we need to enlarge our background to get something like this: (the div should have
overflow:hidden
though)To achieve that, just divide the width of the complete sprite by the width of the single icon:
2. This one is basically inspired by a blog post form tkenny: http://inspectelement.com/tutorials/a-responsive-css-background-image-technique/
The resulting sass code is this:
3. The rest is just some basic math to calculate the top spacing of the icon inside of the sprite as a relative value:
Get the space from the top in pixels (a negative value):
Sass will need a px-value
Set the background position with percentages:
Here's a mixin for resizing sprites that works beautifully
and the github it came from: https://gist.github.com/darren131/3410875