Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- ID Attribute
February 10, 2008, 5:45 pm
rate this thread
Reading a message in here, I noticed the use of the attribute id
(as in <div id="xxx">). I use css but mainly as a template for
similarity of all pages at a site.
For special colors or something like that for a word in a HTML
page, I have been using <div class="xxx"> if the color is
predefined in my .css file, and <div style="xxx"> when I need to
spell out something special.
Why and when would it be good to use the id attribute?
Re: ID Attribute
Personal preferences actually.
Just remember that a class can be used any number of times on the same
page, as to where an ID can only be used once.
So if you had
<div id="xxx">text row one</div>
<div id="xxx">text row two</div>
would not be allowed.
Where it may be allowed, using a class and ID name as the same, I
refrain from doing so to avoid possible confusion.
Such as ID="xxx" and class="xxx".
Re: ID Attribute
When an element is unique. You can only legally assign an id to a single
element. Also, an element can have multiple class values (seperated by
space) but only one id.
Additionally ids have a higher specifity than a class. That is, if you
apply both a class and an id to an element, the css for the id will overrule
that for the class.
As an example, you will probably only ever have one page footer but you may
have content arranged in such a way that you also have section footers. So
one pageFooter, many sectionFooters:
Generally I ask myself, am I *ever* going to possibly need more than one of
these? If the answer is even very tentatively maybe then I use a class. If
I am sure that it will only ever be used once, then I use an id.
Note that by using selectors to their fullest you can get away with using
very few id/classes in your CSS.