LESS - !important 關鍵字



描述

!important 關鍵字用於覆蓋特定的屬性。當它放在混合(Mixin)呼叫之後時,它會將所有繼承的屬性標記為!important

以下示例演示了在 LESS 檔案中使用!important 關鍵字

<html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
      <title>The !important keyword</title>
   </head>

   <body>
      <h1>Welcome to Tutorialspoint</h1>
      <p class = "para1">LESS is a CSS pre-processor that enables customizable, 
      manageable and reusable style sheet for web site.</p>
      <p class = "para2">LESS is a CSS pre-processor that enables customizable, 
      manageable and reusable style sheet for web site.</p>
   </body>
</html>

接下來,建立style.less 檔案。

style.less

.mixin() {
   color: #900;
   background: #F7BE81;
}

.para1 {
   .mixin();
}

.para2 {
   .mixin() !important;
}

您可以使用以下命令將style.less 編譯為style.css

lessc style.less style.css

執行上述命令;它將自動建立style.css 檔案,其中包含以下程式碼:

style.css

.para1 {
   color: #900;
   background: #F7BE81;
}

.para2 {
   color: #900 !important;
   background: #F7BE81 !important;
}

輸出

請按照以下步驟檢視上述程式碼的工作原理:

  • 將上述 html 程式碼儲存到less_mixin_important.html 檔案中。

  • 在瀏覽器中開啟此 HTML 檔案,將顯示以下輸出。

The !important keyword
less_mixins.htm
廣告

© . All rights reserved.