View Full Version : difference btw class and id?
nemex
08-20-2002, 08:26 PM
What is the diff btw class and id?
I've read a few books about it but still don't understand. Even an example without explanation would do. Thanks.
redhead
08-20-2002, 08:53 PM
erm... as far as CSS/HTML goes... if your referancing to it using CSS... then not alot.... eg
<span class="aClass">Text</span>
<span id="anId">Text</span>
<style>
.aClass {
color: red}
#anId {
color: blue}
</style>
so... with CSS you would referance an ID with a "#" and a class with a "." .
I'm not so fluent with JavaScript, but I believe (some one correct me if i'm wrong) that classes are alot harder to find with JavaScript, however... they can be changed with it... eg:
<span onmouseover="this.className='class2';">Text</span>
But... not doing huge amounts of JavaScript I wont try to explain any further than I have.
Hope this helps, :thumbsup:
Zvona
08-20-2002, 10:18 PM
Every object (element) has an unique id, but several objects can belong under one class. For example :
John is a mammal, who is an instance of human class, thus John is an object. John has attributes name, age and id. Id is his social security id and is unique - like primary keys in data relations :
<mammal class="human" name="John" age="23" id="140479-115K" />
John has a girlfriend Mary, who is also an instance of human class :
<mammal class="human" name="Mary" age="19" id="160583-242A" />
Mary has a Dog called Spiff, which is also an mammal, but a canine. Canines don't have a social security number, but they has a personal id number and reference to their owner :
<mammal class="canine" name="Spiff" age="6" id="609A" idref="160583-242A" />
All the attributes can be declared in constructors (in object-oriented programming). Since we're dealing with mark-up languages, there's DTD's for that.
Like mentioned above, CSS uses classes and ids for style definitions. In this case, we could have following definitions :
<style type="text/css">
.mammal {head:1;spine:true;}
.human {arms:2;legs:2;}
.canine {legs:4;tail:1;}
#140479-115K {width:60cm;height:178cm;weight:75kg;}
#160583-242A {width:50cm;height:165cm;weight:54kg;}
#609A {width:120cm;height:65cm;weight:12kg;}
</style>
It can be very complicated to determine the difference with pure object-oriented programming and mark-up languages / style languages, so if your uncertain what I'm trying to explain, don't think too much..otherwise you get fully confused :D
tom-mt
08-21-2002, 02:27 AM
I donīt see much difference besides the fact that "class" can be referenced by many elements multiple times while "ID" must occur only once in each page.
adios
08-21-2002, 02:46 AM
http://www.w3.org/TR/html4/struct/global.html#h-7.5.2
Roy Sinclair
08-21-2002, 02:29 PM
Originally posted by tom-mt
I donīt see much difference besides the fact that "class" can be referenced by many elements multiple times while "ID" must occur only once in each page.
An item can be styled by both an id and by a class, they are not mutually exclusive.
Multiple class selectors can be applied to one element as well:
<element class="someclass anotherclass class3 etcclass">bla</element.
MCookie
08-22-2002, 01:02 AM
An id has more importance. The text in the paragraph below will be blue, not red...
<html>
<head>
<title>Untitled</title>
<style type="text/css">
<!--
#blue {color:#0000ff}
.red {color:#cc0000}
-->
</style>
</head>
<body bgcolor="#ffffff">
<p id="blue" class="red">Blue</p>
</body>
</html>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.