CSS选择器的优先级是什么 [英] What are the priorities among CSS selectors
问题描述
CSS问题:如果两个不同的选择器适用于一个元素,谁赢了?
CSS Question: If two different selectors apply to an element, who wins?
我知道这不应该发生,但我想调整一个遗留的应用程序,
I know this shouldn't happen, but I want to tweak a legacy application, and the CSS is getting in the middle.
推荐答案
gory细节在规范实际上是可读的。总结:
The gory details in the spec are actually reasonably readable. In summary:
-
!important
规则和内联<$ c $
!important
rules and inlinestyle
rules win most.
否则,通常更具体的胜利。 是比
更具体的选择器.classname
是比 tagname
。
Otherwise, normally the more specific wins. #id
is a more specific selector than .classname
is a more specific selector than tagname
.
如果规则具有同等的特定性,则最后一次获胜的规则。
Where rules are equally specific, the one declared last wins.
没有特别的理由为什么这个不应该发生。指定一个宽刷规则是正常的,然后添加一个更具体的规则来定位一个案例;对单个元素的多个相同属性规则并不常见或不合需要。
There is no particular reason why this ‘shouldn't happen’. It's normal to specify a broad-brush rule and then add a more specific rule to target one case; multiple same-property rules on a single element are not unusual or undesirable.
这篇关于CSS选择器的优先级是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!