Last active
November 13, 2019 21:07
-
-
Save MrSwed/958077f384bfeb62cc799b0946999590 to your computer and use it in GitHub Desktop.
SASS function to calculate needed units
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
| @function strip-unit($num) { | |
| @return $num / ($num * 0 + 1); | |
| } | |
| @function calcSize($size,$base:16px,$unit:1rem,$calc:true) { | |
| // https://gist.github.com/MrSwed/958077f384bfeb62cc799b0946999590 | |
| @if ($calc) { | |
| @return calc(#{$unit} * #{strip-unit($size)} / #{strip-unit($base)}); | |
| } @else { | |
| @return $unit * $size / $base; | |
| } | |
| /* Examples | |
| font-size: calcSize(18px); | font-size: calc(1rem * 18 / 16); | |
| font-size: calcSize(14px,16px,1em,false); | font-size: 0.875em; | |
| */ | |
| } | |
| @function calcEm($size,$base:16px,$unit:1em,$calc:false) { | |
| @return calcSize($size, $base, $unit, $calc); | |
| } | |
| @function calcRem($size,$base:16px,$unit:1rem,$calc:false) { | |
| @return calcSize($size, $base, $unit, $calc); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment