LESS - 選擇器



描述

選擇器可以引用任何變數,並在編譯時構建。變數名必須放在花括號( { } )內,並在前面加上@符號。

示例

以下示例演示了在 LESS 檔案中使用選擇器

<html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
      <title>LESS selectors</title>
   </head>
	
   <body>
      <h2>Welcome to Tutorialspoint</h2>
         
      <div class = "div1">
         <p>LESS is a CSS pre-processor that enables customizable, 
            manageable and reusable style sheet for web site.</p>
      </div>
      
      <div class = "div2">
         <p>LESS is a dynamic style sheet language that extends the capability of CSS. 
            LESS is also cross browser friendly.</p>
      </div>
   </body>
	
</html>

現在建立style.less檔案。

style.less

@selector: h2;

@{selector} {
   background: #2ECCFA;
}

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

lessc style.less style.css

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

style.css

h2 {
   background: #2ECCFA;
}

輸出

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

  • 將以上 html 程式碼儲存到less_variables_interpolation_selectors.html檔案中。

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

LESS Selectors
less_variables.htm
廣告

© . All rights reserved.