LESS - CSS Guards

描述

Guards 用于匹配表达式上的简单值或多个参数。它应用于 CSS 选择器。它是声明 mixin 并立即调用它的语法。要成功调用 if 类型语句;请将其与功能 & 结合使用,该功能允许您对多个 guards 进行分组。

示例

以下示例演示了在 LESS 文件中使用 css guard −

css_guard.htm

<!doctype html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
   </head>

   <body>
      <div class = "cont">
         <h2>Welcome to TutorialsPoint</h2>
      </div>
		
      <div class = "style">
         <h3>The largest Tutorials Library on the web.</h3>
      </div>
   </body>
</html>

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

style.less

@usedScope: global;
.mixin() {
   @usedScope: mixin;
   .cont when (@usedScope = global) {
      background-color: red;
      color: black;
   }
   
   .style when (@usedScope = mixin) {
      background-color: blue;
      color: white;
   }
   @usedScope: mixin;
}
.mixin();

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

lessc style.less style.css

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

style.css

.style {
   background-color: blue;
   color: white;
}

输出

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

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

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

Less css guard