LESS - 保護比較運算子



描述

LESS 包含五個保護比較運算子:<,>,<=,>= 和 =。您可以使用比較運算子 (=) 來比較數字、字串、識別符號等,其餘運算子只能用於數字。

示例

以下示例演示了在 LESS 檔案中使用保護比較運算子:

<!doctype html>
   <head>
      <title>Guard Comparison Operators</title>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
   </head>

   <body>
      <h2>Example of Guard Comparison Operators</h2>
      <p class = "myclass">Hello World!!!Welcome to Tutorialspoint...</p>
   </body>
</html>

接下來,建立 style.less 檔案。

style.less

.mixin (@a) when (@a = 20px){
color:red;
}

.mixin (@a) when (@a < 20px) {
   color:blue;
}

.mixin (@a) {
   font-size: @a;
}

.myclass { .mixin(20px) }

您可以使用以下命令將 style.less 編譯為 style.css

lessc style.less style.css

執行上述命令;它將自動建立包含以下程式碼的 style.css 檔案:

style.css

.myclass {
   color: red;
   font-size: 20px;
}

輸出

按照以下步驟檢視上述程式碼的工作原理:

  • 將上述 html 程式碼儲存到 guard_comparison_operators.html 檔案中。

  • 在瀏覽器中開啟此 HTML 檔案,將顯示以下輸出。

Mixin Guards
less_mixin_guards.htm
廣告
© . All rights reserved.