How do you remove units of measurement from a Sass mixin equation?

旧城冷巷雨未停 提交于 2019-12-04 05:23:14
Martin Bavio

Removing units from a number is done by division, just like in Algebra.

$base-font-size: 10px;
$rem-ratio: $base-font-size / 1rem;

@mixin rem($key,$px) {
  #{$key}: $px;
  #{$key}: $px/$rem-ratio;
}


// Include syntax
p {
  @include rem(font-size,14px);
}

// Rendered CSS
p {
  font-size: 14px;
  font-size: 1.4rem;
}

Units in Sass can be treated as in real math, so px/px/rem goes to just rem. Enjoy it!

Here is a function you could use. Based of Foundation global helper functions.

@function strip-unit($num) {

  @return $num / ($num * 0 + 1);
}

Sample use:

... Removed for brevity ...

@mixin rem( $key: font-size, $val ) {
    #{$key}: strip-unit( $val ) * 1px;
    #{$key}: ( strip-unit( $val ) / $base-font-size ) * 1rem;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!