css: planning your stylesheet

the more we rely upon css, the larger and more complex stylesheet files become.

planning and organising your stylesheet is essential to creating a lean, manageable website. there are many ways of organising css code but the following are best practice…


comment your stylesheet

commenting your stylesheet makes it much easier to find the information or the commands you’re looking for.

meaningful comments include:

  • last updated– this information can quickly let developers know about recent changes made to the file:/* webcredible
    updated: thu 1 jan 2008
    author: john doe
    updates: add new section ‘forum’
  • references– comments can also be used as a quick reference for the main style guides applied throughout the site:/* colors
    body background: #ffffff
    main text: #000000
    link: #f0f0f0
    etc …
  • organisation– use comments to identify the different sections of the stylesheet:/* =header
/* =footer


  • reminders and notes– leaving reminders and notes for yourself and other developers can help avoid confusion later:/* the width is overwritten for ie 6 in: cssie.css */
     div {width: 150px;


define general rules and main classes at the top of the stylesheet

set the styles of generic html elements, for example:

 background: #fff;
 font: arial, sans-serif 75%;
 h1 {
 font-size: 1.2em;
 color: #000;
 h2 {
 font-size: 1em;
 color: #f0f0f0;
 img {border: 0;
then, list the classes that will be most commonly used across the site, for example:
 position: absolute;
 left: -9000px;
 .required {
 background: url(#) no-repeat 100% 0;
 float: left;
 float: right;


order the css in the same order as the html

the order of the html should be used to determine the order of the css sections. css files can sometimes be large and commands difficult to find. having some correlation between the html and css file makes it easier to locate how an element is being styled.


know when to use elements, ids and classes

using the correct selector type means your css file can be significantly reduced in size:

  • elements– elements such as body, (), paragraphs, () and headings, (,etc.) should be used to define general rules
  • ids– these are unique identifiers and should only be used once within a document. ids should be used to style major structural sections of a web page such as the header or the footer.
  • classes– these can be used on any type of html

too many ids or classes can overload the html and the css files unnecessarily. try and define as many rules as possible by referencing elements and/or ids by nesting the selectors.

imagine the following html code:

because each of the list items has a common parent, descendant selectors can simplify the css markup as follow:

#nav { properties listed here }
#nav li { properties listed here }
#nav li a { properties listed here }


name classes and ids logically

don’t name classes and ids based on their color or position as these may change in time. try and give them a name that’s likely to remain relevant over time. also, use hyphens ahead of underscores as certain old browsers have a hard time understanding the latter. use a common naming system for your classes and ids. it will save a lot of time and confusion when developing, debugging and updating documents.


nest css selectors

by nesting css selectors (i.e. using more than one css selector in one command) we can apply styles by navigating the html document tree. for example, to apply a colour of red to all paragraphs within a div, we can use the following rule:

div p {color: red;}


take advantage of inheritance

some css commands inherit from their parents whereas others don’t. the use of nesting means you don’t have to declare the same properties over and over again.

generally speaking, text-related css commands (e.g. font-size, color) are inherited by descendant elements, whereas layout-related commands (e.g. float, width) aren’t inherited.


group selectors with common css declarations

you can avoid specifying the same set of properties several times by grouping the selectors that share the same css declarations together. for example:

h1, h2, h3
 color: black;
 padding: .2em;