Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does SASS support inline IF statements?

Tags:

sass

inline

I'd like to set a SASS variable using inline IF's similar to how its done in PHP or Javascript

Is something like this possible?

$myVar: ($something >= 10 ? true : false);

I'm aware of @if control directives but I want to use a shorter inline syntax.

like image 673
Nick Carson Avatar asked Aug 14 '14 05:08

Nick Carson


People also ask

Can we use if condition in SCSS?

In SASS we can make use of the if-else statement, and even else-if , just as we can do in programming languages.

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.

Which directive executes code based on the condition 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.


1 Answers

Sass does support of conditional (ternary) operator or one line if statement as it works in languages like Javascript or PHP.

$variable: if(condition, result-when-true, result-when-false);

Article about improved if and what's new in Sass 3.3.
P.S. Before Sass 3.3 it did not work as it should. Issue in Sass repo.

like image 55
Rakhat Avatar answered Sep 17 '22 13:09

Rakhat