註解

Sass 註解的運作方式在 SCSS 和縮排語法之間有很大的不同。兩種語法都支援兩種註解:使用 /* */ 定義的註解(通常)會編譯成 CSS,以及使用 // 定義的註解則不會。

在 SCSS 中在 SCSS 中的永久連結

SCSS 中的註解與 JavaScript 等其他語言中的註解類似。**單行註解**以 // 開頭,直到該行結尾。單行註解中的任何內容都不會輸出為 CSS;就 Sass 而言,它們如同不存在一樣。它們也被稱為**靜默註解**,因為它們不會產生任何 CSS。

**多行註解**以 /* 開頭,並以下一個 */ 結束。如果多行註解寫在允許陳述式 (statement) 的位置,它會被編譯成 CSS 註解。它們也被稱為**大聲註解**,與靜默註解相對。編譯成 CSS 的多行註解可能包含插值 (interpolation),插值會在註解編譯之前進行評估。

預設情況下,在壓縮模式下,多行註解會從編譯後的 CSS 中移除。但是,如果註解以 /*! 開頭,它將始終包含在 CSS 輸出中。

程式碼遊樂場

SCSS 語法

// This comment won't be included in the CSS.

/* But this comment will, except in compressed mode. */

/* It can also contain interpolation:
* 1 + 1 = #{1 + 1} */

/*! This comment will be included even in compressed mode. */

p /* Multi-line comments can be written anywhere
  * whitespace is allowed. */ .sans {
  font: Helvetica, // So can single-line comments.
        sans-serif;
}

CSS 輸出

/* But this comment will, except in compressed mode. */
/* It can also contain interpolation:
* 1 + 1 = 2 */
/*! This comment will be included even in compressed mode. */
p .sans {
  font: Helvetica, sans-serif;
}







在 Sass 中Sass 中的永久連結

縮排語法中的註釋運作方式略有不同:它們是基於縮排的,就像其餘語法一樣。與 SCSS 相似,使用 // 撰寫的靜默註釋永遠不會輸出為 CSS,但與 SCSS 不同的是,在開頭 // 下方縮排的所有內容也會被註釋掉。

/* 開頭的縮排語法註釋在縮排方面的工作方式相同,除了它們會被編譯成 CSS。由於註釋的範圍基於縮排,因此結尾的 */ 是可選的。也與 SCSS 相似,/* 註釋可以包含插值,並且可以以 /*! 開頭以避免在壓縮模式中被移除。

註釋也可以用於縮排語法中的表達式內。在這種情況下,它們的語法與 SCSS 中的語法完全相同。

程式碼遊樂場

Sass 語法

// This comment won't be included in the CSS.
  This is also commented out.

/* But this comment will, except in compressed mode.

/* It can also contain interpolation:
  1 + 1 = #{1 + 1}

/*! This comment will be included even in compressed mode.

p .sans
  font: Helvetica, /* Inline comments must be closed. */ sans-serif

CSS 輸出

/* But this comment will, except in compressed mode. */
/* It can also contain interpolation:
 * 1 + 1 = 2 */
/*! This comment will be included even in compressed mode. */
p .sans {
  font: Helvetica, sans-serif;
}





文件註釋文件註釋的永久連結

使用 Sass 撰寫樣式庫時,您可以使用註釋來記錄您的程式庫提供的Mixin函式變數佔位符選擇器,以及程式庫本身。這些註釋由SassDoc工具讀取,該工具使用它們來產生美觀的文件。查看Susy 網格引擎的文件以了解它的實際應用!

文件註釋是靜默註釋,使用三個斜線 (///) 直接寫在您要記錄的事物上方。SassDoc 將註釋中的文字解析為Markdown,並支援許多有用的註釋來詳細描述它。

程式碼遊樂場

SCSS 語法

/// Computes an exponent.
///
/// @param {number} $base
///   The number to multiply by itself.
/// @param {integer (unitless)} $exponent
///   The number of `$base`s to multiply together.
/// @return {number} `$base` to the power of `$exponent`.
@function pow($base, $exponent) {
  $result: 1;
  @for $_ from 1 through $exponent {
    $result: $result * $base;
  }
  @return $result;
}
程式碼遊樂場

Sass 語法

/// Computes an exponent.
///
/// @param {number} $base
///   The number to multiply by itself.
/// @param {integer (unitless)} $exponent
///   The number of `$base`s to multiply together.
/// @return {number} `$base` to the power of `$exponent`.
@function pow($base, $exponent)
  $result: 1
  @for $_ from 1 through $exponent
    $result: $result * $base

  @return $result