Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using variables in SASS percentage function

Tags:

css

sass

For some reason my SASS is not compiling when I do this:

Suppose this is my global variable:

$gridColumnCount: 12;

@function gridCalc($colNumber, $totalColumns) {
    @return percentage(($colNumber / $totalColumns));
}

@mixin columns($columnSpan: 1) {
    width: gridCalc($columnSpan, $gridColumnCount);
}

This error is returned & the scss file does not compile.

Syntax error: "1/12" is not a unitless number for `percentage'

It seems to not be calculating the percentage but returning the arguments as a string.

If I change the mixin to use non-variable arguments, everything works perfectly... like this:

@mixin columns($columnSpan: 1) {
    width: gridCalc(4, 12);
}

Does anyone know where I'm going wrong.

FYI: SASS version: 3.2.2

like image 904
Zander Avatar asked Sep 18 '25 18:09

Zander


1 Answers

Thanks to @Wesley Murch I figured out what was wrong with my code. I should have called my mixin using this syntax:

@for $i from 1 to $gridColumnCount {
    .span#{$i}, .mq-alpha-resize-to#{$i}  { @include columns($i);  }
}

NOT this:

@for $i from 1 to $gridColumnCount {
    .span#{$i}, .mq-alpha-resize-to#{$i}  { @include columns(#{$i});  }
}

The reason was that SASS was interpolating the numbers.

like image 106
Zander Avatar answered Sep 21 '25 08:09

Zander