views:

1667

answers:

3

Hi there,

Does anyone know of a good resource to explain good naming conventions for HTML ID and classes and whether to prefix with IDs with an element type i.e. btn or button or similar?

Should classes be plural or singular? I get that IDs should be singular due to them being unique, but what about classes?

IDs and classes should use nouns, right?

I am using pages that inject other pages in the existing pages, kind of like partial pages ... hence... I was wondering if anybody as prefixed a name in front of IDs and/or classes .. kind of like a namespace or similar?

Any comments or insights really appreciated.

+5  A: 

Six revisions has done a good doc on the subject.

http://sixrevisions.com/css/css-tips/css-tip-2-structural-naming-convention-in-css/

SleepyCod
Very good article on the subject.
rpflo
+5  A: 

I wouldn't prefix with the type, as you can infer this in the selector if you must

form#contact {
    property: value;

}

The method you described is known as Hungarian notation, and isn't very popular.

For what you mention, you could place your injected HTML inside one div with one unique class/id, which has a sort of localised reset. Look up CSS selector specificty to ensure your rules will take affect and not other rules in the host page's CSS. See this answer to a similar question regarding styling an element within a parent page.

alex
agreed (don't add the type)... however there is a catch. In old versions of IE, you couldn't style some types of input elements different than others (without) adding a class to certain types. e.g. an input type="text" vs. an input type="button". You would want different styles for each but IE didn't support the input[type=button]{...} syntax
scunliffe
Are you talking about IE6?
alex
A: 

A lot of people don't realize you can do this stuff:

.awesome {
 /* rules for anything awesome */
}

div.awesome {
 /* rules for only awesome divs */
}

button.awesome {
 /* rules for any awesome buttons, but not awesome divs */
}

div.awesome h1 {
 /* rules for H1s inside of any div.awesome */
}

div.awesome>button {
 /* rules for immediate children buttons (not grandchildren+) of div.awesomes */
}
rpflo