Subtract Pixels From Percentage in SASS?

realph picture realph · May 18, 2012 · Viewed 11.1k times · Source

I know you're able to do some simple math in Sass/Scss. But is there a way to subtract pixels from percentages? For example:

$image-size: 200px;

.bio {
    width: 100% - $image-size;
}

Answer

Balthazar picture Balthazar · Aug 19, 2014

You can use the calc() function to get what you want. It's experimental but still pretty good supported by the different browsers.

With Sass, you can create a calc mixin to get this working on more browser versions with the-webkit and -moz prefixes (no need of the Opera one):

@mixin calc($key, $value) {
  #{$key}: -webkit-calc(#{$value});
  #{$key}: -moz-calc(#{$value});
  #{$key}: calc(#{$value});
}

And call it with something like:

.bio {
  @include calc("width", "100% - #{$image-size}");
}