views:

498

answers:

8

Sorry if this is waaayyy to basic of a question to be asked here. But here goes...

Ok so in another question something was being discussed, and this link was mentioned:

https://developer.mozilla.org/en/Writing_Efficient_CSS

In that article, they say some things I didn't know, but before I ask about them, I should ask this... Does that apply to CSS interpreted by Firefox? Forgive my noobness, but I wasn't sure what they meant by Mozilla UI. (don't hurt me!)

If it does apply, when they say:

Avoid the descendant selector!

The descendant selector is the most expensive selector in CSS. It is dreadfully expensive, especially if a rule using the selector is in the tag or universal category. Frequently what is really desired is the child selector. The use of the descendant selector is banned in UI CSS without the explicit approval of your skin's module owner.

* BAD - treehead treerow treecell { }
* BETTER, BUT STILL BAD (see next guideline) - treehead > treerow > treecell { }

The descendant selector is just a space? And then what would the difference be between child and descendant? Child is an element inside another, but isn't that the same as descendant? OH! Shit as I'm writing I think I might have figured it out. A descendant could be a child/grandchild/great-grandchild/etc? And child is only one deep?

Sorry again for the stupid level of my question... just wondering, because I have been constantly using descendants in my CSS for my site. But yeah, if this isn't about Firefox then this whole question is pointless...

If its not about Firefox, does anyone have a link to an article explaining efficiency for Firefox or Browsers in general?

+6  A: 

A descendant could be a child/grandchild/great-grandchild/etc? And child is only one deep?

Yes, exactly. Since a child can only be one deep, there's a much smaller space that the rendering engine has to recursively search to check if the rule matches or not.

And yes, that article is about both Firefox and browsers in general. Most (all?) of what is in it applies to any page rendering engine.

Amber
+1  A: 

A "parent > child" is only one step down, whereas an "ancestor descendant" could be one or more steps down.

Even better is to use "#id" tags wherever possible such that there is less DOM searching.

Bryan Denny
+1  A: 

The UI CSS is for styling the internals of the browser - the settings dialog, extensions interfaces etc.

Descendants and children are different, children are much more specific and result in much less having to be considered.

Rich Bradshaw
+2  A: 

...as I'm writing I think I might have figured it out. A descendant could be a child/grandchild/great-grandchild/etc? And child is only one deep?

Indeed.

One thing I can add on the efficiency side of things is: Don't use * unless you really mean it. It's pretty intensive as rules go and most people could get away just specifying the elements they really want to target.

Oli
I did learn that one while adding a reset. But thanks for reiterating! I didn't even realize some of those universals described in that article were possible, but maybe that was for the better hehe.
Ian Storm Taylor
@Oli: I know it was a direct quote, but the profanity got flagged so I edited it out. Ridiculous, I know, but I don't think it takes anything away from your answer.
Bill the Lizard
+1  A: 

The problem with the child selector is that it's not as well supported. Of course, this might've been fixed on newer IE browsers.

In any case, when writing CSS for a webpage it isn't going to be that big of a deal. I doubt the fractions of seconds you'd save in page load would even be noticed. This article seems more directed towards people writing stuff for the actual browser, not websites.

Karl
+4  A: 

First of all - the suggestions in this article are not for html pages - they are specifically for the Mozilla UI - XUL, so it may be best practice for XUL, but not for html.

Applying the CSS on an average HTML page is one of the quickest things than happen while loading the page.
Also, the article may suggest the fastest way to apply css rules, but at what cost? For example, they suggest not having more than one class per rule:

BAD - .treecell.indented { }
GOOD - .treecell-indented { }

That is almost outrageous. It may lead to quicker CSS, but who cares? Assuming you already have .treecell and .indented, following these suggestions leads to complicated logic, harder maintenance, duplicated css rules, harder JavaScript (which costs a lot more that CSS), etc.
They suggest not using the full richness of CSS selectors and replacing these selectors with flat classes, which is a shame.

Kobi
Actually, the article gives "treecell.indented" not ".treecell.idented" as "BAD". It's giving a generic example of something like "p.indented" and the point is "don't qualify class-categorized rules with tag names". It doesn't say anything about having more than one class per rule.
pw
@pw - You are correct. I can't explain how this happened, but the article has enough examples of what people do daily on CSS, that isn't recommended on XUL. With your permission, I'll leave the answer as is. :)
Kobi
+1  A: 

O'Reillys "Even Faster Web Sites" has a whole chapter on this entitled "Simplifying CSS Selectors". It references your link on Mozilla.

I think two points are worth bearing in mind.

  1. Yes, if you did this as far as possible, your HTML and CSS would be a mess of styles and possibly even more inefficient due to added file size. It is up to the developer to pick the best balance. Don't agonize over optimizing every line as you write it, get it working then see what can be beneficial.

  2. As another commenter noted, it takes the browser milliseconds to figure it out how to apply your styles on page load. However, where this can have much bigger impact is with DHTML. Every time you change the DOM, the browser re-applies your whole style sheet to the page. In this scenario many inefficient selectors could make a visible impact to your page (perceived lagginess/ unresponsiveness).

Matt Turner
A: 

The documentation for Google's Page Speed (a Firefox/Firebug add-on) includes a good page on efficient CSS.

pw