Created
June 28, 2015 18:42
-
-
Save Dobby89/e9cb26d0c2173d674a0a to your computer and use it in GitHub Desktop.
Very simple method of creating a responsive image sprite using background images (good with an SVG image sprite)
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
// The mixin | |
@mixin responsive-sprite( | |
$sprite-path, | |
$sprite-width, | |
$sprite-height, | |
$icon-width, | |
$icon-height, | |
$icon-pos-x, | |
$icon-pos-y){ | |
$background-pos-x: 0; | |
$background-pos-y: 0; | |
@if ($sprite-width - $icon-width) != 0 { | |
$background-pos-x: ($icon-pos-x * -1) / ($sprite-width - $icon-width); | |
} | |
@if ($sprite-height - $icon-height) != 0 { | |
$background-pos-y: ($icon-pos-y * -1) / ($sprite-height - $icon-height); | |
} | |
display: inline-block; | |
width: 100%; | |
height: 0; | |
padding-bottom: percentage($icon-height / $icon-width); | |
background-image: url($sprite-path); | |
background-size: percentage($sprite-width / $icon-width) percentage($sprite-height / $icon-height); | |
background-position: percentage($background-pos-x) percentage($background-pos-y); | |
background-repeat: no-repeat; | |
} | |
// Usage | |
.sprite { | |
@include responsive-sprite( | |
'sprite.svg', | |
512, | |
256, | |
50, | |
20, | |
-4, | |
-4 | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment