I am trying to do the following statement in SASS:
($value * $ratio) + "em";
I am trying to get it to compile to a result like 16em
but instead am getting "16em"
There appears to be a coercion function coerce(num_units, den_units)
, but I don't understand the documentation, it does not provide any examples, and when I try and compile it, it just spits the function out as a string.
Can anybody tell me how to get this function to work?
You've provided a link to the documentation for SASS source code, which is in Ruby.
The coerce
function is an internal function and in fact is supposed to be applied as a method to a number, e. g. some_number.coerce(...)
.
This function is not available as SASS.
For the list of functions available in SASS, see this page: http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html
There are no existing SASS functions to manipulate units, so you have to create your own:
@function strip-units($value)
@if unitless($value)
@return $value
@else
@return $value / ($value * 0 + 1)
@function add-unit($value, $new-unit)
@return $value + 0#{$new-unit}
@function change-units($value, $new-unit)
@return add-unit(strip-units($value), $new-unit)
$pxs-in-em: 16
@function pxtoem($px-value)
@return change-units($px-value / $pxs-in-em, em)
@function emtopx($em-value)
@return change-units($em-value * $pxs-in-em, px)
// Removing "px" from 1px
@warn "strip-units(1px)", strip-units(1px) // => 1
// Adding "px" to 1
@warn "add-unit(1, px)", add-unit(1, px) // => 1px
// Changing 1px to 1em
@warn "change-units(1px, em)", change-units(1px, em) // => 1em
// Converting 16px to 1em
@warn "pxtoem(16px)", pxtoem(16px) // => 1em
// Converting 1em to 16px
@warn "emtopx(1em)", emtopx(1em) // => 16px