Skip to content

Instantly share code, notes, and snippets.

@lightyrs
Forked from brianmcallister/maintain-ratio.scss
Last active December 3, 2015 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lightyrs/c528727798bcf6afb7b3 to your computer and use it in GitHub Desktop.
Save lightyrs/c528727798bcf6afb7b3 to your computer and use it in GitHub Desktop.
Sass mixin for a responsive box that maintains an aspect ratio.
aspect-ratio-container($horizontal, $vertical)
height 0
padding-bottom ( $vertical / $horizontal * 100 ) %
// Maintain ratio mixin. Great for responsive grids, or videos.
// https://gist.github.com/brianmcallister/2932463
//
// $ratio - Ratio the element needs to maintain.
//
// Examples
//
// // A 16:9 ratio would look like this:
// .element {
// @include maintain-ratio(16 9);
// }
@mixin maintain-ratio($ratio: 1 1) {
@if length($ratio) < 2 or length($ratio) > 2 {
@warn "$ratio must be a list with two values.";
}
$height: percentage(nth($ratio, 2) / nth($ratio, 1));
height: 0;
padding-bottom: $height;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment