Created
September 7, 2013 14:59
-
-
Save SteAllan/6476266 to your computer and use it in GitHub Desktop.
Pixel to REM Fallback Mixin
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
| // My modified version of Karl Merkli's rem mixin from http://css-tricks.com/snippets/css/less-mixin-for-rem-font-sizing/. | |
| // Pass in a property name and the target pixel values and the mixin will spit out the pixel fallback version and the rem version. | |
| @function strip-unit($num) { | |
| @return $num / ($num * 0 + 1); | |
| } | |
| @mixin rem-fallback($property, $values...) { | |
| $max: length($values); | |
| $pxValues: ''; | |
| $remValues: ''; | |
| @for $i from 1 through $max { | |
| $value: strip-unit(nth($values, $i)); | |
| // If the value is '0' don't output the unit. | |
| @if $value == 0 { | |
| $pxValues: #{$pxValues + $value*1}; | |
| $remValues: #{$remValues + $value/16}; | |
| } | |
| // Else do output the unit. | |
| @else { | |
| $pxValues: #{$pxValues + $value*1}px; | |
| $remValues: #{$remValues + $value/16}rem; | |
| } | |
| @if $i < $max { | |
| $pxValues: #{$pxValues + " "}; | |
| $remValues: #{$remValues + " "}; | |
| } | |
| } | |
| #{$property}: $pxValues; | |
| #{$property}: $remValues; | |
| } | |
| //Example usage | |
| h3 { | |
| display: inline-block; | |
| background-color: red; | |
| @include rem-fallback(padding, 16, 32, 48, 64); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment