Home

Awesome

Writing and refactoring for clean CSS

This note is meant as a guide for writing clean CSS, as well as refactoring bad CSS as you touch code. The problem is that people don't want to touch an old CSS style and break something. So bad styles stick around for a long time. We need to put in the extra effort of getting rid of those bad styles.

General Guidelines

Our highest priorities are:

Readability

Modularity

Performance

  <ul class="social">
    <li><a class="social-link" href="">Facebook</a></li>
  </ul>

Selector Efficiency

Below is the order of efficiency for selectors. Id's are the most efficient and pseudo classes and pseudo elements are the least efficient.

Even though id's are more efficient than classes, the difference is extremely small, so opt for classes when you can. Id's have to be unique, while classes are more flexible and reusable.

  1. id (#myid)
  2. class (.myclass)
  3. tag (div, h1, p)
  4. adjacent sibling (h1 + p)
  5. child (ul > li)
  6. descendent (li a)
  7. universal (*)
  8. attribute (a[rel="external"])
  9. pseudo-class and pseudo element (a:hover, li:first)

Refactoring