Skip to content

Instantly share code, notes, and snippets.

@agkovalev
Created March 25, 2020 17:25
Show Gist options
  • Save agkovalev/2f38d47292ce271fe32f458fc3bb01b6 to your computer and use it in GitHub Desktop.
Save agkovalev/2f38d47292ce271fe32f458fc3bb01b6 to your computer and use it in GitHub Desktop.
Helper Mixins SCSS
@function strip-unit($num) {
@return $num / ($num * 0 + 1);
}
@mixin rem2px($property, $values...) {
$max: length($values);
$pxValues: '';
$remValues: '';
@for $i from 1 through $max {
$value: strip-unit(nth($values, $i));
$pxValues: #{$pxValues + $value*$font-size-basic}px;
@if $i < $max {
$pxValues: #{$pxValues + " "};
}
}
@for $i from 1 through $max {
$value: strip-unit(nth($values, $i));
$remValues: #{$remValues + $value}rem;
@if $i < $max {
$remValues: #{$remValues + " "};
}
}
#{$property}: $pxValues;
#{$property}: $remValues;
}
@mixin px2rem($property, $values...) {
$max: length($values);
$pxValues: '';
$remValues: '';
@for $i from 1 through $max {
$value: strip-unit(nth($values, $i));
$pxValues: #{$pxValues + $value}px;
@if $i < $max {
$pxValues: #{$pxValues + " "};
}
}
@for $i from 1 through $max {
$value: strip-unit(nth($values, $i));
$remValues: #{$remValues + $value/$font-size-basic}rem;
@if $i < $max {
$remValues: #{$remValues + " "};
}
}
#{$property}: $pxValues;
#{$property}: $remValues;
}
@mixin px2remImportant($property, $values...) {
$max: length($values);
$pxValues: '';
$remValues: '';
@for $i from 1 through $max {
$value: strip-unit(nth($values, $i));
$pxValues: #{$pxValues + $value}px;
@if $i < $max {
$pxValues: #{$pxValues + " "};
}
}
@for $i from 1 through $max {
$value: strip-unit(nth($values, $i));
$remValues: #{$remValues + $value/$font-size-basic}rem;
@if $i < $max {
$remValues: #{$remValues + " "};
}
}
#{$property}: $pxValues !important;
#{$property}: $remValues !important;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment