css - 哪个CSS选择器更强?

标签 css css-selectors

<分区>

哪个选择器更强?

#nav li.current

div #nav li

第二个

a.test

.test .test

答案是什么?

最佳答案

来自spec :

A selector's specificity is calculated as follows:

  • count the number of ID selectors in the selector (= a)
  • count the number of class selectors, attributes selectors, and pseudo-classes in the selector (= b)
  • count the number of type selectors and pseudo-elements in the selector (= c)
  • ignore the universal selector

Selectors inside the negation pseudo-class are counted like any other, but the negation itself does not count as a pseudo-class.

Concatenating the three numbers a-b-c (in a number system with a large base) gives the specificity.

Examples:

*               /* a=0 b=0 c=0 -> specificity =   0 */
LI              /* a=0 b=0 c=1 -> specificity =   1 */
UL LI           /* a=0 b=0 c=2 -> specificity =   2 */
UL OL+LI        /* a=0 b=0 c=3 -> specificity =   3 */
H1 + *[REL=up]  /* a=0 b=1 c=1 -> specificity =  11 */
UL OL LI.red    /* a=0 b=1 c=3 -> specificity =  13 */
LI.red.level    /* a=0 b=2 c=1 -> specificity =  21 */
#x34y           /* a=1 b=0 c=0 -> specificity = 100 */
#s12:not(FOO)   /* a=1 b=0 c=1 -> specificity = 101 */

关于css - 哪个CSS选择器更强?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19117761/

相关文章:

javascript - UIKit CSS : Insert sticky sidebar between <article>-tags

javascript - 用于跨度内跨度的 CSS 或 jQuery 选择器

html - 如何在输入元素之前选择标签?

css - TR 中的最后一个 TD 和倒数第二个 TD (CSS/LESS)

javascript - 访问样式背景颜色

html - 表格行没有全高

css - 使 div 在悬停时保持可见 css

php - 使用 Twitter Bootstrap 创建图片库

html - 计算 "Absolutely positioned, non-replaced elements"的宽度

css - 如何跳过第一个 child ?