Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SASS ignores variables, defined in if-statement

I have one file named style.scss with the following code:

@import 'variables';  body {     color: $text-color;     background: $background-color; } 

And one partial named _variables.scss:

$colorscheme: white;  @if $colorscheme == white {     $text-color: #333;     $background-color: #fff; } @else {     $text-color: #ccc;     $background-color: #333; } 

The if-statement works properly, but the variables defined inside, do not work. When I try to compile it, I keep getting:

Syntax error: Undefined variable: “$text-color”.

like image 672
Afterlame Avatar asked Mar 12 '13 20:03

Afterlame


People also ask

How do I write an if statement in Sass?

The @if rule is written @if <expression> { ... } , and it controls whether or not its block gets evaluated (including emitting any styles as CSS). The expression usually returns either true or false —if the expression returns true , the block is evaluated, and if the expression returns false it's not.

Does Sass support CSS variables?

Sass variables are all compiled away by Sass. CSS variables are included in the CSS output. CSS variables can have different values for different elements, but Sass variables only have one value at a time.

Which directive executes code based on the condition in Sass?

The @if directive executes a set of statements a single time based on a Boolean expression. If, on the other hand, you want to execute the statements multiple times, but still control their execution based on a condition, you can use the @while directive.

Where do you declare variables in Sass?

Sass variables are only available at the level of nesting where they are defined. Will the color of the text inside a <p> tag be red or green? It will be red! The other definition, $myColor: green; is inside the <h1> rule, and will only be available there!


2 Answers

That's completely expected. Variables have a scope to them. If you define them inside of a control block (like an if statement), then they won't be available outside. So, what you need to do is initialize it outside like so:

$text-color: null; $background-color: null; @if $colorscheme == white {     $text-color: #333;     $background-color: #fff; } @else {     $text-color: #ccc;     $background-color: #333; } 

Or...

$text-color: #ccc; $background-color: #333; @if $colorscheme == white {     $text-color: #333;     $background-color: #fff; } 

Though it would be less verbose to use the if() function like this:

$text-color: if($colorscheme == white, #333, #ccc); $background-color: if($colorscheme == white, #fff, #333); 
like image 55
cimmanon Avatar answered Oct 01 '22 20:10

cimmanon


While this example is even more verbose, it eliminates the need to set two empty variables:

@if $colorscheme == white {   $text-color: #333 !global;   $background-color: #fff !global; } @else {   $text-color: #ccc !global;   $background-color: #333 !global; } 

@cimmanon's 2nd and 3rd examples are much better, though.

like image 32
graygilmore Avatar answered Oct 01 '22 20:10

graygilmore