-
-
Save alanhogan/2878758 to your computer and use it in GitHub Desktop.
Using Compass to generate normal and retina sprite maps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$sprites: sprite-map("sprites/*.png"); | |
$sprites-retina: sprite-map("sprites-retina/*.png"); | |
@mixin sprite-background($name) { | |
background-image: sprite-url($sprites); | |
background-position: sprite-position($sprites, $name); | |
background-repeat: no-repeat; | |
display: block; | |
height: image-height(sprite-file($sprites, $name)); | |
width: image-width(sprite-file($sprites, $name)); | |
@media (-webkit-min-device-pixel-ratio: 2), (-o-min-device-pixel-ratio: 3/2), (min-device-pixel-ratio: 2) { | |
// Workaround for https://gist.github.com/2140082 | |
@if (sprite-position($sprites, $name) != sprite-position($sprites-retina, $name)) { | |
$ypos: round(nth(sprite-position($sprites-retina, $name), 2) / 2); | |
background-position: 0 $ypos; | |
} | |
// Grab size of non-retina sprite image. | |
@include background-size(image-width(sprite-url($sprites)) image-height(sprite-url($sprites))); | |
background-image: sprite-url($sprites-retina); | |
} | |
} | |
// Usage. | |
.mail-icon { | |
@include sprite-background(mail); | |
} |
Instead of using
@include background-size(image-width(sprite-url($sprites)) image-height(sprite-url($sprites)));
try
@include background-size(image-width(sprite-path($sprites)) image-height(sprite-path($sprites)));
Here is my try to make it little short and for best practice. I wanted to use background-image property for once instead of using it in every class to minimise my code. I make this
https://gist.github.com/mbilalsiddique1/fe0098702e10c27371d1
@import "compass/utilities/sprites"; // Include compass sprite helpers
@import "compass/css3/background-size"; // Include helper to calc background size
// General Sprite Defaults
// You can override them before you import this file.
$icon-sprite-base-class: ".icon-sprite" !default;
$icon-sprite-dimensions: false !default;
$icon-spacing: 10px !default;
$icon-position: 0% !default;
$icon-repeat: no-repeat !default;
$icon-sprites: sprite-map("sprites/*.png", $spacing: $icon-spacing, $repeat: $icon-repeat, $position: $icon-position);
$icon-sprites-retina: sprite-map("sprites-retina/*.png", $spacing: $icon-spacing * 2, $repeat: $icon-repeat, $position: $icon-position);
// All sprites should extend this class
// The icon-sprite mixin will do so for you.
#{$icon-sprite-base-class} {
background: $icon-sprites $icon-repeat;
}
@media (-webkit-min-device-pixel-ratio: 1.5), (min--moz-device-pixel-ratio: 1.5), (-o-min-device-pixel-ratio: 3 / 2), (min-device-pixel-ratio: 1.5), (min-resolution: 1.5dppx) {
#{$icon-sprite-base-class} {
background: $icon-sprites-retina $icon-repeat;
@include background-size(ceil(image-width(sprite-path($icon-sprites-retina)) / 2) auto);
}
}
// Extends the sprite base class and set the background position for the desired sprite.
// It will also apply the image dimensions if $dimensions is true.
@mixin icon-sprite($name, $dimensions: $icon-sprite-dimensions, $offset-x: 0, $offset-y: 0) {
@extend #{$icon-sprite-base-class};
@include sprite($icon-sprites, $name, $dimensions, $offset-x, $offset-y)
}
// Example Usage.
.icon-analytics {
@include icon-sprite(main-sprite);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Getting an error. Seems to be on like 18 (in my scss).
error sass/screen.scss (Line 21 of sass/include/_nav-icons.scss: Unrecognized file type: png'))