Direktif @if dalam SASS

Jika kita perlu agar gaya tertentu hanya diterapkan ketika kondisi spesifik terpenuhi, maka kita menggunakan direktif @if, yang mengembalikan nilai apa pun, kecuali false dan null:

Mari kita lihat contoh berikut:

styles.scss#main-button {
  @if 2 - 1 == 1 {
    color: green;
  }
  @if (2+3) < 3 {
    color: orange;
  }
  @if null {
    color: red;
  }
}

Hasil kompilasi:

styles.css#main-button {
  color: green; 
}

Juga ketika menetapkan kondisi setelah @if, beberapa @else if dapat digunakan dan satu ekspresi @else.

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;
  }
}

Sekarang mari kita lihat file styles.css:

styles.cssa {
  color: green;
}
Задание 1

Jelaskan, apa hasil kompilasi dari kode berikut:

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

Jelaskan, apa hasil kompilasi dari kode berikut:

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;
  }
}