tags:

views:

104

answers:

7

Which one between

.my_css_class{}

... and ...

.my-css-class{}

is the best way to go and why? Is it just best practice or is there another reason?

+7  A: 

Either is OK - I would pick whichever one you (and your colleagues) find to be the most readable. You will find neither gains nor losses with either one, it is purely an aesthetic choice.

But whatever you do - once you pick a style - stick with it. Consistency is key for decisions like this.

Andrew Hare
+3  A: 

I prefer hyphens to underscores since underscores are an extra keystroke--that's how trivial you can be since it makes no difference, and in my opinion they're both equally readable.

Andrew Noyes
+2  A: 

The one reason to go with the underscore style is that it's consistent with the naming of variables and types in other languages (C*,Java, etc). I think that it's a bit better to go with something familiar so that it's more natural to read the source (and faster).

Otherwise, the other suggestions given sound reasonable as well. Maybe have quick chat with your team and go with what works best for everyone.

Dana the Sane
I do something very similar--I use the same naming conventions I use for variables/members in Java, JavaScript, PHP, etc... camelCase!
Andrew Noyes
I'm with Andrew on this one (upvoted the comment); camelCase seems more consistent with everything else I do.
David Thomas
I didn't say anything about camel case because it wasn't in the question, but I agree.
Dana the Sane
+3  A: 

I prefer underscores because in Vim - are considered word separators and it's extra work to highlight the entire name.

apphacker
I agree with you, it's the same in a lot of IDEs
marcgg
+2  A: 

I agree with everyone here - neither one is better, but choose one and stick with it.

Personally I use hyphens for the same reason Andrew does, they're easier to type. However I also use underscores for jQuery class targeting.

So I can have ".modal-popup" for styles.

And then I can have "._rollover" for targeting and applying functionality to specific elements without worrying about applying unwanted styles. "._rollover" will never have any style information applied to it.

It's kind of a hack, but a good way to get around the limit amounts of selectable attributes in HTML.

Mike Robinson
I'd consider that a great idea, not a hack! I will take this into consideration on my next project. Could lead to more reusable semantic class names and less headache in the long run.
Mark Hurd
+3  A: 

I would go with the hyphens. Because there are browsers that don’t understand underscores in class names as they were not yet permitted in CSS 1.

Gumbo
Who still use browsers like that? It must be like 0.0x% of the market share?
marcgg
A: 

If you use hyphens, you can use the |= attribute selector to select a group of classes based on the first portion of the class name:

/* Select open folders */
.folder-open { }
/* Select closed folders */
.folder-closed { }
/* Select both kinds */
*[class|="folder"] { }

Browser have varying support for that kind of selector.

Rob Kennedy
You can't use that if you use underscores?
marcgg
@Marc, apparently not, according to the linked page (www.w3.org/). However the CSS 3 (I think '3'), allows the use of *[class^="folder"], which does (at least 'much') the same thing, so far as I can tell.
David Thomas