Last active
August 29, 2015 14:02
-
-
Save ogbaoghene/20222a08fcab548f5593 to your computer and use it in GitHub Desktop.
Convert px values for multiple property-values pairs to rem values with px fallbacks.
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
| // ---- | |
| // Sass (v3.3.8) | |
| // Compass (v1.0.0.alpha.19) | |
| // ---- | |
| $baseFontSize: 16; | |
| @function stripUnit ($num) { | |
| @return $num / ($num * 0 + 1); | |
| } | |
| @function pxToRem($values) { | |
| $list: (); | |
| @each $value in $values { | |
| @if $value == 0 or $value == auto or unit($value) != px and unitless($value) != true { | |
| // Ignore 0, auto, and units except px | |
| $list: append($list, $value); | |
| } | |
| @else { | |
| $remValue: ( stripUnit($value) / stripUnit($baseFontSize) ) * 1rem; | |
| $list: append($list, $remValue); | |
| } | |
| } | |
| @if length($list) == 1 { | |
| // return a single value instead of a list, | |
| // so it can be used in calculations | |
| @return nth($list, 1); | |
| } | |
| @else { | |
| @return $list; | |
| } | |
| } | |
| @mixin remFallback($properties) { | |
| @each $property, $value in $properties { | |
| @if type-of($value) == list { | |
| #{$property}: $value; | |
| #{$property}: pxToRem($value); | |
| } | |
| @else if $value == 0 or $value == auto or unit($value) != px and unitless($value) != true { | |
| // Filter values to avoid errors | |
| #{$property}: $value; | |
| } | |
| @else { | |
| #{$property}: $value; | |
| #{$property}: pxToRem($value); | |
| } | |
| } | |
| } | |
| .example { | |
| @include remFallback(( | |
| height: 240 / 2, | |
| font-size: 13px, | |
| text-indent: 0, | |
| left: auto, | |
| margin: 0 10px 3vh 30% | |
| )); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Libsass doesn't support
@eachmultiple assignments yet, which causes errors on compilation. I discovered a workaround [here](After finding this [issue]%28sass/node-sass#263, I discovered that I can). Adjust the mixin to pass eachproperty:valuepair as an item before isolating the$propertyand$valuevariables to allow Libsass compile successfully.Also, had to change the
@includestatement to reflect this.