Last active
August 29, 2015 14:16
-
-
Save bookwyrm/e7d1f930ee351eb17123 to your computer and use it in GitHub Desktop.
Using an icon sprite in a Media Query with Compass
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
$icons-spacing: 2px; | |
@import "compass/utilities/sprites"; | |
@import "icons/*.png"; | |
@mixin show-icon-sprite($name) { | |
@include ir; | |
@include icons-sprite($name); | |
width: icons-sprite-width($name); | |
height: icons-sprite-height($name); | |
} | |
// Modern compass uses `@extend` for image sprites, but `@extend` doesn't work | |
// with media queries. This alternate form of `show-icon-sprite` doesn't | |
// use `@extend` behind the scenes so it is safe for sprites in media queries. | |
@mixin show-icon-sprite-in-mq($name) { | |
@include ir; | |
@include get-sprite($icons-sprites, $name); | |
} | |
// @mixin based on https://gist.github.com/brubrant/3166895 | |
// http://compass-style.org/reference/compass/helpers/sprites | |
@mixin get-sprite($map, $sprite, $repeat: no-repeat, $height: true, $width: true) { | |
// http://compass-style.org/reference/compass/helpers/sprites/#sprite-file | |
$sprite-image: sprite-file($map, $sprite); | |
// http://compass-style.org/reference/compass/helpers/sprites/#sprite-url | |
$sprite-map: sprite-url($map); | |
// http://compass-style.org/reference/compass/helpers/sprites/#sprite-position | |
$sprite-position: sprite-position($map, $sprite); | |
// output background | |
background: $sprite-map $sprite-position $repeat; | |
// http://compass-style.org/reference/compass/helpers/image-dimensions/ | |
// Checks to see if the user wants width output | |
@if $width == true { | |
width: image-width($sprite-image); | |
} | |
// http://compass-style.org/reference/compass/helpers/image-dimensions/ | |
// Checks to see if the user wants height output | |
@if $height == true { | |
height: image-height($sprite-image); | |
} | |
} |
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
$icons-spacing: 2px; | |
@import "compass/utilities/sprites"; | |
@import "icons/*.png"; | |
@mixin show-icon-sprite($name) { | |
@include ir; | |
@include icons-sprite($name); | |
width: icons-sprite-width($name); | |
height: icons-sprite-height($name); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment