So
what??™s the difference?
STYLIN??™ WITH CSS - CHAPTER 2 44
The Difference Between IDs and Classes
So far, I have shown aspects of classes and IDs that might make
them seem to be interchangeable??”we have used them both to identify
a speci?¬? c tag within our markup. However, an ID is more powerful
than a class, rather like the queen is more powerful than a pawn
in a game of chess. (You will see just how true this is when you look
at the concept of rule speci?¬? city in the ???The Cascade??? section later
in this chapter.) This is because, according to the rules of XHTML,
only a single instance of a particular ID (such as id="mainmenu")
can be in a page, but a class (such as class="strongparagraph") can
appear many times.
So, if you want to identify a unique piece of your page??™s markup,
such as the main navigation menu to which you want to target a
special set of CSS rules, you might use an ID on a div (division element)
that encloses the menu??™s elements.
To identify a number of special paragraphs in a page that all require
the same variation of styling from the basic paragraph, you would
use a class.
As an aside, you also use an ID to enable JavaScript to be targeted at
a tag (for example, to activate a DHTML animation when the user
mouses over a link). You JavaScript jocks might like to know that
the id attribute replaces the deprecated name attribute (which the
XHTML validator ?¬‚ ags as invalid) for this purpose.
Pages:
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75