Arahan @if dalam SASS

Jika kita perlu gaya tertentu diterapkan hanya apabila syarat-syarat spesifik dipenuhi, maka kita menggunakan arahan @if, yang mengembalikan sebarang nilai, kecuali false dan null:

Pertimbangkan 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, apabila meletakkan syarat selepas @if, beberapa @else if boleh digunakan dan satu ungkapan @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 fail styles.css:

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

Beritahu, apakah hasil kompilasi kod 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

Beritahu, apakah hasil kompilasi kod 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;
  }
}