ID Attribute

J

JohnW-Mpls

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?
 
R

richard

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?

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".
 
M

Michael Fesser

..oO(JohnW-Mpls)
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?

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.

Micha
 
N

Nik Coughlin

JohnW-Mpls said:
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?

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.
 
D

dorayme

"Nik Coughlin said:
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.

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
classes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top