!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混合類型