CSS child vs descendent selector

后端 未结 1 992
忘掉有多难
忘掉有多难 2021-01-04 05:45

I read that using the child selector (>) in css is faster than the descendant selector (). For example: p > em as opposed to

相关标签:
1条回答
  • 2021-01-04 06:48

    Theoretically the child selector will be faster than the descendant selector because the browser can stop checking child nodes after the first level. However, I suspect that any performance enhancement you see from this will be negligible as browsers parse CSS quickly in the first place.

    As NullUserException pointed out, the selector doesn't work in IE6, so if you care much about IE6 I wouldn't load your CSS with it. But a good thing to keep in mind is that you should have a very clear idea of which to use in which situation. Ask yourself, "do I want this declaration to cover all matching children, or do I want it to cover only direct matching children?" It may seem obvious to ask yourself such a question, but it really is the only way you should choose between the two. Don't use > unless you mean it.

    Also see my question Is there an advantage to using very specific selectors in CSS?

    See also: CSS selector support per browser

    0 讨论(0)
提交回复
热议问题