1

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!

3
  • 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) Commented Sep 6, 2013 at 18:06
  • I don't think it should be moved to UX (there's about USER experience), not code conventions. Maybe webmasters...
    – RMalke
    Commented Sep 6, 2013 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
    Commented Sep 6, 2013 at 20:32

1 Answer 1

5

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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