Skip to content

Instantly share code, notes, and snippets.

@MrSwed
Last active November 13, 2019 21:07
Show Gist options
  • Select an option

  • Save MrSwed/958077f384bfeb62cc799b0946999590 to your computer and use it in GitHub Desktop.

Select an option

Save MrSwed/958077f384bfeb62cc799b0946999590 to your computer and use it in GitHub Desktop.
SASS function to calculate needed units
@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