Last active
January 2, 2016 09:29
-
-
Save KATT/8283246 to your computer and use it in GitHub Desktop.
Uses CSS hack in order to make an element maintain the aspect ratio of supplied background-image. Demo: http://codepen.io/KATT/pen/yBrni
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
// Uses CSS hack in order to make an element maintain the aspect ratio of supplied background-image | |
// Latest version available @ https://gist.github.com/KATT/8283246 | |
// Demo @ http://codepen.io/KATT/pen/yBrni | |
responsive-image($image, $width = false, $height = false) { | |
$external = match('^https?:\/\/', $image); | |
error('expect width and height on external images') if $external and !($width or $height) | |
unless $width and $height { | |
$size = image-size($image); | |
$width = $size[0]; | |
$height = $size[1]; | |
} | |
if !$external and $public-image-path is defined { | |
$image = $public-image-path + $image | |
} | |
box-sizing: content-box; | |
padding-top: unit($height / $width*100, '%'); | |
background-image: url($image); | |
background-repeat: no-repeat; | |
background-size: $width $height; | |
background-size: contain; | |
if !$external and $public-image-path is defined and $retina is defined { | |
$extension = extname($image); | |
$image2x = pathjoin(dirname($image), basename($image, $extension) + '@2x' + $extension); | |
@media $retina { | |
background-image: url($image2x); | |
} | |
} | |
} |
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
$public-image-path = '../images/'; | |
$retina = 'all and (-webkit-min-device-pixel-ratio: 1.5)'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment