Skip to content

Instantly share code, notes, and snippets.

@dropofwill
Created April 2, 2014 19:04
Show Gist options
  • Save dropofwill/9940875 to your computer and use it in GitHub Desktop.
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
@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