Skip to content

Instantly share code, notes, and snippets.

@brianmcallister
Created March 30, 2016 18:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianmcallister/03b9038b45c02dabba3a779efc6c4e38 to your computer and use it in GitHub Desktop.
Save brianmcallister/03b9038b45c02dabba3a779efc6c4e38 to your computer and use it in GitHub Desktop.
Sass mixin to convert a ratio to a percentage.
// Convert a ratio into a percentage. Useful for responsive images and videos.
// https://gist.github.com/brianmcallister/03b9038b45c02dabba3a779efc6c4e38
//
// $ratio - 2 item list of integers. Represents a ratio. Defaults to 1:1.
//
// Examples
//
// ratio-as-percentage();
// #=> 100%
//
// ratio-as-percentage(16 9);
// #=> 56.25%
//
// Returns a percent value.
@function ratio-as-percentage($ratio: 1 1) {
@if length($ratio) < 2 or length($ratio) > 2 {
@warn '$ratio must be a list with two values.';
}
@return percentage(nth($ratio, 2) / nth($ratio, 1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment