Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.