tags:

views:

214

answers:

6

In creating CSS styles one approach seems to be a fully qualified style such as

#pnlImage div.ipd-imageInfo div.ipd-tags span.ipd-tag

compared to a shorter definition such as

div.ipd-tags span.ipd-tag

which would uniquely identify the style as well. However, if the site is expanded or changed the 2nd style runs the risk of not uniquely identifying the element.

Is there a performance hit from fully qualifying a style, i.e., it is longer? Are there some guidelines/best practice references for style naming?

Thanks

+4  A: 
  1. Looking up an #id is fast.
  2. Looking up a tag is a bit slower.
  3. Looking up a .class is the slowest.

Starting your selectors with a faster lookup will reduce the number of lookups required for then next part. That is, if I wrote p.myClass, then the browser can very quickly find all the p tags and then it only has to loop through those to check for the class name.

That said, it would rate the maintainability of your CSS file higher than its rendering speed. Blah blah blah premature optimisation blah blah.

nickf
My implementation doesn't work like that. The algorithm that I implemented is "for each element in the DOM, see which rules are selected (i.e. get from the ruleset the rules whose selectors match the element), and then apply the cascade algorithm". So any simple selector (tag, class, ID) takes about the same small amount of time (to test whether or not it matches the DOM element in question). Compound selectors take longer (take a time that's linearly proportional to the number of ancestors which the DOM element has).
ChrisW
*your* algorithm? are you writing your own CSS engine?
nickf
Yes I am. But this is the algorithm which seemed to me suggested by the specs: the API between my HTML renderer and my CSS parser is like http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-ViewCSS which I invoke for each element in the DOM (as a performance optimization, for sufficiently-similar DOM elements the CSS parser returns results from cache). Are you really saying that the algorithm used by other browser implementations is "for each CSS rule, find DOM elements" instead of "for each DOM element, find CSS rules"? And if so do you have any idea why they'd do it like that?
ChrisW
So... Why is _your_ algorithm relevant to the real world, here?
jason
@jason nickf seemed to me to be saying that browsers implement a certain algorithm (i.e. he seems to be suggesting that as part of using a given rule a browser might "very quickly find all the p tags, and then it only has to loop through those to check for the class name"). It's an algorithm that surprises me. At a minimum I'd like to query/double-check whether he's really saying that (all? most?) other browsers implement the algorithm he stated.
ChrisW
The Youtube video (which describes Mozilla) agrees with your conclusion: that "performance depends on the right-most part of the selector being as specific as possible". And, sorry, I think I misunderstood you: when you said "find all the p tags" I thought you meant "find the p tags in the DOM", but presumably you meant "find the CSS rules whose selectors match a DOM element with a p tag".
ChrisW
well, I did mean that (the first one, "find the p tags in the DOM"), but it wouldn't be the first time that I've been wrong...
nickf
A: 

I don't see how a theoretical answer is possible: the answer is implementation-dependent; so I suggest you profile it to be sure.

ChrisW
A: 

Is there a performance hit from fully qualifying a style, i.e., it is longer?

Yes, on every dynamic change of the DOM-tree, the CSS-expression has to be rematched against at least some nodes. I doubt this will lead to any noticeable delay, though.

Your stated objective (making the selectors robust against changes to the page structure) is not quite solid: hardcoding intricate details about the site structure into the CSS will just mean that you'll have more statements to maintain and update when the page structure changes.

If it's under your control, stick with simple classes (even if you have more of them) and as few levels as possible (doing some relative sizing of fonts is the only use case where I have used several levels, and even this was somewhat superfluous). It just wastes too cognitive capacity to keep track of the page structure in your head.

pmf
+2  A: 

Google (not a search, actually them) seems to think that it does cause a performance hit.

Also, the Mozilla foundation has an article "Writing Efficient CSS for use in the Mozilla UI" that outlines the possible performance pitfalls of CSS selectors in their browser(s), and how to optimize your style rules for their rendering engine. Their article has tons of examples of what's good and what's bad. Please keep in mind this is only relevant to their browsers, though.

There are also some benchmarks publicly available, about CSS selectors affect on rendering speeds:

I, however, think this is, for the most part, horse manure. You can effect FAR greater change on your page's loading/rendering speed by using some other simple optimizations. I believe that your sanity, and a well-organized code base should come first. If this were as big of a deal as some make it out to be, we'd all be back using HTML < 4 attributes (bgcolor=, border=, etc) to style our web pages.

jason
+1  A: 

You might be interested in David Baron (Mozilla)'s Google Tech talk.

Ms2ger
+1 very relevent. The description of CSS selector matching starts at about time 0:15:40.
ChrisW
A: 

Although your question is about the performance, (and I would suggest, measure it..) I would really like to add that you should always try to use the shortest definition possible to identity the correct elements.

The reason is not the file size, but the ability to extend your site without altering the main css.

For example you've got this part in your html site:

<div id="Header">
    <h1>Css example</h1>
    <h2>Welcome to the css example site</h2>
    <p>An example page by davy</p>
</div>

and this is the css:

#Header 
{
    background-color: #ffeedd;
    padding: 1em;
}
#Heading h1
{
    font-size: 3em;
    color: #333;
}
#Heading h2
{
    font-size: 1.5em;
    color: #666;
}
#Heading p
{
    margin: 0 0.5em 1.5em 1em;
    font-size: 1.1em;
    color: #999;
}

And later on you'd get to a page where you'd like your header to have a different background.

Had you chosen to use div#Header in you main css file, you'd either have to change the html (which depending on your system might mean creating a different template/masterpage) to add an extra class, or create a more qualified css selector such as body div#Header.

Using the shortest selector you could still use div#Header { background : ... } to change your header. You can either create an extra css file and load that into your header on that page (if allowed) or add a style definition directly to your <head> section. The nice thing about this is your css file does not grow with selectors for each different page, and you can keep clear of classitis.

You could also use it to switch the sizing method of your page (static/fluid) so that one template/masterpage uses the default css, and the other derives from that template/masterpage and just links a css called FluidWitdth90.css to change the template to 90% width fluid layout.

Davy Landman
Just to make sure I understand you correctly, the example you provide is the preferred approach? Rather than the longer fully qualified style I used, make them shorter and more specific to only that element so they can be replaced?
ChrisP
My rule is to always use the least specific selector possible. So you'd have about 3/4 times to override the styling with other css files by being more specific.
Davy Landman