Last active
December 28, 2016 06:19
-
-
Save craigmdennis/c36664b5f0c07bdcb186 to your computer and use it in GitHub Desktop.
PX to REM Sass conversion utility
This file contains hidden or 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
// =================================================== | |
// 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