May 8th, 2006

Giving Markup Some Class

One of the great things about CSS is the ability to give elements a class or id. The trouble is, like many other elements of XHTML/CSS, they can be abused. I know when I first began using CSS, I would give just about anything a class just because I could. I would then style elements based on their class, completely disregarding semantics. What resulted was cluttered markup filled with far too many class declarations, and a stylesheet that could use a trim.

<h1 class="main">Lorem Ipsum</h1>
<h2 class="level2">Fusce tellus</h2>
<p class="standout">Nam hendrerit. Vivamus mollis ipsum ac nisl. Fusce adipiscing. Nunc nec felis at neque vestibulum bibendum. Nulla commodo sollicitudin erat. Praesent feugiat.</p>
<h2 class="level2">Vestibulum at velit</h2>
<p class="regular">Vivamus mattis nunc quis metus. Nunc lobortis. Vestibulum sed tellus. Duis auctor.</p>

Coming Up With a Solution

While completely valid, markup like that degrades the stability of your code. When every element is given a class, you as the developer will need to continually remember to assign each and every element that class when you want the associated style applied.

A Recommendation

Instead, I recommend taking a step back and looking at the parent elements. Chances are there is a container element such as a div that definately deserves its own class and has one. You can use this to your advantage and have the container element act as a support for your style. Use the parent element to style the children.

div.content h1 { font-size:2.2em; }
div.content h2 { font-size:1.7em; }
div.content p { margin-bottom:1em; }
div.content p em { background-color:#efefef; }

Using CSS such as that would allow for the following markup:

<div class="content">
<h1>Lorem Ipsum</h1>
<h2>Fusce tellus</h2>
<p><em>Nam hendrerit. Vivamus mollis ipsum ac nisl. Fusce adipiscing. Nunc nec felis at neque vestibulum bibendum. Nulla commodo sollicitudin erat. Praesent feugiat.</em></p>
<h2>Vestibulum at velit</h2>
<p>Vivamus mattis nunc quis metus. Nunc lobortis. Vestibulum sed tellus. Duis auctor.</p>
</div>

Now, instead of markup cluttered with classes and a stylesheet with excessive code, we have a more streamlined version which is easier to follow and also easier to maintain.

In Closing

It is those circumstances when the style you wish to apply to an element is unique in nature when a specific class or id should be used. That way it will only make an effective appearance in the markup and in the stylesheet.

 Comments

8 Comments

  1. Fredrik Wärnsberg May 8th, 2006

    Small typo on the both examples, you open a <h2> but close it with a <h1>

    Nice writeup, but I do hope that most people have this figured out already :)

  2. trovster May 8th, 2006

    I think for content an ID should be used, as there is usually only one content-container on the page, therefore ID is appropriate.

    Your last markup example doesn’t show the containing div.content :S

    I’m using classes now to markup Microformats

  3. Derek Punsalan May 8th, 2006

    Funny that you posted this article / tip today. Over the weekend I was scratching my head wondering why I was adding classes for <h2>, <h3>, and <p> elements when using the parent would suffice. Doing so not only prevents “degrading the stability” of your code, but also keeps your markup a little lighter.

  4. jammo May 8th, 2006

    aside from minor mistakes others have caught, i’ll focus on the lesson of todays article:
    great read again!
    i too used to give every container a classtag.
    now i dont.
    your method proves it is simple.

  5. P.J. Onori May 8th, 2006

    Very good point. The less code you need to write to accomplish something, the better. This is pretty much universal for all programming.

    The less classes, results in less code, which (usually) makes it less complicated, which allows you to accomplish more, which allows you to leave work eariler, which allows you to have more time for hobbies, which makes your life happier.

    See, the less classes you write makes your life happier! Why wouldn’t you head Jon’s advice? ;)

  6. Pedro Rogério May 9th, 2006

    Until at last I obtained to understand this!