目录

Selectors

描述 (Description)

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

例子 (Example)

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

<html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
      <title>LESS selectors</title>
   </head>
   <body>
      <h2>Welcome to IoWiki</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;
}

输出 (Output)

请按照以下步骤查看上述代码的工作原理 -

  • 将上述html代码保存在less_variables_interpolation_selectors.html文件中。

  • 在浏览器中打开此HTML文件,将显示以下输出。

少选择器
↑回到顶部↑
WIKI教程 @2018