LESS - 命名空间和访问器

描述

命名空间用于将 mixin 分组到一个通用名称下。使用命名空间,您可以避免名称冲突并从外部封装一组 mixin。

示例

以下示例演示了如何在 LESS 文件中使用命名空间和访问器 −

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

现在创建 style.less 文件。

style.less

.class1 {
   .class2 {
      .val(@param) {
         font-size: @param;
         color:green;
      }
   }
}

.myclass {
   .class1 > .class2 > .val(20px);
}

您可以使用以下命令将 style.less 文件编译为 style.css

lessc style.less style.css

执行上述命令;它将使用以下代码自动创建 style.css 文件 −

style.css

.myclass {
   font-size: 20px;
   color: green;
}

输出

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

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

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

Less Scope