LESS - Mixin 命名空间



描述

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

示例

以下示例演示了在 LESS 文件中使用mixin 命名空间

<html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
      <title>Mixin Namespaces</title>
   </head>

   <body>
      <h2>Welcome to Tutorialspoint</h2>
      <p>LESS is a CSS pre-processor that enables customizable, 
      manageable and reusable style sheet for web site.</p>
   </body>
</html>

接下来,创建style.less 文件。

style.less

#outer() {
   background:yellow;
   .inner {
      color: red;
   }
}

p {
   #outer > .inner;
}

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

lessc style.less style.css

执行上述命令;它将自动创建style.css 文件,其中包含以下代码:

style.css

p {
   color: red;
}

输出

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

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

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

LESS Mixin Namespaces
less_mixins.htm
广告

© . All rights reserved.