Write Better CSS With 'Idiomatic CSS'

Large projects with many programmers contributing can result in tangled code, but style guides like Idiomatic CSS offer a way out of the confusion.
Image may contain Animal Sheep and Mammal
Wooly, the CSS Sheep Image: W3C

If you've ever worked on a large programming project you know all about the joy of trying to read other people's code. And of course that's how everyone else feels about reading your code. That's why formal programming style guides exist – to help bridge the gap between individual styles.

There is no right or wrong style of writing code, but there are styles that are easier to read and share with other people. Search the web and you'll find formal guides to writing readable JavaScript, Python, Ruby and countless other popular languages, but one language that doesn't get as much attention is CSS.

Developer Nicolas Gallagher wants to change that. To do so Gallagher has put together Idiomatic CSS, a style guide for how to format, organize and craft quality CSS that anyone can work with. Here are the general principles of the project:

"Part of being a good steward to a successful project is realizing that writing code for yourself is a Bad Idea™. If thousands of people are using your code, then write your code for maximum clarity, not your personal preference of how to get clever within the spec." — Idan Gazit

  • All code in any code-base should look like a single person typed it, no matter how many people contributed.
  • Strictly enforce the agreed upon style.
  • If in doubt use existing, common patterns.

Idiomatic CSS follows in the footsteps of Rick Waldron's Idiomatic JS, which does the same thing for JavaScript.

If you've made the leap to a CSS preprocessor like SASS or LESS, fear not, Idiomatic CSS has you covered as well. Preprocessor syntax varies and Idiomatic CSS offers examples in SCSS, but the more general rule, "your conventions should be extended to accommodate the particularities of any preprocessor in use," apply to others as well.

Wrangling CSS on large projects can be a pain, but if you take the time to create a set of conventions and ensure that everyone sticks to them it becomes a much more manageable task. If you've got experience and insight to share, head on over to the Idiomatic CSS GitHub page and contribute your knowledge.