Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Over the years I have assigned values to classes and html elements in all sorts of ways:

  • myClass
  • MyClass
  • my_class
  • my-class

. . .and so on. As I am always trying to hone my craft and make my code more readable for others, I am going to discipline myself to stick to one convention moving forward. I am leaning towards all lower case and hyphenated, but that is a purely subjective choice.

So I wanted the input of the SE community--is there a standard or recommended convention already in place?

Thanks!

share|improve this question
1  
I think jquery's use of my-class (which may be based on prior libraries?) has become quite common. I don't suspect there's any convention though, but I'm no UX guy. In fact, now that I mention it, I'm going to flag this for migration to UX.SE... (don't cross post, let moderators move it) –  Jimmy Hoffa Sep 6 '13 at 18:06
    
I don't think it should be moved to UX (there's about USER experience), not code conventions. Maybe webmasters... –  RMalke Sep 6 '13 at 19:12
    
Yeah, this has absolutely nothing to do with UX. Half of the UX designers I know don't even know CSS, let alone write it, let alone have specific preferences about how to structure markup and style sheets. –  Aaronaught Sep 6 '13 at 20:32

1 Answer 1

up vote 4 down vote accepted

There is a CSS Lint, although it's more about the structure of CSS than things like hyphenation of class names. That's a personal preference and really needs to be a judgment call based on your environment.

For example, if you're working exclusively with a JavaScript framework like Knockout or AngularJS, hyphenation is the norm. On the other hand, if you're working with a control-based framework like ASP.NET Web Forms, hyphenation of certain things (IDs and names especially) can be incredibly dangerous because they're invalid on the server side.

As far as other things like whether open braces go on a new line or not, it's the same holy war that it's always been. Pick a convention that works for you and/or your team and stick with it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.