Picard102
Picard102

Reputation: 644

How can I pass an expression to a calc() mixin that uses Sass variables

I have a mixin set up to do a cross browser calc,

@mixin calc($property, $expression...) { 
  #{$property}: -moz-calc(#{$expression}); 
  #{$property}: -o-calc(#{$expression}); 
  #{$property}: -webkit-calc(#{$expression}); 
  #{$property}: calc(#{$expression}); 
} 

I also have a variable.

$line: 12px;

I want to be able to use a variable within it.

@include calc(width, "30% - ( $line * 2) ) ");

But I'm not sure if that's the best way to go about it.

Upvotes: 17

Views: 8893

Answers (1)

cimmanon
cimmanon

Reputation: 68339

You'll need to use string interpolation on the value you're passing to the mixin:

.foo {
  @include calc(width, #{"30% -  #{$line * 2}"});
}

Output:

.foo {
  width: -moz-calc(30% - 24px);
  width: -o-calc(30% - 24px);
  width: -webkit-calc(30% - 24px);
  width: calc(30% - 24px);
}

Upvotes: 31

Related Questions