@if

@if

The @if directive takes a SassScript expression and uses the styles nested beneath it if the expression returns anything other than false or null:

p {
  @if 1 + 1 == 2 { border: 1px solid;  }
  @if 5 < 3      { border: 2px dotted; }
  @if null       { border: 3px double; }
}

is compiled to:

p {
  border: 1px solid; }

The @if statement can be followed by several @else if statements and one @else statement. If the @if statement fails, the @else if statements are tried in order until one succeeds or the @else is reached. For example:

$type: monster;
p {
  @if $type == ocean {
    color: blue;
  } @else if $type == matador {
    color: red;
  } @else if $type == monster {
    color: green;
  } @else {
    color: black;
  }
}

is compiled to:

p {
  color: green; }
doc_Sass
2016-11-11 13:09:00
Comments
Leave a Comment

Please login to continue.