ID Attribute

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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


Quoted text here. Click to load it

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


Quoted text here. Click to load it

For example if you want to access or manipulate that element with
JavaScript or if you want to use it as a named anchor. IDs are also
often used for the main parts of a webpage, like #nav, #head, #content
etc. But that's more or less just personal preference.


Re: ID Attribute

Quoted text here. Click to load it

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:

#pageFooter {

.sectionFooter {

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.

Re: ID Attribute

Quoted text here. Click to load it

Well said. I tend this way too. By using an id in such cases, it
is information to oneself (and other possible mark up source
sticky beaks). One can be sure it was put in to mark something
uniquely thus avoiding mistaken paths in troubleshoots over


Site Timeline