您可以以相同的方式嵌套指令,例如 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 .
.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 文件;
@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文件,将显示以下输出.