重写css属性
我的Html如下所示:重写css属性,css,css-specificity,Css,Css Specificity,我的Html如下所示: <a class="another addAnother">Add another</a> fieldset.associations a.addAnother { display: none !important; } fieldset.associations a.addAnother.another { display: none; } 我正在尝试使用“AddOther”类覆盖标记的样式,如下所示(如果需要): 但我无法超
<a class="another addAnother">Add another</a>
fieldset.associations a.addAnother {
display: none !important;
}
fieldset.associations a.addAnother.another {
display: none;
}
我正在尝试使用“AddOther”类覆盖标记的样式,如下所示(如果需要):
但我无法超越。感谢您的帮助
是否有规则规定在重写样式时,选择器应该相同(我尝试了这个,但没有效果)?这最终取决于这两种样式在CSS中的位置,但您不能再像这样强调一点:
<a class="another addAnother">Add another</a>
fieldset.associations a.addAnother {
display: none !important;
}
fieldset.associations a.addAnother.another {
display: none;
}
您的两个原始声明都具有特异性
0,0,2,2
。如果第二次声明低于第一次声明,则应予以否决。如果没有,请重新排序您的声明或增加特定性
您可以添加
body
标记以增加特异性:
body fieldset.associations a.addAnother {
display: none;
}
这将增加特异性0,0,0,1
,这是您可以添加的最小特异性量
您还可以通过链接类声明使其特定于
。另一个类:
fieldset.associations a.another.addAnother {
display: none;
}
这将增加特异性0,0,1,0
。请注意,本文没有提到!重要
通过1,0,0,0
增加特异性,使其几乎不可能被否决。两个属性具有相同的重要性,因为两个选择器具有相同的特异性。因此,如果第二个出现在CSS中的第一个,它需要获得更大的重要性来覆盖较低的一个。您可以更具体地覆盖第一个,如下所示:
<a class="another addAnother">Add another</a>
fieldset.associations a.addAnother {
display: none !important;
}
fieldset.associations a.addAnother.another {
display: none;
}
或
或
请不要使用!重要的是,它会弄乱你的CSS。请看@Seth:thxs,它现在正在工作@斯蒂芬:实现这一目标的替代方法是什么?thxs@Arjun:请看我关于特定性如何影响CSS规则声明的回答。Lol,或者Andrew的onyMoore@Seth,其他地方(特别是Stephan的链接)提到,!重要信息
是为了允许用户覆盖网站样式而创建的,主要是出于可访问性的原因。我同意在这些情况下应该使用它,因为这是相当必要的。另一方面,可能不应该鼓励Web开发人员使用它来代替理解他们自己的样式规则的层叠和特殊性。@Stephan:3rd样式(以body开头的样式)不起作用。让我检查另外两个。斯蒂芬:检查你的第一句话a.AddOther。另一句话,写下答案是a.Other。addAnother@sandeep,为什么会有什么不同?就我所知,当同一个元素有两个类时,按什么顺序列出它们并不重要。@Arjun:如果它不起作用,那么样式表中还有其他问题@史蒂芬:不知道,但是我给你+1。!重要信息
倾向于将一个好的样式表变成一堆乱七八糟的东西,而且它绝对不能代替理解您自己的CSS规则的特殊性。