开发学院

您的位置:首页>教程>正文

教程正文

LESS 保护命名空间

LESS 保护命名空间

  当保护应用于命名空间时,只有当保护条件返回true时,才使用由命名空间定义的混合。命名空间保护类似于混合保护。

例子

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

   <body>
      <h2>Welcome to Tutorialspoint</h2>
      <p>This will paragraph be displayed red, when (@color = blue) in style.less 
      and when color is other than blue, then this paragraph will be default black.</p>
   </body>
</html>

  接下来创建style.less文件

/*style.less*/
@import "http://www.tutorialspoint.com/less/lib.less";
#namespace when (@color = blue) {
   .mixin() {
      color: red;
   }
}

p {
   #namespace .mixin();
}

 上面代码导入了路径为https://www.tutorialspoint.com/less/lib.less的lib.less文件到style.less中

/*lib.less*/
@color: blue;

  使用下面命令编译less文件

lessc style.less style.css

  得到css文件

/*style.css*/
p {
   color: red;

输出

  执行以下步骤:

  •   保存上面的HTML代码在 less_mixin_guarded_namespaces.html 文件。

  •   在浏览器中打开该HTML文件,得到如下输出显示。

less_mixin_guarded_namespaces.jpg