LESS - 條件混合



描述

您可以使用default函式將混合與其他混合匹配進行匹配,並建立類似於elsedefault語句的條件混合

示例

以下示例演示了在 LESS 檔案中使用條件混合 −

<!doctype html>
   <head>
      <title>Conditional Mixins</title>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
   </head>

   <body>
      <h2>Example of Conditional Mixins</h2>
      <p class = "myclass">LESS enables customizable, manageable and reusable style sheet for web site.</p>
   </body>
</html>

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

style.less

.mixin (@a) when (@a > 22px) {
   color:blue;
}

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

.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 程式碼儲存在conditional_mixins.html檔案中。

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

Mixin Guards
less_mixin_guards.htm
廣告

© . All rights reserved.