Created
April 2, 2014 19:04
-
-
Save dropofwill/9940875 to your computer and use it in GitHub Desktop.
Sass mixin for scaling elements to an aspect ratio, useful for responsive background images
This file contains 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
@function strip-unit($num) { | |
@if unitless( $num ) != true { | |
@return $num / ($num * 0 + 1); | |
} | |
@else { | |
@return $num; | |
} | |
} | |
// Shortcut for scaling elements to an aspect ratio, useful for responsive background images | |
// Takes: the image's actual dimensions (any units as long as they are consistent) | |
// Outputs: a height: 0 and an appropriate height as a padding-bottom percentage | |
@mixin scale-aspect-ratio($img_width, $img_height) { | |
@if type-of( $img_width ) == number and type-of( $img_height ) == number { | |
$img_width: strip-unit( $img_width ); | |
$img_height: strip-unit( $img_height ); | |
$padding-height: percentage( $img_height / $img_width ); | |
height: 0; | |
padding-bottom: $padding-height; | |
} | |
@else { | |
@warn "Either #{$img_width} or #{$img_height} are not numbers" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment