scriptygoddess

12 Sep, 2002

class vs. ID

Posted by: Jennifer In: CSS

I'm a little slow on the uptake with all this CSS stuff, and the longer an article is, the less likely I am to understand it. So when I can, I try to make things simpler.

One concept I have been trying to wrap my head around is the difference between class vs ID. I always specified styles using the attribute class="classname". I could never understand why/when you'd use ID="classname"… that is until today when I read this article on digital-web

So, here's my cliffnotes version "dumbed" down so I could finally understand it.

The basic idea, let's say you have two sections on your site. a header, and a content area. You want to use the H1 style in each section but you want it to "behave" (look) slightly differently depending on which section it's in….

So first you define the sections (in your stylesheet). So, that would look like this:

#header {
//put any generic styling for the div… like background color, etc. if any…
background-color: Olive;
}

#content {
//put any generic styling for the div… like background color, etc. if any…
border : medium dotted;
}

Then you can define the H1 tag as it should appear in those sections:

#header h1 {
//put specific styles for header-h1 in here.
font-family: Arial, Helvetica, sans-serif;
font-size: 24px;
}

#content h1 {
//put specific styles for content-h1 in here.
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
}

Then on your page you put a DIV tag around the "header area", when you use the H1 tag inside that area.. it'll look like the HEADER version. And the same with the content area:

<div id="header">
<h1> HEADER STYLE HEADLINE </h1>
</div>

<div id="content">
< h1 >CONTENT STYLE HEADLINE </h1>
</div>

10 Responses to "class vs. ID"

1 | erica

September 13th, 2002 at 1:24 am

Avatar

Ah I don't blame you, I had to look into it a few months ago, and I still always end up using # instead of . or vice versa.

2 | erica

September 13th, 2002 at 1:36 am

Avatar

What my understanding was;
"The difference is an ID is unique to one area, classes are not. Thus IDs can be used in conjunction with Javascript/DHTML."

Related –
http://bitworking.org/stories/2002/02/16/cssCodingStyle.html

3 | Shawn

September 13th, 2002 at 2:11 am

Avatar

This is actually just a side effect. The real reason for the two is that Microsoft defined ID (and uses it when writing Microsoft-specific COM objects to interface with a web page), while the W3C defined the class attribute with a similar function.

4 | Cis

September 13th, 2002 at 3:37 am

Avatar

For pratical purposes though, using the same ID division more than once in a single page can cause problems with your site resolving properly. The best HTML comparison would be the NAME attribute, I think.

5 | Jennifer

September 13th, 2002 at 8:12 am

Avatar

Can anyone give me a "good" example of when to use IDs over classes? (If there is one?? Or as a general rule should one just use class= all the time and forget the whole ID thing?)

(And Erica brought up another question… when do you use # or . when defining a custom class? – I always use . but you can # and still call it with class="classname".)

6 | kristine

September 13th, 2002 at 9:13 am

Avatar

Here's my example of ID vs Class – In the MT default templates, there's a #content and a #links. These are the 2 main sections of the page (main posts and sidebar), and because they are only referred to once in the html, they are IDs instead of classes.
Personal thought – I think this is a stronger definition than a class definition, when you look at the cascading of the stylesheet. It seems to be more important, and that works well if you are only going to have that section once in the document.

Ahh, and Here's more info on this subject – I knew I'd read more somewhere! This explains what I said a bit better :)

7 | kristine

September 13th, 2002 at 9:15 am

Avatar

Also – when I just set up my newest skin, I used ID's to absolutely position the background images. I figured that I would never use #top1 – #top6 accidently as classnames, and since there was only one of each image, I'd insure that they were the only things that got absolutely positioned the way they did :) I notice that Image Ready does this if you tell it to generate CSS instead of Tables, too.

8 | ste

September 13th, 2002 at 9:42 am

Avatar

Yeah, from what I've read, the main differences between ID and class are that ID is applied only the first time it is used in a document where class can be applied to many different instances. So for example, you could have defined:
#pageHead { properties },
and in your document, only the first <div id="pageHead"> would have the properties specified. And contrasting, you could have defined:
.sectionHead { properties },
and any <div class="sectionHead"> in your document would have the properties. For most uses, I think it's better to use classes, unless you're absolutely certain you're only going to use the ID once (or want to make sure you only use the ID once).

9 | Just Orb

September 16th, 2002 at 12:44 am

Avatar

Simple explanation.
Thanks to Scriptygoddess, I now understand the difference between the CSS tags "div id" and "div class". My CSS is

10 | LawLawLaw: Erik J. Heels

April 28th, 2004 at 12:16 am

Avatar

Movable Type Brain Dump
Tips and tricks I wish I had known when I started using Movable Type.

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements