Skip to content

Instantly share code, notes, and snippets.

@craigmdennis
Last active December 28, 2016 06:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigmdennis/c36664b5f0c07bdcb186 to your computer and use it in GitHub Desktop.
Save craigmdennis/c36664b5f0c07bdcb186 to your computer and use it in GitHub Desktop.
PX to REM Sass conversion utility
// ===================================================
// Rem Conversion
// ===================================================
$pixelBase : 16;
@function parseInt($n) {
@return $n / ($n * 0 + 1);
}
@function u($values){
$list: ();
@each $value in $values {
// If a 0 or auto just add it to the list
@if $value == 0 or type-of($value) != "number" {
$list: append($list, $value);
}
// Else split the unit and value
@else {
$unit : unit($value);
$val : parseInt($value);
// Convert rem to px if $old-ie is true
@if ($old-ie) and ($unit == 'rem') {
$list: append($list, ($val * $pixelBase) + px);
}
// Convert px to rem
@else if($unit == 'px') {
$list: append($list, ($val / $pixelBase) + rem);
}
// Leave rems alone
@else {
$list: append($list, $value);
}
// Return a warning for unknown units
@else {
@warn 'There is no unit conversion for #{$unit}';
}
}
}
@return $list();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment