CSS选择器的优先级
关于CSS选择器优先级,目前国内已有很多人进行过解释,但感觉不如人意,特别对于初学者,更是难以理解。这里我把W3C上所描述的规范以我的理解再解释一下,希望能给大家提供到帮助。
在Calculating a selector’s specificity上的原文摘选如下:
(相信很多人更喜欢看原文)
A selector’s specificity is calculated as follows:
- count 1 if the declaration is from is a ’style’ attribute rather than a rule with a selector, 0 otherwise (= a) (In HTML, values of an element’s “style” attribute are style sheet rules. These rules have no selectors, so a=1, b=0, c=0, and d=0.)
- count the number of ID attributes in the selector (= b)
- count the number of other attributes and pseudo-classes in the selector (= c)
- count the number of element names and pseudo-elements in the selector (= d)
The specificity is based only on the form of the selector. In particular, a selector of the form “[id=p33]” is counted as an attribute selector (a=0, b=0, c=1, d=0), even if the id attribute is defined as an “ID” in the source document’s DTD.
Concatenating the four numbers a-b-c-d (in a number system with a large base) gives the specificity.
Some examples:
* {} /* a=0 b=0 c=0 d=0 -> specificity = 0,0,0,0 */
li {} /* a=0 b=0 c=0 d=1 -> specificity = 0,0,0,1 */
li:first-line {} /* a=0 b=0 c=0 d=2 -> specificity = 0,0,0,2 */
ul li {} /* a=0 b=0 c=0 d=2 -> specificity = 0,0,0,2 */
ul ol+li {} /* a=0 b=0 c=0 d=3 -> specificity = 0,0,0,3 */
h1 + *[rel=up]{} /* a=0 b=0 c=1 d=1 -> specificity = 0,0,1,1 */
ul ol li.red {} /* a=0 b=0 c=1 d=3 -> specificity = 0,0,1,3 */
li.red.level {} /* a=0 b=0 c=2 d=1 -> specificity = 0,0,2,1 */
#x34y {} /* a=0 b=1 c=0 d=0 -> specificity = 0,1,0,0 */
style="" /* a=1 b=0 c=0 d=0 -> specificity = 1,0,0,0 */
============
CSS优先级的读法
这里先更正一些错误的读法。通过百度搜索到的内容中,通常出现这样的写法:(1,0,0,0),但有部分文章对它的解释不全面,有的甚至有误。
其中最大的一个错误就是把结果加:(1,0,0,0)=1000,(0,0,2,2)=22,更有甚者:(0,1,0,1)=0+1+0+1=2!虽然这些理解在很简单的情况下看上去是正确的,但本质上却是个重大的错误。
另外有部分文章把它理解为4个级别,也相近,但不能把条理分清楚,理解起来也难。
“CSS优先级包含四个级别(文内选择符,ID选择符,Class选择符,元素选择符)以及各级别出现的次数。根据这四个级别出现的次数计算得到CSS的优先”级。
这句话总结得很好,但对初学者来说,在理解方面就有点难度了,“四个级别”,太容易混淆,其实应该是“四组级别”。
我认为,对优先级的读法,应该是以“组”来分,这个组之间相互独立,从左到右进行对比。它们成组出现,以逗号分隔。
selector( a , b , c , d ) compare: ↑ , ↑ , ↑ , ↑ selector( a , b , c , d )
正如w3c.org中原文所示,分为a,b,c,d四组,全为正整娄,默认为0,对应于不同的选择器结构和组成形式。在选择器之间的优先级进行对比时,从左到右1对1对比,当比出有大者时即可停止比较。
li.red.level {} /* a=0 b=0 c=2 d=1 -> specificity = 0 , 0 , 2 , 1 */
/*compare ↑ , ↑ , √ */
h1 + *[rel=up]{} /* a=0 b=0 c=1 d=1 -> specificity = 0 , 0 , 1 , 1 */
/*compare ↑ , ↑ , ↑ , √ */
ul ol li.red {} /* a=0 b=0 c=1 d=3 -> specificity = 0 , 0 , 1 , 3 */
/*compare ↑ , ↑ , √ */
#x34y {} /* a=0 b=1 c=0 d=0 -> specificity = 0 , 1 , 0 , 0 */
/*compare ↑ , √ */
style="" /* a=1 b=0 c=0 d=0 -> specificity = 1 , 0 , 0 , 0 */
(上表中,↑表示还要进行比较,√表示从此处已得到了结果)
其实这里就像个奥运金牌榜,到我修改文章为止(2008-08-12),我国以13金领先,详细如下:
| 1 | 中国 | 13 | 3 | 4 |
| 2 | 美国 | 7 | 6 | 8 |
| 3 | 韩国 | 5 | 6 | 1 |
| 4 | 意大利 | 3 | 4 | 2 |
.
再有,只要正确书写,仅从优先级中大概知道选择器结构形式了,如:
1,0,0,0表示是元素内的style;
0,2,1,1表示是一个由两个ID选择器,1个类或伪类或属性选择器,以及一个元素选择器组成的选择器。
CSS优先级规则的细节:
在纠正读法后,才能开始讲详细的规则:
- a组数值只有把CSS写进style属性时才会为1,否则为0.写进style的样式声明其实不算是个选择器,所以这里面的b,c,d组值均为0,只有真正的选择器才会有b,c,d组值。
- b组数值决定于ID选择器#ID,有多少个ID选择器,并会进行此组数值累加;
- c组数值决定于类、伪类和属性选择符,并会进行该组数值累加;
- d组数值决定于元素名,即元素选择器,并会进行该组数值累加;
注意,这四组数值分别对应于不同类型的选择器,互不影响,根据读法法则进行比较。
(这里没有讨论到!important,就近原则和继承,也没有实例代码,欢迎大家共同讨论!)
参考:
[1] CSS优先级问题
[2]Calculating a selector’s specificity
Popularity: 49% [?]
Tags: CSS
文章字体大小:

2 Responses to “CSS选择器的优先级”
Leave a Reply
八月 12th, 2008 at 3:45 下午
八月 13th, 2008 at 12:15 上午
没工夫思考这么高深的东西,CSS我一直在用,原来一直只是皮毛,呵呵。