LESS - 保护比较运算符

说明

LESS 包含五个保护比较运算符:<、>、<=、>= 和 =。您可以使用比较运算符 (=) 来比较数字、字符串、标识符等,其余运算符只能用于数字。

示例

以下示例演示了如何在 LESS 文件中使用保护比较运算符 −

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

   <body>
      <h2>Example of Guard Comparison Operators</h2>
      <p class = "myclass">Hello World!!!Welcome to Tutorialspoint...</p>
   </body>
</html>

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

style.less

.mixin (@a) when (@a = 20px){
color:red;
}

.mixin (@a) when (@a < 20px) {
   color:blue;
}

.mixin (@a) {
   font-size: @a;
}

.myclass { .mixin(20px) }

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

lessc style.less style.css

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

style.css

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

输出

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

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

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

Mixin Guards

less_mixin_guards.html