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 zaixian zaixian</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文件,得到如下输出显示。


上一篇: Less守护命名空间 下一篇: Less混合类型