Supported by :   My Law Forms Ltd. - Deed polls and Tenancy Agreements

ID vs Class

One common question when working with XHTML and CSS is whether to use the id or class attribute on an element. There are several rules that you should consider when using either attribute.

  1. Will the selector be used more than once within a document?
  2. Do the selectors need to be combined?
  3. Does the specificity matter?

If the answer to question number one or two is yes, then you need to use a class. Classes can be used more than once in a document whereas id’s cannot. A neat little CSS trick is that you can combine classes, for example:

<div class="news highlight">CSS is the best!</div>

Lastly, id’s have a higher specificity than classes, which means that the styles for the id element will be given priority over the styles for a class.

You can also use both a class and id on a single element in order to gain greater control over the element.

This entry was posted on Friday, February 16th, 2007 at 11:32 am and is filed under HTML help, XHTML tips. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed.