LESS - 巢狀指令和冒泡



描述

您可以像巢狀選擇器一樣巢狀 mediakeyframe 等指令。您可以將指令放在頂部,其相關元素在其規則集中不會更改。這被稱為冒泡過程。

示例

以下示例演示了在 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 檔案,將顯示以下輸出。

Less Nested Directives
廣告

© . All rights reserved.