Skip to content

Instantly share code, notes, and snippets.

@joshuacerbito
Last active February 3, 2017 12:21
Show Gist options
  • Save joshuacerbito/2989258b71e5792a7e7fc36425040073 to your computer and use it in GitHub Desktop.
Save joshuacerbito/2989258b71e5792a7e7fc36425040073 to your computer and use it in GitHub Desktop.
Aspect Ratio Handler Mixin
/*---------------------------------------------------------------*\
ASPECT RATIO HANDLER
preserves the aspect ratio of an element
usage:
@include aspect-ratio(16, 9);
note:
you can use this link to find your desired aspect ratio
http://andrew.hedges.name/experiments/aspect_ratio/
or you can just use the original dimensions of your element
e.g.
@include aspect-ratio(1280, 720);
\*---------------------------------------------------------------*/
@mixin aspect-ratio($w, $h, $bgs: cover, $bgpx: center, $bgpy: center) {
overflow: hidden;
position: relative;
background-size: $bgs;
background-position: $bgpx $bgpy;
background-repeat: no-repeat;
&:before {
padding-bottom: $h * 100% / $w;
display: block;
content: '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment