目录

较少 - 嵌套指令和冒泡( Nested Directives and Bubbling)

描述 (Description)

您可以以与嵌套选择器的方式相同的方式嵌套指令(如mediakeyframe 。 您可以将指令置于顶部,并且不会在其规则集内更改其相关元素。 这被称为冒泡过程。

例子 (Example)

以下示例演示了在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;
   }
}

输出 (Output)

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

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

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

较少的嵌套指令
↑回到顶部↑
WIKI教程 @2018