Created
March 30, 2016 18:17
-
-
Save brianmcallister/03b9038b45c02dabba3a779efc6c4e38 to your computer and use it in GitHub Desktop.
Sass mixin to convert a ratio to a percentage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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