If I have a structure like this:
<div id="main">
<div id="sidebar">
<div id="tag-cloud"/>
</div>
</div>
… what’s best practice for css:
#main #sidebar #tag-cloud { ... }
#tag-cloud { ... }
Or if id='main' was instead class='main', would it be worse to scope? I’m just wondering, if you have ids, do you need to scope at all?
Depends. Most of the time, you could say:
is the best (performance-wise, see below). This:
just does a lot of unnecessary checks that are bound to succeed.
Unless you want to do this:
in which case scoping defines a different look depending on where
#tag-cloudis. Of course there can be only one#tag-cloudin your page, but your CSS could handle both cases anyway.CSS checks are done from right to left. This:
Evaluates to: “The element with ID
tag-cloudthat has a parent with IDsidebarthat has a parent with IDmain. If that’s how your site looks anyway, that’s just a whole lot of useless DOM traversal.Not to speak of the fact that with over-specific, superfluous scoping the whole CSS must change in a lot of places if you modify your site structure, which somehow defies the purpose.