sass

Convert units

Convert px to (r)em

To convert px to em or rem you can use the following function:

@function rem-calc($size, $font-size : $font-size) {
    $font-size: $font-size + 0px;
    $remSize: $size / $font-size;
    @return #{$remSize}rem;
}

@function em-calc($size, $font-size : $font-size) {
    $font-size: $font-size + 0px;
    $remSize: $size / $font-size;
    @return #{$remSize}em;
}

The $font-size is the original font size.

For example:

$font-size: 14;

body {
  font-size: #{$font-size}px;
  font-size: rem-calc(14px); // returns 1rem
  // font-size: rem-calc(28); // returns 2rem
}

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow