LESS - 选择器

描述

选择器可以引用任何变量,它是在编译时构建的。变量名必须放在花括号( { } ) 内,并以 @ 符号为前缀。

示例

以下示例演示了在 LESS 文件中使用 selector

<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 选择器

less_variables.html