LESS - 嵌套指令和冒泡
描述
您可以像嵌套选择器一样嵌套 media 和 keyframe 等指令。您可以将指令放在顶部,其相关元素不会在其规则集内发生变化。这称为冒泡过程。
示例
以下示例演示了如何在 LESS 文件中使用嵌套指令和冒泡 −
<html>
<head>
<title>Nested Directives</title>
<link rel = "stylesheet" type = "text/css" href = "style.css" />
</head>
<body>
<h1>Example using Nested Directives</h1>
<p class = "myclass">LESS enables customizable,
manageable and reusable style sheet for web site.</p>
</body>
</html>
接下来,创建文件 style.less。
style.less
.myclass {
@media screen {
color: blue;
@media (min-width: 1024px) {
color: green;
}
}
@media mytext {
color: black;
}
}
您可以使用以下命令将 style.less 文件编译为 style.css −
lessc style.less style.css
执行上述命令;它将使用以下代码自动创建 style.css 文件 −
style.css
@media screen {
.myclass {
color: blue;
}
}
@media screen and (min-width: 1024px) {
.myclass {
color: green;
}
}
@media mytext {
.myclass {
color: black;
}
}
输出
按照以下步骤查看上述代码的工作原理 −
将上述 html 代码保存在 nested_directives_bubbling.html 文件中。
在浏览器中打开此 HTML 文件,将显示以下输出。

