css – 如何使用LESS将变量声明为!important?

作者:互联网

2026-03-28

HTML教程
是否可以使用LESS将变量声明为!important?目前,我收到以下语法错误.

例如:

@fontColor: #000000 !important;p {    color: @fontColor;}

应呈现为:

p {    color: #000000 !important;}

我知道有一种逃避的方法,提到HERE,使用以下内容:

@fontColor: #000000 ~'!important';

很久以前有人谈到将它作为简单的语法实现,就像我的第一个例子一样,只是不确定它们是否曾经 – https://github.com/less/less.js/issues/1401

使用Visual Studio 2013(更新4)

我想你@fontColor的声明:#000000!important;确实没有意义,你指定什么?一个字符串,一个列表的东西? 最合理的是一个字符串,在使用时应引用并确实转义:

因此,以下代码似乎是正确的(对我来说)

@fontColor: "#000000 !important";p {    color: ~"@{fontColor}";}

我还发现了以下代码:

@wrong: red noncolor;selector {property: @wrong;}@wrongtoo: red !important;selector {propertytoo: @wrongtoo;}

输出:

selector {  property: red noncolor;}selector {  propertytoo: red;}

编译器(Less v2)在两种情况下都不会抛出错误.但在第二种情况下!重要的是没有在CSS代码中编译.也是红色!noncolor编译成CSS代码.可能!重要的是某种关键字,但对我而言,现在似乎是inconsequence in the compiler.

另请注意,文档描述了如何使用!important与mixins在http://lesscss.org/features/#mixins-feature-the-important-keyword