Just remember that when you use class selectors, you don’t have to define
each particular tag as blue in your CSS file. You instead define a “highlight”
class having a bluevalue for its colorproperty, and then just use class=
”highlight”throughout your HTML code whenever you want anyelement
to be blue.
If you want to add comments within a CSS file, enclose the comment between
/*and */ symbols. The purpose of commenting your code is to make it
easier for you to later read and modify it, or for others to modify your code
after you have, say, left the company. You can instantly read the purpose of,
or technique used with, the commented code:
/* This class can be used with any HTML tag */
These symbols are awkward because they’re so similar to each other, but yet
not identical. A variety of other, more easily typed, and more convenient
punctuations could have been chosen. A good choice would have been the
single quotation mark ( ' ). But committees designed CSS, so you get inefficien-
cies from time to time. All in all, CSS is certainly worthwhile, but you might
end up wishing that it didn’t have so many qualities derived from C program-
ming languages. This is the way C programmers add comments to their code,
and C is notorious for being the most difficult, often the most inefficient, of all
computer languages.
Using an ID Selector ......................................................................................
ID selectors are similar to generic classes — they, too, are independent of
specific document elements. But IDs differ from a class in that an ID is sup-
posed to work onlyonce on a give Web page — only a single element (the one
with the ID) is supposed to be affected. Any references further down in HTML
code to an already used ID are supposed to be ignored. Why am I using the
phrase “supposed to” so much here? Because ID selectors don’t work as
advertised. Inpractice, browsers just ignore this “use it only once” rule. For
example, I tried this code in Internet Explorer, and the browser didn’t enforce
the “only once” regulation; all three elements with duplicate IDs get high-
lighted here:
html>
<head>
<style>
#highlight {color: yellow; font-style: italic;}
</style>
</head>
<body>
38 Part I: The ABCs of CSS