Su Zhang
Su Zhang

Reputation: 2270

Selector Performance - 'E#id' vs '#id' - in CSS and jQuery

When using selectors in CSS and in jQuery, are there any efficiency differences between using'E#id' and #id, where E is any HTML element? If yes, does it only apply to certain layout engines and/or Javascript engines?

Upvotes: 4

Views: 284

Answers (2)

Neeraj
Neeraj

Reputation: 142

1) It's considered that using ID's for CSS is not a good practice. It should be used when manipulating content through javascript.

2) Over qualifying CSS selector with tag name ul.top-nav or ul#top-nav will only increase overhead on browser since it has to match both the tag & class/ID. Hence, avoid over-qualifying the selector.

Upvotes: 0

Naren
Naren

Reputation: 1920

Browsers read the selectors right to left so there is little to be gained by prefixing anything before the id; it is redundant at that point. Source: Writing Efficient CSS from Mozilla

Here's a real world example to test it for yourself. TL:DR; it doesn't seem to matter enough to make a difference.

Relevant further reading from a previous Stack Overflow question

Upvotes: 3

Related Questions