2011-03-16 62 views
1

我的HTML是这样的:重写的CSS属性

<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; 
} 

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

是否有任何规则,虽然覆盖样式选择器应该是相同的(我试过这个,但没有用)??

回答

2

这两个属性都具有相同的重要性,因为这两个选择器具有同等的特定性。因此,如果第二个出现在CSS中的第一个,它需要更重要,以覆盖更低的一个。你可以通过更具体的覆盖第一位的,是这样的:

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

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

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

@Stephan:第三种风格(一个以body开头的)不起作用。让我检查另外两个。 Thxs。 – Arjun 2011-03-16 12:38:12

+0

@Stephan:检查你的第一条语句a.addAnother.another,写回答是a.another.add另一个 – sandeep 2011-03-16 12:47:52

+2

@sandeep,为什么会有所作为?据我所知,当同一个元素有两个类时,它们列出它们的顺序并不重要。 – 2011-03-16 13:09:15

-3
fieldset.associations a.addAnother { 
    display: none !important; 
} 
+0

'important'有转好的样式表成的趋势!完全混乱,它绝对不能替代你理解你自己的CSS规则的特殊性。 – 2011-03-16 12:40:34

-3

这将最终取决于在那两个款式都在你的CSS,但你不能给一个更重要的是这样的:

fieldset.associations a.addAnother { 
    display: none !important; 
} 
+1

请不要使用!重要的是,它会弄乱你的CSS。请参阅http:// stackoverflow。com/q/3706819/124238 – 2011-03-16 12:22:20

+0

@Seth:thxs,现在工作。 @Stephan:实现这一目标的另一种方式是什么? thxs。 – Arjun 2011-03-16 12:26:56

+0

@Arjun:看我的答案如何特异性影响CSS规则声明。 – 2011-03-16 12:28:55

3

无论你原来的声明具有的0,0,2,2特异性。如果第二个声明低于第一个声明,它应该否决它。如果没有,请重新排列您的声明或增加特异性。

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

将由0,0,0,1增加特异性,特异性的最低金额,您可以添加:


你可以为了增加特异性添加body标签。


你也可以把它具体到.another类通过链接类声明:

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

将由0,0,1,0增加特异性。


Here is an article explaining CSS specificity。请注意,该文章没有提及!important1,0,0,0增加了特异性,使其几乎不可能推翻。

+1

+1是特定的,并链接到文章 – 2011-03-16 12:31:41