Created
July 17, 2013 16:59
-
-
Save bkrall/6022402 to your computer and use it in GitHub Desktop.
Foundation's Sass Function: Convert PX to EMs
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
// Converts "px" to "em" using the ($)em-base | |
@function convert-to-em($value) { | |
$value: strip-unit($value) / strip-unit($em-base) * 1em; | |
@if ($value == 0em) { $value: 0; } // Turn 0em into 0 | |
@return $value; | |
} | |
// Working in ems is annoying. Think in pixels by using this handy function, emCalc(#) | |
// Just enter the number, no need to mention "px" | |
@function emCalc($values...) { | |
$max: length($values); // Get the total number of parameters passed | |
// If there is only 1 parameter, then return it as an integer. | |
// This is done because a list can't be multiplied or divided even if it contains a single value | |
@if $max == 1 { @return convert-to-em(nth($values, 1)); } | |
$emValues: (); // This will eventually store the converted $values in a list | |
@for $i from 1 through $max { | |
$emValues: append($emValues, convert-to-em(nth($values, $i))); | |
} | |
@return $emValues; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment