LESS - 条件混合
描述
您可以使用 default 函数将混合与其他混合匹配进行匹配,并创建类似于 else 或 default 语句的 条件混合。
示例
以下示例演示了如何在 LESS 文件中使用条件混合 −
<!doctype html>
<head>
<title>Conditional Mixins</title>
<link rel = "stylesheet" href = "style.css" type = "text/css" />
</head>
<body>
<h2>Example of Conditional Mixins</h2>
<p class = "myclass">LESS enables customizable, manageable and reusable style sheet for web site.</p>
</body>
</html>
接下来,创建 style.less 文件。
style.less
.mixin (@a) when (@a > 22px) {
color:blue;
}
.mixin (@a) when (@a <= 20px) {
color:red;
}
.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 代码保存在 conditional_mixins.html 文件中。
在浏览器中打开此 HTML 文件,将显示以下输出。

less_mixin_guards.html