CSS Style Best Practices

When writing CSS styles, the following rules make for easy code management and more efficient processing.

#id
Only reference one ID name in a style.
Good: #breadcrumbs { … }
Bad: #content #breadcrumbs { … }

tag.class
When reference classes, prefix them with the tag they are applied to.
Good: ul.features { … }
Bad: .features { … }

tag
If a style can be applied to the tag without a separate class or ID, then just style the tag. e.g. a { … }

Other good practices are to

  • use a CSS reset
  • use Modernizr to accommodate non-supporting browsers
  • name your styles symantically (according to type of content, not the style), e.g. feature-links as opposed to red-links