Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sass

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.

like image 286
Picard102 Avatar asked May 21 '13 01:05

Picard102


1 Answers

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);
}
like image 129
cimmanon Avatar answered Nov 07 '22 22:11

cimmanon