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.html