Skip to content

Instantly share code, notes, and snippets.

@premregmi
Created February 1, 2018 07:07
Show Gist options
  • Save premregmi/19fc6f3a1a11e28024a44251c97cd94a to your computer and use it in GitHub Desktop.
Save premregmi/19fc6f3a1a11e28024a44251c97cd94a to your computer and use it in GitHub Desktop.
PX TO REM FUNCTION in SASS - Convert px value to rem easily in SASS
// SASS FUNCTION TO CONVERT PX INTO REM
// Defining base font size
// The default font size for html is 16px
$base-size: 16;
//Function to convert px value into rem
//You can replace rem with any other value you want (rem is most preferred value for web)
@function size($target, $context: $base-size) {
@return ($target / $context) * 1em;
}
// Let's use the size whenever you want
body {
margin: size(20);
}
@piotrszczesniak
Copy link

Thanks for this one, I prefer to use $base-size with pixels and then add size(...px) with pixels again, the result is the same but for the sake of readability.

@ravivit9
Copy link

Hi, In the latest sass version, '/' won't work and I am trying to use math.div but still doesn't work to calculate the px to rem. Appreciate your input on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment