Skip to content

Instantly share code, notes, and snippets.

@brianmcallister
Created October 15, 2012 21:53
Show Gist options
  • Save brianmcallister/3895807 to your computer and use it in GitHub Desktop.
Save brianmcallister/3895807 to your computer and use it in GitHub Desktop.
Retina background image.
/*
Set a background image at 1x and 2x.
https://gist.github.com/3895807
@param {String} $name Image name.
@param {String} $ext File extension, e.g.: '.png'.
*/
@mixin retina-bg($name, $ext) {
@include squish-text;
$image: '#{$name}#{$ext}';
$image2x: '#{$name}@2x#{$ext}';
$imageUrl: image-url($image, true);
$image2xUrl: image-url($image2x, true);
width: image-width($image);
height: image-height($image);
background: 0 0 no-repeat transparent url($imageUrl);
.lt-ie9 & {
background-image: none;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='#{$imageUrl}',sizingMethod='scale');
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='#{$imageUrl}',sizingMethod='scale')";
}
@include retina {
background-image: url($image2xUrl);
background-size: 100%;
.lt-ie9 & {
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='#{$image2xUrl}',sizingMethod='scale');
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='#{$image2xUrl}',sizingMethod='scale')";
}
}
}
/*
Retina mixin.
*/
@mixin retina($ratio: 1.3) {
@media only screen and (-webkit-min-device-pixel-ratio: $ratio),
only screen and (min--moz-device-pixel-ratio: $ratio),
only screen and (-o-min-device-pixel-ratio: ($ratio * 10) / 10),
only screen and (min-resolution: #{round($ratio * 96)}dpi),
only screen and (min-resolution: #{$ratio}dppx) {
@content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment