When to Use CSS IDs and Classes

Published January 21, 2008 by CSS Newbies.

Trogdor License Plate - photo by emdot

There are three different ways in CSS you can dictate which elements you want to style. Each way is useful for a specific set of purposes, but by using all three together, you can really harness the cascading power of style sheets. The three methods of describing objects on a page are by their tag name, their ID, or their class.

First and most obviously, you can style all of an entire subset of elements by styling a specific tag. For instance, take the following code (please!):

p { color: red; }

This states that all text within a paragraph <p> tag anywhere in your document should be colored bright red, like so. As with most of my code examples, this practice is ill-advised but hopefully illuminating nonetheless. This technique is most useful when you want to change a large number of elements on your page at once.

However, you can also describe elements according to their IDs and their classes. Both IDs and classes are ways of further clarifying which elements you’re planning to alter. So consider the following snippet of HTML:

<p id=”introtext”>This is a snappy introduction.</p>
<p class=”aside”>Okay, so the intro isn’t that great.</p>

Here we have two paragraphs, one of which has an id assigned, and the other which has a class. You could manipulate these two paragraphs independently of one another (and the other regular paragraphs in your document) with code such as this:

p#introtext { font-size: 150%; }
p.aside { font-style: italic; }

IDs are unique and beautiful snowflakes. Classes are not.

The first rule affects our “introtext” paragraph. We select that specific element with “p#introtext,” with a pound sign between the two elements, which means “paragraph tags with an id of introtext.” We’ve set its font size to be 1.5 times the size of a normal paragraph tag.

Our aside, on the other hand, uses a period between the element and the class, which lets the browser know we’re talking about classes instead of IDs. And you don’t even technically need the “p” identifier before the ID or class: styling simply “#introtext” or “.aside” would have worked just as well in this example, but the more specific you are, the less danger you’re in of accidentally styling things you didn’t mean to.

So what’s the difference between an ID and a class, then? It’s pretty simple: IDs are unique and beautiful snowflakes (just like you!). Classes are not.

Think of an ID something like the license plate on your car. It’s an identifier that, presuming you haven’t been a victim of theft, is unique to your vehicle. Similarly, an ID can only be used once per HTML document. So if you have something on your page that you need to be different from everything else, that is a good time to set an ID.

A class, on the other hand, can be applied to multiple elements on the same page. So, going back to our CSS example above, we’re saying that our page will have only one introduction, but can or will have multiple asides. Sort of like this article, when you get right down to it.

Another important distinction between the two is that each element on your page can have only one ID, but can have multiple classes. So you could have HTML like this:

<p class=”aside right”>Was this example really necessary?</p>

You could then have two classes defined in your CSS: one that does something specifically to asides, and another that does something to elements with a class of “right” (for example, it could float them to the right side of the page). Elements with just an “aside” class would only have the “aside” attributes applied, while elements with just the “right” class would just be floated right. But by applying both to one element as we have above, we can create a unique type of element without having to write a lot of extra code. And that’s what CSS is all about.

3 Responses

  1. Kimberly Steele (reply)

    Thank you! I never understood the difference between classes and IDs before. The snowflake analogy helped in a big way . . . actually all of the analogies are helping me to understand CSS a great deal more. Your tutorials are the best.

  2. Narendran (reply)

    Thanks a lot for a great change to have look into p#introtext, which makes a clear definition to clear my clearance. It’s a great deal to know much about CSS………

  3. Prodyot (reply)

    Dear Rob
    Great tutorial.
    I am enjoying it and it is already helping me understand about CSS.
    One request-
    Can you kindly add “Previous” “Next” or name the previous and the next chapter at the bottom of each tutorial page?
    That way readers don’t need to go back to the master page to look up what is the next chapter.
    And, also that way we will end up reading the tutorial in an organised manner.
    Thanks for the tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>