Should you use ID or class?

There is a lot of debate about whether you should use classes or IDs. Your decision should be based on the following:

1. Repeated use within a document

So, if you need to use the same specific selector more than once, classes are a better choice.

2. Combining class selectors

You can use multiple classes to style an HTML element but you can only use one ID when styling an HTML element. This means that class selectors have a wider range of applications. An example of combining classes would be:

<p class="highlight indent">

.highlight { font-weight: bold; }
.indent { padding-left: 2em; }

3. IDs have higher specificity than classes

There may be times when a declaration conflicts with another declaration. These conflicts are resolved using the Cascade rules. In simple terms, if a class selector and ID selector were to be in conflict, the ID selector would be chosen.

Inheritance »

Other Max Design articles and presentations
Associated with webstandardsgroup.org