Skip to content

Instantly share code, notes, and snippets.

@rhysburnie
Last active December 28, 2015 23:08
Show Gist options
  • Save rhysburnie/7576221 to your computer and use it in GitHub Desktop.
Save rhysburnie/7576221 to your computer and use it in GitHub Desktop.
@mixin instrinsic-ratio($width, $height, $position: relative)
{
@if unit($width) != unit($height) {
@warn "@include instrinsic-ratio-wrapper: $width and $height must share the same unit type, supplied was: #{$width} / #{$height}";
}
$w: to-unitless($width);
$h: to-unitless($height);
$ratio: $h / $w;
$percent: $ratio * 100%;
& {
position: $position;
height: 0;
padding-bottom: $percent;
}
}
@mixin maintain-aspect
{
& {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
}
@function to-unitless($val){
@if unitless($val) {
@return $val;
}
$u:unit($val);
$u:if($u==px,1px,
if($u==em,1em,
if($u==rem,1rem,
if($u=='%',1%,
if($u==pt,1pt,
if($u==pc,1pc,
if($u==in,1in,
if($u==mm,1mm,
if($u==cm,1cm,
1)))))))));
@return $val/$u;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment