Type Height Mixin

Automatically create a EM valued font style with automatic line-height

Set a base font size

  $basefontsize: 16px;
  

Calculate an EM value based on pixel based font size

  @function calculateEm($size) {
    $remSize: $size / $basefontsize;
    @return $emSize * 1em;
  }
  

Calculate a em based line-height based on 1.6 times the font size

  @function lineheight($size) {
    $stripped: $size / ($size * 1 + 1);
    $lineHeight: ($stripped) * 1.6;
    @return ($lineHeight) * 1em;
  }
  

Mixin to include all pixel size, em size and line height

  @mixin typeheight($size) {
    font-size: $size;
    font-size: calculateEm($size);
    line-height: lineheight($size);
  }
  

Call the mixin in your page with a pixel value to get new values with fallback support

  body {
   @include typeheight($basefontsize);
  }

  h1 {
    @include typeheight(36px);
  }
  

And you get...

  body {
    font-size: 16px;
    font-size: 1em;
    line-height: 1.50588em;
  }

  h1 {
    font-size: 36px;
    font-size: 2.25em;
    line-height: 1.55676em;
  }