LESS - 保护逻辑运算符
描述
您可以使用 and 关键字来解决带保护的逻辑运算符问题。您可以使用 and 关键字组合保护条件,并使用 not 关键字否定条件。
示例
以下示例演示了如何在 LESS 文件中使用保护逻辑运算符 −
<!doctype html>
<head>
<title>Guard Logical Operators</title>
<link rel = "stylesheet" href = "style.css" type = "text/css" />
</head>
<body>
<h2>Example of Guard Logical Operators</h2>
<p class = "class1">Hello World...</p>
<p class = "class2">Welcome to Tutorialspoint...</p>
</body>
</html>
接下来,创建 style.less 文件。
style.less
.mixin (@a) when (@a > 50%) and (@a > 5px) {
font-size: 14px;
}
.mixin (@a) when not (@a < 50%) and not (@a < 5px) {
font-size: 20px;
}
.mixin (@a) {
color: @a;
}
.class1 { .mixin(#FF0000) }
.class2 { .mixin(#555) }
您可以使用以下命令将 style.less 编译为 style.css −
lessc style.less style.css
执行上述命令;它将使用以下代码自动创建 style.css 文件 −
style.css
.class1 {
font-size: 20px;
color: #FF0000;
}
.class2 {
font-size: 20px;
color: #555;
}
输出
按照以下步骤查看上述代码的工作原理 −
将上述 html 代码保存在 guard_logical_operators.html 文件中。
在浏览器中打开此 HTML 文件,将显示以下输出。

less_mixin_guards.html