Skip to content

Instantly share code, notes, and snippets.

@Darep
Created July 11, 2013 21:05
Show Gist options
  • Save Darep/5979236 to your computer and use it in GitHub Desktop.
Save Darep/5979236 to your computer and use it in GitHub Desktop.
My favorite Sass function <3
// Converts pixels to EMs
// Usage: em(16) em(16px) em(16, 24) em(16px, 24px) em(16px, 1em)
@function strip-units($number) {
@return $number / ($number * 0 + 1);
}
@function em($pixels, $context: 16) {
$base: 16;
$value: strip-units($pixels);
$value_unit: unit($pixels);
$c: strip-units($context);
$c_unit: unit($context);
@if $c_unit == "em" {
@return $value/($base/$c)+0em;
} @else {
@return $value/$c+0em;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment