Created
March 1, 2017 09:39
-
-
Save yemster/55bf771a0a739750768f806943a52b8e to your computer and use it in GitHub Desktop.
Automate SVG Sprite Background Image Variations with a SCSS Mixin
This file contains hidden or 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
/* | |
Automate SVG Sprite Background Image Variations with a SCSS Mixin | |
- see egghead.io video: https://egghead.io/lessons/css-automate-svg-sprite-background-image-variations-with-a-scss-mixin | |
• This utilises a sass mixing to generate the necessary code for the icons within the sprite | |
*/ | |
$ico-width-default: 3em; | |
$ico-width-small: 2em; | |
$icons: plug, star, umbrella; | |
.icon { | |
width: $ico-width-default; | |
height: $ico-width-default; | |
display: inline-block; | |
background-image: url(http://link-to-svg-image-sprite.svg) | |
background-repeat: no-repeat | |
background-size: cover; | |
} | |
.icon—small { | |
width: $ico-width-small; | |
height: $ico-width-small; | |
} | |
@each $icon in $icons { | |
$index: index($icons, $icon) - 1; // index of current icon in the loop - 1 as sass index normally starts at 1 but we need it to start from 0 | |
.icon—#{$icon} { | |
background-position: 0 #{-$index * $ico-width-default} | |
&.icon—small { | |
background-position: 0 #{-$index * $ico-width-small} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment