Skip to content

Instantly share code, notes, and snippets.

@elisechant
Created November 15, 2013 00:59
Show Gist options
  • Save elisechant/7477377 to your computer and use it in GitHub Desktop.
Save elisechant/7477377 to your computer and use it in GitHub Desktop.
SASS Type Coercion
// Strip units from any value
//
@function strip-units($number) {
@return $number / ($number * 0 + 1);
}
// Convert pixels to ems
// $pxval may have units or be unitless
// eg. for a relational value of 12px write em(12) when the parent is 16px
// if the parent is another value say 24px write em(12, 24)
@function em($pxval, $context: $base) {
@return (strip-units($pxval) / $context) * 1em;
}
// Convert em to px
// $emval may have units or be unitless
@function px($emval, $context: $base){
@return (strip-units($emval) * $context) +0px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment