HighDots Forums  

IDs versus Classes

Cascading Style Sheets Layout/presentation on the WWW (comp.infosystems.www.authoring.stylesheets)


Discuss IDs versus Classes in the Cascading Style Sheets forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
AT
 
Posts: n/a

Default IDs versus Classes - 10-17-2003 , 03:49 PM






I'm renewing my interest in CSS and downloaded some stylesheets.
However, the .css uses both an ID and class. It looks like something
like this.

..class1 {
/* CSS code */
}

#styleID1 {
/* CSS code */
}

#styleID2 {
/* CSS code */
}

<DIV Class="class1">some text</DIV>
<DIV ID="styleID1">some text</DIV>
<DIV ID="styleID2">some text</DIV>

..class1 is not part of any other known tag, so why wouldn't one simply
use IDs throughout? Basically, what is it that classes can do that IDs
can't? Thanks.
-- Michel

Reply With Quote
  #2  
Old   
Gerhard Fiedler
 
Posts: n/a

Default Re: IDs versus Classes - 10-17-2003 , 08:42 PM






On 17 Oct 2003 13:49:54 -0700, Michel wrote:
Quote:
Basically, what is it that classes can do that IDs can't?
The formidable Brian already explained the essentials. I just wanted
to add that the question is not quite right... ids and classes are two
different things -- it's like asking "what can a color do that an
address can't?" (both of a house, for example).

Ids are unique (within a page) identifiers of elements. Classes are
groups of elements -- elements that have something in common.

It may happen that you look at a document and you see only one single
instance of a class. But maybe that class gets used multiple times in
other documents that use the same stylesheet, or there were multiple
instances and modifications removed all but one, or the designer
planned for the future and saw the possibility of later modifications
introducing multiple instances.

Or maybe the designer simply decided that in this case, the associated
style was not a feature of _one_ unique element, but something that
could be a feature of a class of elements -- no matter whether there
actually are multiple instances of that class in the document.



Reply With Quote
  #3  
Old   
Stan Brown
 
Posts: n/a

Default Re: IDs versus Classes - 10-18-2003 , 03:20 PM



In article <13689b68.0310171249.3579f10f (AT) posting (DOT) google.com> in
comp.infosystems.www.authoring.stylesheets, Michel
<m.tresfort (AT) att (DOT) net> wrote:
Quote:
Basically, what is it that classes can do that IDs
can't?
class="x" may be used many times in a given document. ID="x" must
not be used more than once in any document.

--
Stan Brown, Oak Road Systems, Cortland County, New York, USA
http://OakRoadSystems.com/
HTML 4.01 spec: http://www.w3.org/TR/html401/
validator: http://validator.w3.org/
CSS 2 spec: http://www.w3.org/TR/REC-CSS2/
2.1 changes: http://www.w3.org/TR/CSS21/changes.html
validator: http://jigsaw.w3.org/css-validator/


Reply With Quote
  #4  
Old   
Kris
 
Posts: n/a

Default Re: IDs versus Classes - 10-18-2003 , 04:01 PM



In article <MPG.19fb6a84dce67ea698b5bb (AT) news (DOT) odyssey.net>,
Stan Brown <the_stan_brown (AT) fastmail (DOT) fm> wrote:

Quote:
Basically, what is it that classes can do that IDs
can't?

class="x" may be used many times in a given document. ID="x" must
not be used more than once in any document.
Also, the ID attribute can be addressed by CSS which will have a higher
authority over styles associated with classes. ID labeled elements can
also be easily targeted by JavaScript.

Just two practical advantages I thought worth mentioning.

--
Kris
kristiaan (AT) xs4all (DOT) netherlands (nl)
"We called him Tortoise because he taught us" said the Mock Turtle.


Reply With Quote
  #5  
Old   
AD.
 
Posts: n/a

Default Re: IDs versus Classes - 10-19-2003 , 09:10 PM



On Sat, 18 Oct 2003 16:20:06 -0400, Stan Brown wrote:

Quote:
In article <13689b68.0310171249.3579f10f (AT) posting (DOT) google.com> in
comp.infosystems.www.authoring.stylesheets, Michel <m.tresfort (AT) att (DOT) net
wrote:
Basically, what is it that classes can do that IDs can't?

class="x" may be used many times in a given document. ID="x" must not be
used more than once in any document.
And an element can also have more than one class eg: class="x y".

Cheers
Anton


Reply With Quote
  #6  
Old   
CJM
 
Posts: n/a

Default Re: IDs versus Classes - 10-20-2003 , 09:11 AM



I think the other guys have this thoroughly covered, but I thought of a
little addition that might be worth considering...

It may be the case, in this example, that the styles are used only once, and
that specificity is not an issue. In which case, the author could as easily
opted to use IDs throughout. However, the author may well be planning
ahead... he may have further pages coming that will use these styles again
and again. Better he build this in now, than to have to go back and changes
his IDs to Classes later on.

On the other hand, I might be talking out of my pants, and the situation is
simply that the author has make an arbitrary decision to simply use Classes
in this example!

Chris

"Michel" <m.tresfort (AT) att (DOT) net> wrote

Quote:
.class1 is not part of any other known tag, so why wouldn't one simply
use IDs throughout? Basically, what is it that classes can do that IDs
can't? Thanks.
-- Michel



Reply With Quote
  #7  
Old   
Stan Brown
 
Posts: n/a

Default Re: IDs versus Classes - 10-20-2003 , 02:16 PM



In article <pan.2003.10.20.02.10.47.233673 (AT) privacy (DOT) net> in
comp.infosystems.www.authoring.stylesheets, AD. <me (AT) privacy (DOT) net>
wrote:

Quote:
And an element can also have more than one class eg: class="x y".
Yes, but MSIE doesn't do the right thing. I can't remember whether
it ignores all the classes or all after the first.

--
Stan Brown, Oak Road Systems, Cortland County, New York, USA
http://OakRoadSystems.com/
HTML 4.01 spec: http://www.w3.org/TR/html401/
validator: http://validator.w3.org/
CSS 2 spec: http://www.w3.org/TR/REC-CSS2/
2.1 changes: http://www.w3.org/TR/CSS21/changes.html
validator: http://jigsaw.w3.org/css-validator/


Reply With Quote
  #8  
Old   
Steve Pugh
 
Posts: n/a

Default Re: IDs versus Classes - 10-20-2003 , 03:36 PM



Stan Brown <the_stan_brown (AT) fastmail (DOT) fm> wrote:

Quote:
In article <pan.2003.10.20.02.10.47.233673 (AT) privacy (DOT) net> in
comp.infosystems.www.authoring.stylesheets, AD. <me (AT) privacy (DOT) net
wrote:

And an element can also have more than one class eg: class="x y".

Yes, but MSIE doesn't do the right thing. I can't remember whether
it ignores all the classes or all after the first.
Which version of MSIE?

I can't remember what IE4 did but IE5+ applies all the classes. Try it
and see for yourself.

Steve

--
"My theories appal you, my heresies outrage you,
I never answer letters and you don't like my tie." - The Doctor

Steve Pugh <steve (AT) pugh (DOT) net> <http://steve.pugh.net/>


Reply With Quote
  #9  
Old   
Stan Brown
 
Posts: n/a

Default Re: IDs versus Classes - 10-20-2003 , 04:56 PM



In article <cph8pvg7oab1geduia6stja8gild7vesu9 (AT) 4ax (DOT) com> in
comp.infosystems.www.authoring.stylesheets, Steve Pugh
<steve (AT) pugh (DOT) net> wrote:
Quote:
I can't remember what IE4 did but IE5+ applies all the classes. Try it
and see for yourself.
I'll be happy to take your word for it. I know this has come up in
the past and I was warned off multiple classes because _some_
browser in fairly common use screws that up.

I's like to try MSIE5, but I can't because Microsoft won't let us
have multiple versions of MSIE installed on the same machine.

--
Stan Brown, Oak Road Systems, Cortland County, New York, USA
http://OakRoadSystems.com/
HTML 4.01 spec: http://www.w3.org/TR/html401/
validator: http://validator.w3.org/
CSS 2 spec: http://www.w3.org/TR/REC-CSS2/
2.1 changes: http://www.w3.org/TR/CSS21/changes.html
validator: http://jigsaw.w3.org/css-validator/


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.