Skip to content

Instantly share code, notes, and snippets.

@nboliver
Last active August 14, 2018 16:20
Show Gist options
  • Save nboliver/614a36fba72a1f95c915533a1c7a0796 to your computer and use it in GitHub Desktop.
Save nboliver/614a36fba72a1f95c915533a1c7a0796 to your computer and use it in GitHub Desktop.
$browser-context: 16px !default;
@function strip-unit($num) {
@return $num / ($num * 0 + 1);
}
@function convert-to-rem($value, $base-value: $browser-context) {
// stylelint-disable length-zero-no-unit
$value: strip-unit($value) / strip-unit($base-value) * 1rem;
@if ($value == 0rem) { $value: 0; } // Turn 0rem into 0
@return $value;
}
@function rem($values, $base-value: $browser-context) {
$max: length($values);
@if $max == 1 {
@return convert-to-rem(nth($values, 1), $base-value);
}
$rem-values: ();
@for $i from 1 through $max {
$rem-values: append($rem-values, convert-to-rem(nth($values, $i), $base-value));
}
@return $rem-values;
}
@function em($pixels, $context: $browser-context) {
@if (unitless($pixels)) {
$pixels: $pixels * 1px;
}
@if (unitless($context)) {
$context: $context * 1px;
}
@return $pixels / $context * 1em;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment