Skip to content

Instantly share code, notes, and snippets.

@mikol
Created March 4, 2015 18:40
Show Gist options
  • Save mikol/0d34adccd7e4ce13175b to your computer and use it in GitHub Desktop.
Save mikol/0d34adccd7e4ce13175b to your computer and use it in GitHub Desktop.
Using SASS to calculate lengths in em from lengths specified in px.
// Ems relative to the typical 16px default body font-size.
$BASE-PX: 16;
// Default $em value, which can be overridden in narrower scopes.
$em: 1em / $BASE-PX;
// Writes a font-size declaration with EM units based on the pixel measurement
// $size-px, overriding the $em unit variable temporarily for any included
// @content block.
@mixin rel-font-size($size-px, $relative-to-px: $BASE-PX) {
$_m: $em;
$em: 1em / $size-px;
font-size: 1em * $size-px / $relative-to-px;
@content;
$em: $_m;
}
.before-rel-font-size {
font-size: 14 * $em; /* 14 / 16 = 0.875em */
line-height: 20 * $em; /* 20 / 16 * 14 = 17.5 */
}
*, .md-body-1 {
@include rel-font-size(14) { /* 14 / 16 = 0.875em */
letter-spacing: 0.010em;
line-height: 20 * $em; /* 20 / 14 * 14 = 19.99998 */
}
}
.after-rel-font-size {
font-size: 14 * $em; /* 14 / 16 = 0.875em */
line-height: 20 * $em; /* 20 / 16 * 14 = 17.5 */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment