LESS - 預設變數



描述

預設變數只在變數未被設定時才設定變數。此功能並非必需,因為可以透過之後定義輕鬆覆蓋變數。

示例

以下示例演示了 LESS 檔案中預設變數 的用法:

<html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
      <title>LESS Default Variables</title>
   </head>

   <body>
      <h1>Welcome to Tutorialspoint</h1>
      <p>LESS is a CSS pre-processor that enables customizable, 
      manageable and reusable style sheet for web site.</p>
   </body>
</html>

現在建立檔案style.less

style.less

@import "https://tutorialspoint.tw/less/lib.less"; // first declaration of @color
@color: green; // this will override @color defined previously
p {
   color : @color;
}

以下程式碼將從https://tutorialspoint.tw/less/lib.less路徑匯入lib.less檔案到style.less中:

lib.less

@color: blue;

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

lessc style.less style.css

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

style.css

p {
   color: green;
}

輸出

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

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

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

LESS Default Variables
less_variables.htm
廣告
© . All rights reserved.