I'm wondering if there is a performance issue using attribute selectors instead of class selectors.
div.test {}
div[test] {}
P.S. : I'm only interested in CSS performance, not JS.
I'm wondering if there is a performance issue using attribute selectors instead of class selectors.
div.test {}
div[test] {}
P.S. : I'm only interested in CSS performance, not JS.
There is no performance issue. Both act same. But there is difference in specificity of the css with class versus Elements.
Specificity - Specificity determines, which CSS rule is applied by browsers.
If two selectors apply to the same element, the one with higher specificity wins.
But specificity has hierarchy.
Source: http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/
Hence div.test {}
is more specific.
div.test
can't be more specific than div[test]
. –
Stifling According to this article, class selectors are more efficient than attribute selectors.
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.
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/
[title]
, it is just a 1 character difference. the more important thing to consider is the data-
prefix, if you find it's worth using to insure that your website functions the same in case a new attribute with the same name was added to the HTML standard in the future, or if you need to use the HTMLOrForeignElement.dataset readonly property. though with gzip/compression, most likely there won't be a big difference in size/bytes. –
Bechtel 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.
They have benchmarks as well. EDIT: see comment: they have benchmark ideas*, not benchmarks.
Data attributes are slower, but not so much that I would care. Testing it with JS gives me these results:
Test name Executions per second
.....................................
Class 5017198.0 Ops/sec
ID 3808182.0 Ops/sec
data attribute 3289109.0 Ops/sec
https://measurethat.net/Benchmarks/ShowResult/436379
I'm pretty sure that pure CSS is much faster.
There is no performance issue. Both act same. But there is difference in specificity of the css with class versus Elements.
Specificity - Specificity determines, which CSS rule is applied by browsers.
If two selectors apply to the same element, the one with higher specificity wins.
But specificity has hierarchy.
Source: http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/
Hence div.test {}
is more specific.
div.test
can't be more specific than div[test]
. –
Stifling 1
There is no performance issue. Both act same. But there is difference in specificity of the css with class versus Elements.
Specificity - Specificity determines, which CSS rule is applied by browsers.
If two selectors apply to the same element, the one with higher specificity wins.
But specificity has hierarchy.
Inline styles (Presence of style in document). An inline style lives within your XHTML document. It is attached directly to the element to be styled. E.g. IDs (# of ID selectors) ID is an identifier for your page elements, such as #div. Classes, attributes and pseudo-classes (# of class selectors). This group includes .classes, [attributes] and pseudo-classes such as :hover, :focus etc. Elements and pseudo-elements (# of Element (type) selectors). Including for instance :before and :after.strong text
© 2022 - 2025 — McMap. All rights reserved.