According to W3c Recommendation 7.5.2 Element identifiers: id and class attributes :
id - [case sensitive] . This attribute names the element. This name must be unique in the document.
class - [case sensitive] . This attribute assigns a class name or a set of class names to an element. Any number of elements can be assigned the same name or class names. Multiple class names must be separated by space characters.
The id attribute assigns a unique identifier to the element. For example, the following paragraphs differ in id values:
<P id="myparagraph"> This is a uniquely named paragraph.</P> <P id="yourparagraph"> This is also a uniquely named paragraph.</P>
The id attribute has several roles in HTML:
- As a style sheet selector.
- As a target anchor for hypertext links.
- As a means of referencing a specific element from a script.
- As the name of the declared OBJECT element.
- For general-purpose processing by user agents (for example, to identify fields when extracting data from HTML pages into a database, transfer HTML documents to other formats, etc.).
The class attribute, on the other hand, manually assigns one or more class names to an element; an item can be specified to belong to these classes. The name class can be shared by multiple instance elements. The class attribute has several roles in HTML:
- As a style selector (when the author wants to assign a style of information to a set of elements).
- For general-purpose processing by user agents.
The following example uses the SPAN element in conjunction with the id and class attributes to mark up documents. Messages appear in the English and French versions.
<P><SPAN id="msg1" class="info" lang="en">Variable declared twice</SPAN> <P><SPAN id="msg2" class="warning" lang="en">Undeclared variable</SPAN> <P><SPAN id="msg3" class="error" lang="en">Bad syntax for variable name</SPAN> <P><SPAN id="msg1" class="info" lang="fr">Variable déclarée deux fois</SPAN> <P><SPAN id="msg2" class="warning" lang="fr">Variable indéfinie</SPAN> <P><SPAN id="msg3" class="error" lang="fr">Erreur de syntaxe pour variable</SPAN>
The following CSS style rules will tell visual user agents to display informational messages in green, warning messages in yellow, and error messages in red:
SPAN.info { color: green } SPAN.warning { color: yellow } SPAN.error { color: red }
Please note that French "msg1" and English "msg1" may not appear in the same document because they have the same id value. Authors can additionally use the id attribute to refine the presentation of individual messages, make them target anchors, etc.
Almost every HTML element can be assigned an identifier and class information.
Suppose, for example, that we are writing a document about a programming language. The document should include a number of pre-formatted examples. We use the PRE element to format examples. We also assign a background color (green) to all instances of the PRE element belonging to the example class.
<HEAD> <TITLE>... document title ...</TITLE> <STYLE type="text/css"> PRE.example { background : green } </STYLE> </HEAD> <BODY> <PRE class="example" id="example-1"> ...example code here... </PRE> </BODY>
By setting the id attribute for this, for example, we can (1) create a hyperlink to it and (2) redefine the class style information with information about the instance style.