Click here to Skip to main content
13,052,870 members (76,919 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked
Posted 29 Mar 2008

Planning your stylesheet - the definitive guide

, 29 Mar 2008
Rate this:
Please Sign up or sign in to vote.
Don't let your stylesheet files get out of control - follow these guidelines right from the start and you'll easily be able to manage and update your CSS files.

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<br />
                    Updated: Thu 1 Jan 2008<br />
                    Author: John Doe<br />
                    Updates: Add new section 'Forum'<br />-------------------------------------------*/

  • References - Comments can also be used as a quick reference for the main style guides applied throughout the site:

    /* COLORS<br />
                      Body Background: #ffffff<br />
                      Main Text: #000000<br />
                      Link:    #F0F0F0<br />
                      etc ...<br />

  • 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 */<br />
                div {width: 150px;}

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

Set the styles of generic HTML elements, for example:

body<br />
{<br />
background: #fff;<br />
font: arial, sans-serif 75%;<br />
}<br />
h1 {<br />

font-size: 1.2em;<br />
color: #000;<br />
}<br />
h2 {<br />
font-size: 1em;<br />
color: #f0f0f0;<br />

}<br />
img {border: 0;<br />

Then, list the classes that will be most commonly used across the site, for example:

.hide<br />
{<br />
position: absolute;<br />
left: -9000px;<br />
}<br />
.required {<br />

background: url(#) no-repeat 100% 0;<br />
}<br />
.fl<br />
{<br />
float: left;<br />
}<br />
.fr<br />

{<br />
float: right;<br />
}<br />

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:

  1. Elements - Elements such as body, (<body>), paragraphs, (<p>) and headings, (<h1>,<h2> etc.) should be used to define general rules
  2. 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.
  3. Classes - These can be used on any type of HTML element.

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:

<ul id="nav"><br />
<li><a href="#">Item 1</a></li><br />


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

#nav { properties listed here }<br />
#nav li { properties listed here }<br />
#nav li a { properties listed here }<br />

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<br />

{<br />
color: black;<br />
padding: .2em;<br />

This article was written by Brigitte Simard. Brigitte's crazy about accessibility and CSS - so crazy that she works for Webcredible, an industry leading user experience consultancy, helping to make the Internet a better place for everyone. She's very good at running CSS training and spends much of her time working on the world's most accessible CMS.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Trenton Moss
Web Developer
United Kingdom United Kingdom
Trenton Moss is crazy about usability and accessibility - so crazy that he founded Webcredible, an industry leading user experience consultancy, to help make the Internet a better place for everyone. He's very good at information architecture and interaction design.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 29 Mar 2008
Article Copyright 2008 by Trenton Moss
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid