views:

78

answers:

6

One of the guys here at work puts the tag name in front of all his CSS selectors for element ids. For example:

div#footer {

}

This, as opposed to just:

#footer {

}

His rationale is that this is a quicker lookup for most browsers because they don't need to check the id attributes of every type of element--just div elements. He also points out that this should be done with classes (e.g. div.header-label for <div class="header-label"... elements).

Sounds reasonable and rational, but is it true?

+2  A: 

It probably isn't true; the layout engine is likely to build a hashtable.

However, it definitely will not make a noticeable difference, unless your page has an insane number of elements. (In which case you've got worse problems)

You should use whichever selector more more readable.
Also, remember that someone might change #footer to a <span>.

SLaks
I wonder if Jon Skeet or someone would know for sure the answer to this question at least as far as Chrome is concerned.
JohnB
@JohnB: Go read the WebKit source. (and good luck)
SLaks
A: 

I personally don't think that performance could be an issue here, unless many styles needs to be changed dinamically.

In any case the two examples are semantically different since #footer will cover cases which div#footer won't. So aside from personal taste we're talking about two different things. Then if you are really using ID as they should be used there's no difference at all..

Jack
It won't cover any /valid/ cases though, since ID's should be unique regardless of the tags they are on.
mkoistinen
yes, but I'm used to see every kind of thing about CSS good constraints so it's better to be sure, in any case I specified that now!
Jack
+1  A: 

The only time this would be a useful practice, IMHO, is when you have multiple tag types using the same ID, which is of course, invalid anyway. However, if you were given a piece of invalid HTML/CSS to start with, and couldn't fix it properly, using TAGTYPE#ID might be useful.

mkoistinen
+10  A: 

Your co-worker is wrong, it's exactly the opposite:

But also have in mind that CSS optimization is micro-optimization and finally just irrelevant in most cases:

Dave
+1 for the nice document reference!
mkoistinen
google link: *"Prefer class and ID selectors over tag selectors."* I wish I had asked this question 3 years ago! **:(** I thought it was good to use tag selectors as much as possible; my CSS philosophy is backwards! Great question Foobarbis and great answer Dave!
JohnB
A: 

Attaching the tag to the id is more specific. You can create an id in your css that you can apply to ANY element, but if you attach the tag name you can ONLY use those rules for THAT (unless you list other elements, classes or id's with a comma) tag with THAT id see:

http://www.w3.org/TR/CSS2/selector.html#id-selectors

d2burke
A: 

It's hard to imagine a valid use for this CSS "feature".

Suppose a form has a year field.

input#year {
    width: 4em;
}

This would prevent the rule from applying if, for instance, the field were rendered as a select.

As for the performance factor, I think @Dave does the best debunking.

harpo
So, i'm playing devils advocate here:What would happen if that same ID was used in another page of a massive application in a separate tag? Say you wanted to use that ID to refer to a value in jQuery no matter the element type, but as far as display goes, you want to be sure to style it a certain way DEPENDING on the element type?
d2burke
Like I said, it's hard to imagine.
harpo