我使用以下定义(改编自CSS2规范http://www.w3.org/TR/CSS21/cascade.html#specificity):
- a = 在元素上使用style属性
- b = id属性的数量
- c = 属性(类)和伪类的数量(:link、:hover)
- d = 元素和伪元素的数量(:first-line,:first-letter)
使用以下样式(我的计算结果在右侧):
.content {color: green;} /* a=0 b=0 c=1 d=0 -> 0,0,1,0 */
.content:hover {color: yellow;} /* a=0 b=0 c=2 d=0 -> 0,0,2,0 */
li {color: orange;} /* a=0 b=0 c=0 d=1 -> 0,0,0,1 */
li:first-line {color: pink;} /* a=0 b=0 c=0 d=2 -> 0,0,0,2 */
并且以下是HTML代码
<li class="content">The first line</li>
在浏览器中打开后,这行文字是粉色的,我以为它应该是绿色的,悬停时变成黄色的。我认为元素和伪元素(计算中的“d”)比类和伪类(计算中的“c”)权重小。