CSS ID vs Class

  • allgoodpeople
  • Proficient
  • Proficient
  • User avatar
  • Joined: Jan 16, 2004
  • Posts: 379
  • Loc: here
  • Status: Offline

Post February 11th, 2004, 8:32 pm

Regarding class selectors vs id selectors:

when creating a box with CSS is it more appropriate to write that as an ID or as a class? For instance, lets say you have a navigational box that is 200px by 100px on the left side of the screen, only used for your site navigation. Should that box be written as a class or as an ID? Or, does it really matter at all?

Mark
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post February 11th, 2004, 8:32 pm

  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Joined: Jun 15, 2003
  • Posts: 4583
  • Loc: Maryland
  • Status: Offline

Post February 11th, 2004, 8:44 pm

I've decided that ID and class should be used for organization.

Say you have four tables that you all want to have almost identicle looks, except the font, but instead of copy and pasting the css thrice, you can use id's like so:

Code: [ Select ]

<table class="cool_tables" id=1>
</table>
<table class="cool_tables" id=2>
</table>
<table class="cool_tables" id=3>
</table>
  1. <table class="cool_tables" id=1>
  2. </table>
  3. <table class="cool_tables" id=2>
  4. </table>
  5. <table class="cool_tables" id=3>
  6. </table>


That way you can change the minor aspect without having to have three times as much css.
Pixel Acres V2
  • digitalMedia
  • a.k.a. dM
  • Genius
  • User avatar
  • Joined: Dec 29, 2003
  • Posts: 5169
  • Loc: SC-USA
  • Status: Offline

Post February 11th, 2004, 9:19 pm

An ID is typically used for a single instanced container/object, while a class is applied to many objects.



i think.
- dM
  • gsv2com
  • Professor
  • Professor
  • User avatar
  • Joined: Jan 25, 2004
  • Posts: 776
  • Loc: Nippon
  • Status: Offline

Post February 11th, 2004, 10:07 pm

That is exactly right.

ID = One Instance
Class = Multiple Instances
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post February 12th, 2004, 4:32 am

hence the terms,
ID = one student.
Class = many students.
Strong with this one, the sudo is.
  • allgoodpeople
  • Proficient
  • Proficient
  • User avatar
  • Joined: Jan 16, 2004
  • Posts: 379
  • Loc: here
  • Status: Offline

Post February 12th, 2004, 6:55 am

joebert wrote:
hence the terms,
ID = one student.
Class = many students.


well now, isn't that clever! i never would have made that connection, but it does make sense now.

what defines a single instance? Is it a single instance per site or single instance per page?

mark
  • gsv2com
  • Professor
  • Professor
  • User avatar
  • Joined: Jan 25, 2004
  • Posts: 776
  • Loc: Nippon
  • Status: Offline

Post February 12th, 2004, 7:34 am

Per page.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post February 12th, 2004, 10:34 am

Quote:
well now, isn't that clever! i never would have made that connection, but it does make sense now.


Me either lol, this topic made me think of it :oops:
Actually i usally use class for styles and id as javascript reference, EX:
(note i know i forgot some things)
Code: [ Select ]
function updateTitle(newTitle) {
   title.innerHTML = newTitle;
}

<style>
.titlebar {FILTER: alpha(opacity="75"); font-family: Verdana; font-size: 20px; color: #123456;}
</style>

<div class="titlebar" id="title"> First Title </div>

<a href="previews/whatisOZZU.asp" onMouseDown="updateTitle('Got OZZU ?');" target="namedIFRAME">What is OZZU ? </a>
  1. function updateTitle(newTitle) {
  2.    title.innerHTML = newTitle;
  3. }
  4. <style>
  5. .titlebar {FILTER: alpha(opacity="75"); font-family: Verdana; font-size: 20px; color: #123456;}
  6. </style>
  7. <div class="titlebar" id="title"> First Title </div>
  8. <a href="previews/whatisOZZU.asp" onMouseDown="updateTitle('Got OZZU ?');" target="namedIFRAME">What is OZZU ? </a>
Strong with this one, the sudo is.
  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Joined: Jun 15, 2003
  • Posts: 4583
  • Loc: Maryland
  • Status: Offline

Post February 12th, 2004, 2:07 pm

You can still use ID CSS'ing more then once correct? like have two tags with an ID of 5, even though that isn't what it is meant for I am sure it can be used that way.


I think in the Minds of CSS the properties probably go like this:

Tag
Class
ID
Inline


I'll check w3c later...wanna go play some xbox and make a Van deGraaff generator ;)
Pixel Acres V2
  • allgoodpeople
  • Proficient
  • Proficient
  • User avatar
  • Joined: Jan 16, 2004
  • Posts: 379
  • Loc: here
  • Status: Offline

Post February 12th, 2004, 2:14 pm

i'm sure it would work, but i don't think you're supposed to. i wonder if it would validate to use an ID more than once?

Mark
  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Joined: Jun 15, 2003
  • Posts: 4583
  • Loc: Maryland
  • Status: Offline

Post February 12th, 2004, 2:38 pm

W3C Says exactly what we have said, ID must be unique, that doesn't mean it wont work, but it wont be to standards.

http://www.w3.org/TR/REC-html40/struct/ ... -and-class
Pixel Acres V2
  • RichB
  • Guru
  • Guru
  • User avatar
  • Joined: May 17, 2003
  • Posts: 1121
  • Loc: Boston
  • Status: Offline

Post February 13th, 2004, 4:04 am

I tried it just to see what would happen, and it works with CSS, but the document won't validate if an id is used more than once within that document. In other words the style information is valid, but the html containing the multiple instances of the same id is not.

Another potential problem is that it won't work properly if you use javascript to access elements referred to by the same id. If there is more than one unique instance, only the first instance will be used and the others will be ignored (at least in IE6, Moz 1.5, and Opera 7).

I generally use class with CSS, since I may want to apply it to a different element within the same document at a later time, and id with unique elements that I want to access through code. The exception for me has been using css for layout, which is something I am learning. I have started using id with css for elements that are unique within a document by their nature (i.e. a column like "mainContent").

It would be poor programming practice to use the same id more than once in a document because by definition it is supposed to refer to a unique instance. You may also decide to access that id through code at a later time and get unexpected results. If you stick to the standards then you can use the W3C validator to spot errors in your code, which I think is extremely handy.
Free Programming Resources
  • LazyJim
  • Student
  • Student
  • No Avatar
  • Joined: Feb 15, 2004
  • Posts: 92
  • Status: Offline

Post February 15th, 2004, 10:17 am

I avoid using both as much as I can.
Use the cascade!
  • allgoodpeople
  • Proficient
  • Proficient
  • User avatar
  • Joined: Jan 16, 2004
  • Posts: 379
  • Loc: here
  • Status: Offline

Post February 15th, 2004, 11:36 am

LazyJim wrote:
I avoid using both as much as I can.
Use the cascade!


care to elaborate for those of us who are n00bs to CSS? :D

mark
  • LazyJim
  • Student
  • Student
  • No Avatar
  • Joined: Feb 15, 2004
  • Posts: 92
  • Status: Offline

Post February 15th, 2004, 1:29 pm

allgoodpeople wrote:
care to elaborate for those of us who are n00bs to CSS? :D


of course (although I'm a n00b too)!

The idea is to use the seclector part off each CSS rule to it's fullest potential...
for example, if all your links should be blue-on white in the body of your site, but white-on-blue in the navigation menu, most n00bs (and I think most WYSIWYG editors) would use add class="..." for EVERY link in the nav section, but if you can identify the nav section then you can set rules for any element that is inside the nav section, and so in your HTML code the links all look the same:
Code: [ Select ]
<html>
<head>
    <STYLE>
        a { /* Set the style for links */
            color: blue;
        }
        #nav { /* Give the nav section a blue border so you can see it easily */
            border: solid blue 2px;
        }
        
        #nav a { /* Set the style for links inside the nav section */
            color: white;
            background-color: blue;
        }
    </STYLE>
</head>
<body>
    <DIV id="nav">Navigation menus are best marked up in a list, but here is just some text and <a href="#">link</a> and some more text...
    </DIV>
body text body text... including a <a href="#">link</a> and more text...
</body>
</html>
  1. <html>
  2. <head>
  3.     <STYLE>
  4.         a { /* Set the style for links */
  5.             color: blue;
  6.         }
  7.         #nav { /* Give the nav section a blue border so you can see it easily */
  8.             border: solid blue 2px;
  9.         }
  10.         
  11.         #nav a { /* Set the style for links inside the nav section */
  12.             color: white;
  13.             background-color: blue;
  14.         }
  15.     </STYLE>
  16. </head>
  17. <body>
  18.     <DIV id="nav">Navigation menus are best marked up in a list, but here is just some text and <a href="#">link</a> and some more text...
  19.     </DIV>
  20. body text body text... including a <a href="#">link</a> and more text...
  21. </body>
  22. </html>
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post February 15th, 2004, 1:29 pm

Post Information

  • Total Posts in this topic: 17 posts
  • Users browsing this forum: No registered users and 137 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.