Last active
April 26, 2016 20:15
-
-
Save ryancatalani/52f430223d272744c97a0e2c2b498984 to your computer and use it in GitHub Desktop.
A simple SASS mixin and function for retina background images. Assumes files are named according to Apple's @2x convention (e.g. image.jpg, [email protected]).
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
@mixin background-image-retina($url) { | |
background-image: url($url); | |
@media (-webkit-min-device-pixel-ratio: 1.5), (min-resolution: 144dpi) { | |
background-image: url( str-replace($url, '.', '@2x.') ); | |
} | |
} | |
@function str-replace($string, $search, $replace: '') { | |
// By Hugo Giraudel: http://www.sassmeister.com/gist/1b4f2da5527830088e4d | |
$index: str-index($string, $search); | |
@if $index { | |
@return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace); | |
} | |
@return $string; | |
} | |
// Usage | |
.class { | |
@include background-image-retina('/source/file.jpg'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment