Use Hyphens in Your CSS Naming Conventions
Why does this matter?
Consistency is one of the most important factors of writing maintainable, easy-to-use CSS. Not just for yourself but for the future saps who will have to dig into your code after you. Using hyphens is a great way to improve consistency, and to also take advantage of some the following benefits that you don’t receive through with camelCase or underscores.
-
CSS is already a hyphen-happy syntax
margin-left
,padding-top
,font-size
, etc. We already write CSS with hyphens. It's natural and consistent to use hyphens for our CSS declarations and our HTML class names and IDs. It is inconsistent to make our class names and IDs camelCase while making our declarations with hyphens. -
It is easier to scan
Single line CSS makes it much easier to scan our CSS. Class names and IDs with hyphens further improve this “readabilty” and “scannablity”. Of the following, the bottom with hyphens is easier to read. 1
.navHome a { ... } .navAbout a { ... } .navPortfolio a { ... } .navContact a { ... } .nav-home a { ... } .nav-about a { ... } .nav-portfolio a { ... } .nav-contact a { ... }
-
Using hyphens allows you to easily take advantage of CSS attribute selectors
Hyphens are such an industry standard now that the CSS spec has certain patterns to select hyphen-separated-attributes. See The CSS2 “pipe” pattern
[class|="col"]
and the CSS3 “star” pattern[class*="col-"]
Currently you should normally avoid attribute selectors due to selector speed. At times though they may extremely come in handy, such as for form elements.
input[type="text"]
orinput[disabled]
Another place they could be used is in our grid.css. Each grid column currently has to have 2 classes.
ancCol
to make it be a column and a width declaration likew50
. Rather than having to putting 2 classes onto the column element, we could simply put one class (such ascol-50
) and then use the attribute selector[class*="col-"]
to apply the necessary styles. This is how Chris Coyier recommends on his grid. -
Everybody is doing it
Mock if you will. Lots of the major companies and web gurus both use hypens themselves as well as recommend to others to do the same. True, you shouldn’t always “follow the crowd” in all aspects of life, but guess what - that is exactly how the web industry works. Enough people start doing something, it eventually becomes a standard.
-
Lastly, transitioning is possible
Currently the majority of Ancestry uses camelCase class names and IDs with some underscores and hyphens sporadically mixed in, and it would be a daunting task to try to transition all of our current pages from camelCase into future-happy-hyphenation in a single sprint. Luckily though, we are already setup with a versioning system so we don’t have to do this all at once.
We would setup new versions of each of the global files (which we are already partially doing anyway with the latest changes to the standards). New pages would point to the new styles. Old pages will point to the old files until an update is needed on that page. At that time we would transition out the old styles just like we are currently doing with the deprecated files. Within 6-12 months, 90% of the old pages are transitioned, and more importantly, all new pages are using one standard syntax.
Conclusion
Use hyphens in your class names and IDs to improve readability, take advantage of CSS attribute selectors, and help out future Ancestry developers.
Recommended by the “Big Guys”
- Google Recommends Hyphens http://google-styleguide.googlecode.com/svn/trunk/htmlcssguide.xml?showone=ID_and_class_name_delimiters#ID_and_class_name_delimiters
- Github Recommends Hyphens https://github.com/styleguide/css
- Wordpress Recommends Hyphens http://make.wordpress.org/core/handbook/coding-standards/css/
- Web Designer Depot Recommends Hyphens http://www.webdesignerdepot.com/2009/05/10-best-css-practices-to-improve-your-code/
- Even Some Random Guy on a Blog Recommends Hyphens http://csswizardry.com/2010/12/css-camel-case-seriously-sucks/
Other “Big Guys” who use hyphens:
- Twitter Bootstrap - http://twitter.github.com/bootstrap/components.html
- Stack Overflow - http://cdn.sstatic.net/stackoverflow/all.css?v=65bf65b1790b
- Smashing Magazine - http://coding.smashingmagazine.com/wp-content/themes/smashingv4/stylesheets/main.min.css?ver=1358860727
- YAML - http://www.yaml.de/docs/index.html