简体   繁体   English

CSS类和属性选择器之间的性能

[英]CSS Performance between class and attribute selectors

I'm wondering if there is a performance issue using attribute selectors instead of class selectors. 我想知道使用属性选择器而不是类选择器是否存在性能问题。

div.test {}
div[test] {}

PS : I'm only interested in CSS performance, not JS. PS:我只对CSS性能感兴趣,而不是JS。

根据这篇文章 ,类选择器比属性选择器更有效。

Someone has actually created a real life selector test that showcases selector matching performance. 实际上有人创建了一个真实的生活选择器测试,展示了选择器匹配性能。

The table outlines that attribute selectors are approximately 3x slower compared to standard classes. 该表概述了与标准类相比,属性选择器大约慢了3倍。

Relying on attribute selectors also requires more characters to target an element. 依赖属性选择器还需要更多字符来定位元素。 It's better to define short and concise class names to keep your stylesheet small. 最好定义简短的类名来保持样式表的小巧。

Example: 例:

// 17 Characters (attribute)
[title="example"] {
 ...
}

// 6 Characters (class)
.title {
 ...
}

http://scope.bitbucket.org/tests/selector-matching-performance/ http://scope.bitbucket.org/tests/selector-matching-performance/

According to this project there is no real performance issue. 根据这个项目,没有真正的性能问题。

This surprised us, since selector performance hasn't been a real concern in our day-to-day work for a while. 这令我们感到惊讶,因为选择器性能在我们的日常工作中已经不是真正关注的一段时间了。 Front-end performance is a huge deal, of course, but CSS selectors appear to contribute such a minuscule amount to the total page load time that it didn't occur to us that it might be a major concern to many people. 当然,前端性能是一个巨大的优势,但CSS选择器似乎对总页面加载时间贡献了这么小的数量,而我们没有发现它可能是许多人主要关注的问题。

They have benchmarks as well. 他们也有基准。

https://github.com/amcss/am-benchmarks https://github.com/amcss/am-benchmarks

There is no performance issue. 没有性能问题。 Both act same. 两者都是一样的。 But there is difference in specificity of the css with class versus Elements. 但是css与class与Elements的特异性存在差异。

Specificity - Specificity determines, which CSS rule is applied by browsers. 特异性 - 特定性决定了浏览器应用的CSS规则。

If two selectors apply to the same element, the one with higher specificity wins. 如果两个选择器应用于同一元素,则具有更高特异性的元素将获胜。

But specificity has hierarchy. 但特异性具有等级。

  1. Inline styles (Presence of style in document). 内联样式 (文档中存在样式)。 An inline style lives within your XHTML document. 内联样式存在于XHTML文档中。 It is attached directly to the element to be styled. 它直接附加到要设计样式的元素上。 Eg 例如
  2. IDs (# of ID selectors) ID is an identifier for your page elements, such as #div. ID (ID选择器的数量)ID是页面元素的标识符,例如#div。
  3. Classes, attributes and pseudo-classes (# of class selectors). 类,属性和伪类 (类选择器的数量)。 This group includes .classes, [attributes] and pseudo-classes such as :hover, :focus etc. 该组包括.classes,[attributes]和伪类,例如:hover,:focus等。
  4. Elements and pseudo-elements (# of Element (type) selectors). 元素和伪元素 (元素(类型)选择器的数量)。 Including for instance :before and :after. 包括例如:before和:after。

Source: http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/ 资料来源: http//coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/

Hence div.test {} is more specific. 因此div.test {}更具体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM