Skip to content

Instantly share code, notes, and snippets.

@alisonailea
Last active August 29, 2015 14:16
Show Gist options
  • Save alisonailea/6a8dc9e49f1c84caec79 to your computer and use it in GitHub Desktop.
Save alisonailea/6a8dc9e49f1c84caec79 to your computer and use it in GitHub Desktop.
PX to REM conversion
<html>
<head>
<title>Example: px2rem</title>
</head>
<body>
<h2>This is in rems but it's also 20px.</h2>
</body>
</html>

PX to REM conversion

Created to allow teams to easily use the superior rem unit of measurement without losing time in translation between px and rem or force Design teams to learn rems.

Credit for the calculation (round($val) / $base;) goes to Edward Irby(https://github.com/EdwardIrby/ephemeralXYZ/blob/master/app/src/utils/_util.scss)

Thanks to Offroad Code for the original REM calculator I used which inspired me to create this. https://offroadcode.com/prototypes/rem-calculator/

A Pen by Alison on CodePen.

License.

@import "bourbon";
@function parseInt($n) {
$int: $n * 0 + 1;
@return $n / $int;
}
@function remCalculator($pixelValue){
$font-unit: 0rem;
// strip px from values
$pixelValue: round(parseInt($pixelValue));
$baseFontValue: parseInt($font-size-base);
// calculate rem value in comparison to base fontsize
$remCalc: $pixelValue / $baseFontValue;
@return $remCalc + $font-unit;
}
// USAGE
$font-size-base: 16px;
html, body {
font-size: $font-size-base;
}
h2 {
font-size: px2rem(20px);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment