1

我的 HTML 是这样的:

<a class="another addAnother">Add another</a>

我在外部样式表中使用像这样的“另一个”类为上面定义了一个样式。

fieldset.associations a.another {
   color: #693;
   display: block;
   margin: 7.5px 0px 15px;
}

我正在尝试使用'addAnother'类覆盖标签的样式,如下所示(在需要时):

fieldset.associations a.addAnother {
   display: none;
}

但我无法覆盖。任何帮助表示赞赏。

是否有任何规则在覆盖样式时选择器应该相同(我试过这个,但无济于事)?

4

4 回答 4

4

您的两个原始声明都具有0,0,2,2. 如果第二个声明低于第一个声明,它应该否决它。如果没有,请重新排序您的声明或增加特异性。


您可以添加body标签以增加特异性:

body fieldset.associations a.addAnother {
 display: none;
}

这将增加特异性0,0,0,1,您可以添加的最小特异性。


.another您还可以通过链接类声明使其特定于类:

fieldset.associations a.another.addAnother {
 display: none;
}

这将增加特异性0,0,1,0


这是一篇解释 CSS 特殊性的文章。请注意,文章没有提到通过!important增加特异性1,0,0,0,因此几乎不可能推翻。

于 2011-03-16T12:23:31.663 回答
4

这两个属性具有相同的重要性,因为两个选择器都具有相同的特定性。因此,如果第二个出现在 CSS 中首先出现,它需要获得更多的重要性来覆盖较低的一个。您可以通过更具体来覆盖第一个,如下所示:

fieldset.associations a.addAnother.another {
   display: none;
}

或者

#someID fieldset.associations a.addAnother {
   display: none;
}

或者

body fieldset.associations a.addAnother {
   display: none;
}
于 2011-03-16T12:23:38.403 回答
-3
fieldset.associations a.addAnother {
   display: none !important;
}
于 2011-03-16T12:21:04.670 回答
-3

这最终将取决于这两种样式在您的 CSS 中的位置,但您不能像这样给出更多的重要性:

fieldset.associations a.addAnother {
    display: none !important;
}
于 2011-03-16T12:21:07.883 回答