合格的CSS选择器,什么更有效?

时间:2021-10-26 20:27:04

I wonder which of the following CSS declaration is more efficient and why?

我想知道以下哪个CSS声明更有效,为什么?

JS FIDDLE

HTML

<div class="wrap">
    <div> hallo </div>    
</div>

CSS Version 1:

CSS版本1:

.wrap div{

   color: red;
}

CSS Version 2:

CSS版本2:

div.wrap{

  color: red;
 }

2 个解决方案

#1


6  

div .wrap{
   color: red;
}

Will typically be faster, assuming there are no levels of prior nesting not referred to and the level of relative uniqueness of .wrap is higher than that for div, as browsers interpret and filter selectors right-to-left, typically the level of selector efficiency goes*:

通常会更快,假设没有未提及的先前嵌套级别且.wrap的相对唯一性级别高于div的级别,因为浏览器从右到左解释和过滤选择器,通常是选择器效率的级别去*:

  1. ID, e.g. #header
  2. ID,例如的#header

  3. Class, e.g. .promo
  4. 等级,例如.promo

  5. Type, e.g. div
  6. 类型,例如DIV

  7. Adjacent sibling, e.g. h2 + p
  8. 相邻的兄弟姐妹,例如h2 + p

  9. Child, e.g. li > ul
  10. 孩子,例如li> ul

  11. Descendant, e.g. ul a
  12. 后裔,例如, ul a

  13. Universal, i.e. *
  14. 环球,即*

  15. Attribute, e.g. [type="text"]
  16. 属性,例如[类型= “文本”]

  17. Pseudo-classes/-elements, e.g. a:hover
  18. 伪类/元素,例如答:悬停

*Source

You may want to also have a read of this great article at MDN (and this one as an update to that)

您可能还希望在MDN上阅读这篇精彩文章(并将此文章作为更新)

#2


1  

This depends on how many divs you have and how many instances of the .wrap class you have created.

这取决于您拥有多少div以及您创建的.wrap类的实例数。

If there are 10 divs and 100 .wrap instances then go with version 2, as there are fewer div to test than .wrap.

如果有10个div和100个.wrap实例,则使用版本2,因为测试的div比.wrap少。

How ever I suppose the lookup may be implementation dependent.

我怎么认为查找可能依赖于实现。

#1


6  

div .wrap{
   color: red;
}

Will typically be faster, assuming there are no levels of prior nesting not referred to and the level of relative uniqueness of .wrap is higher than that for div, as browsers interpret and filter selectors right-to-left, typically the level of selector efficiency goes*:

通常会更快,假设没有未提及的先前嵌套级别且.wrap的相对唯一性级别高于div的级别,因为浏览器从右到左解释和过滤选择器,通常是选择器效率的级别去*:

  1. ID, e.g. #header
  2. ID,例如的#header

  3. Class, e.g. .promo
  4. 等级,例如.promo

  5. Type, e.g. div
  6. 类型,例如DIV

  7. Adjacent sibling, e.g. h2 + p
  8. 相邻的兄弟姐妹,例如h2 + p

  9. Child, e.g. li > ul
  10. 孩子,例如li> ul

  11. Descendant, e.g. ul a
  12. 后裔,例如, ul a

  13. Universal, i.e. *
  14. 环球,即*

  15. Attribute, e.g. [type="text"]
  16. 属性,例如[类型= “文本”]

  17. Pseudo-classes/-elements, e.g. a:hover
  18. 伪类/元素,例如答:悬停

*Source

You may want to also have a read of this great article at MDN (and this one as an update to that)

您可能还希望在MDN上阅读这篇精彩文章(并将此文章作为更新)

#2


1  

This depends on how many divs you have and how many instances of the .wrap class you have created.

这取决于您拥有多少div以及您创建的.wrap类的实例数。

If there are 10 divs and 100 .wrap instances then go with version 2, as there are fewer div to test than .wrap.

如果有10个div和100个.wrap实例,则使用版本2,因为测试的div比.wrap少。

How ever I suppose the lookup may be implementation dependent.

我怎么认为查找可能依赖于实现。