cssorganization

Managing CSS Explosion


I have been heavily relying on CSS for a website that I am working on. Right now, all the CSS styles are being applied on a per tag basis, and so now I am trying to move it to more of an external styling to help with any future changes.

But now the problem is that I have noticed I am getting a "CSS Explosion". It is becoming difficult for me to decide how to best organize and abstract data within the CSS file.

I am using a large number of div tags within the website, moving from a heavily table-based website. So I'm getting a lot of CSS selectors that look like this:

div.title {
  background-color: blue;
  color: white;
  text-align: center;
}

div.footer {
  /* Styles Here */
}

div.body {
  /* Styles Here */
}

/* And many more */

It's not too bad yet, but as I am a beginner, I was wondering if recommendations could be made on how best to organize the various parts of a CSS file. I don't want to have a separate CSS attribute for every element on my website, and I always want the CSS file to be fairly intuitive and easy to read.

My ultimate goal is to make it easy to use the CSS files and demonstrate their power to increase the speed of web development. This way, other individuals that may work on this site in the future will also get into the practice of using good coding practices, rather than having to pick it up the way I did.


Solution

  • This is a very good question. Everywhere I look, CSS files tend to get out of control after a while—especially, but not only, when working in a team.

    The following are the rules I myself am trying to adhere to (not that I always manage to.)

    Building sensible classes

    This is how I like to build sensible classes.

    I apply global settings first:

    body { font-family: .... font-size ... color ... }
    a { text-decoration: none; }
    

    Then, I identify the main sections of the page's layout—e.g. the top area, the menu, the content, and the footer. If I wrote good markup, these areas will be identical to the HTML structure.

    Then, I start building CSS classes, specifying as much ancestry as possible as long as it is sensible, and grouping related classes as closely as possible.

    div.content ul.table_of_contents 
    div.content ul.table_of_contents li 
    div.content ul.table_of_contents li h1
    div.content ul.table_of_contents li h2
    div.content ul.table_of_contents li span.pagenumber
    

    Think of the whole CSS structure as a tree with increasingly specific definitions the further away from the root you are. You want to keep the number of classes as low as possible, and you want to repeat yourself as seldom as possible.

    For example, let's say you have three levels of navigational menus. These three menus look different, but they also share certain characteristics. For example, they are all <ul>, they all have the same font size, and the items are all next to each other (as opposed to the default rendering of an ul). Also, none of the menus has any bullet points (list-style-type).

    First, define the common characteristics in a class named menu:

    div.navi ul.menu { display: ...; list-style-type: none; list-style-image: none; }
    div.navi ul.menu li { float: left }
    

    then, define the specific characteristics of each of the three menus. Level 1 is 40 pixels tall; levels 2 and 3, 20 pixels.

    Note: you could also use multiple classes for this but Internet Explorer 6 has problems with multiple classes, so this example uses ids.

    div.navi ul.menu#level1 { height: 40px; }
    div.navi ul.menu#level2 { height: 20px; }
    div.navi ul.menu#level3 { height: 16px; }
    

    The markup for the menu will look like this:

    <ul id="level1" class="menu"><li> ...... </li></ul>
    <ul id="level2" class="menu"><li> ...... </li></ul>
    <ul id="level3" class="menu"><li> ...... </li></ul>
    

    If you have semantically similar elements on the page—like these three menus—try to work out the commonalities first and put them into a class; then, work out the specific properties and apply them to classes, or, if you have to support Internet Explorer 6, ID's.

    Miscellaneous HTML tips

    If you add these semantics to your HTML output, designers can later customize the look of web sites and/or apps using pure CSS, which is a great advantage and time-saver.

    Note that this assigning of multiple classes as outlined in the example above does not work properly in IE6. There is a workaround to make IE6 able to deal with multiple classes. If the workaround is not an option, you will have to set the class that is most important to you (item number, active or first/last), or resort to using IDs.