LESS - 函式



描述

LESS 將 JavaScript 程式碼與值的處理對映起來,並使用預定義函式來處理樣式表中 HTML 元素的各個方面。它提供了一些用於處理顏色的函式,例如 round 函式、floor 函式、ceil 函式、percentage 函式等。

示例

以下示例演示了在 LESS 檔案中使用函式:

<html>
   <head>
      <title>Less Functions</title>
      <link rel = "stylesheet" type = "text/css" href = "style.css" />
   </head>
   
   <body>
      <h1>Example using Functions</h1>
      <p class = "mycolor">LESS enables customizable, 
      manageable and reusable style sheet for web site.</p>
   </body>
</html>

現在建立 *style.less* 檔案。

style.less

@color: #FF8000;
@width:1.0;
.mycolor {
   color: @color;
   width: percentage(@width);
}

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

lessc style.less style.css

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

style.css

.mycolor {
   color: #FF8000;
   width: 100%;
}

輸出

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

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

  • 在瀏覽器中開啟此 HTML 檔案,您將看到以下輸出。

Less Functions
廣告