Skip to content

Instantly share code, notes, and snippets.

@jakebellacera
Last active December 18, 2015 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jakebellacera/5777210 to your computer and use it in GitHub Desktop.
Save jakebellacera/5777210 to your computer and use it in GitHub Desktop.
The non-clunky approach to supporting retina images in SASS.
module Sass::Script::Functions
# Appends "_2x" to the end of a filename before the extension
# e.g. foobar.jpg => foobar_2x.jpg
def retinize(string)
assert_type string, :String
Sass::Script::String.new string.value.gsub(/\.(\w+)$/, '_2x.\\1')
end
declare :retinize, :args => [:string]
end
// DRY retina media queries
@mixin media-retina {
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
@content;
}
}
@mixin retina-bg-image($image, $size) {
background-image: url($image);
background-size: $size;
@include media-retina {
background-image: url(retinize($image));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment