Directive @if in SASS
If we want certain styles to be applied only when specific conditions are met, we use the @if directive, which returns any value except false and null:
Let's consider the following example:
styles.scss#main-button {
@if 2 - 1 == 1 {
color: green;
}
@if (2+3) < 3 {
color: orange;
}
@if null {
color: red;
}
}
Compilation result:
styles.css#main-button {
color: green;
}
Also, when setting a condition after @if, you can use several @else if and one @else expression.
styles.css$var: 20px;
a {
@if $var == 10px {
color: blue;
} @else if $var == 25px {
color: red;
} @else if $var == 10px+10px {
color: green;
} @else {
color: black;
}
}
Now let's look at the styles.css file:
styles.cssa {
color: green;
}
Задание 1
Tell me what the result of compiling the following code will be:
styles.scss$size: 20px;
$font-size: 14px;
body {
@if $size == $font-size {
background-color: #7e7ed8;
} @else if $size > $font-size {
color: #f1bbbb;
} @else {
color: #000000;
}
}
Задание 2
Tell me what the result of compiling the following code will be:
styles.scss$size: 20px;
$font-size: 14px;
.active {
@if $size < $font-size {
color: red;
} @else if $size > $font-size {
color: green;
} @else {
color: #000000;
}
}
.not-active {
@if $size == $font-size {
display:block ;
} @else {
display: none;
}
}