3

It is wide known that overusing id css rules considered to be a bad practice. There are bunch of posts written on this topic - here is one of the latest, for example.

In such posts it is often mentioned that css selectors are actually parsed by browser engines from right to left. That is, if we, say, have selector #very div then, browser first find all divs, then test them for having parent with this very id.

My question consists of two related parts. First part - is it really true? There are lot of implementations of css engines, both opensourced and proprietary, it would be nice if somebody who knows something about any of these engines, shed the light on this topic.

Second part of question is - if it true, if we really always parsing css rules from right to left, why we actually doing so? I am not a parsing expert at all, but wouldn't it be more handy to define which part of selector is more specific (generates lesser output) and to start filtering not necessarily from right? Is it too pricey or something?

5
  • Ummm, no, #very div finds divs and searches up their document trees for an element with ID "very". Commented Jul 20, 2011 at 14:18
  • @BoltClock about what exactly css engine/engines are you talking? Besides, there is a second part of the question ) Commented Jul 20, 2011 at 14:20
  • My point is, you're confusing div#very with #very div. Commented Jul 20, 2011 at 15:12
  • 1
    Related: stackoverflow.com/questions/5797014/… and stackoverflow.com/questions/3851635/css-combinator-precedence Commented Jul 20, 2011 at 15:13
  • @BoltClock Oh, I see. No,actually, I'm not, but I've edited my question to be more clear. Commented Jul 20, 2011 at 15:14

1 Answer 1

0

Seteve Sounders did some tests a while ago and while his test confirm this (at least for tested browsers) it also shows the scale of performance gains. For 6000 dom elements the maximum performance boost using classes vs descendant was 200ms (quite significant) with average being way smaller (and way less significant).

This test also shows that while there are differences between browsers and engines (interestingly ie performance best) the trend is the same.

Sign up to request clarification or add additional context in comments.

1 Comment

@Litek, very useful information, indeed, nevertheless, not an answer to both part of the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.