Forum Moderators: not2easy

Message Too Old, No Replies

Difference between Class and id Selectors?

any special situation for either one?

         

mms19

10:50 am on Sep 17, 2003 (gmt 0)

10+ Year Member



I'm new to CSS and don't quite understand the difference between the class and id selectors.

If I define

.myclass {font-family: "verdana";}
#myid {font-family: "verdana";}

the following 2 lines should be the same

<p class=myclass> text </p>
<p id=myid> text </p>

why we need id selector if class selector is well enough? Is there any special situation for better use of id selector?

Thank you.

PCInk

10:56 am on Sep 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



An id should be used only once per page, a class can be used many times. Usually classes are used for styling and id used for programatic purposes or certain html markup (such as the label tag). I think it's fair to say that most people don't use id.

Nick_W

11:04 am on Sep 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I use id's all the time. In fact, I think you're mistaken PCink ;)

Here's a practical example:

Say you have an h1 on every page of your site, (as you should) and you have an id assigned to that h1 like this:

[pre]


h1#myheader {
color: blue;
font: bold 1.4em arial, verdana, sans-serif;
}

..and in the html

<h1 id="myheader">This Is My Blue Title</h1>

[/pre]

All well and good. But, what if, on one page you want that h1 to be green? --- Assign a class aswell!

[pre]


h1#myheader {
color: blue;
font: bold 1.4em arial, verdana, sans-serif;
}
h1.myoddheader {
color: green;
}

...and in your html

<h1 id="myheader" class="myoddheader">My Green Title</h1>

[/pre]

Not the greatest example but I'm sure you get the point...

Nick

TGecho

1:10 pm on Sep 17, 2003 (gmt 0)

10+ Year Member



I almost use id more than class. My main page elements (header, nav, content, footer, etc...) all get an id. Then I use descendent selectors to define "header h1" and so on. I will go through a lot to minimize use of class (without causing more code bloat, of course :).

PCInk

1:36 pm on Sep 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Nick, but that means you can only use that header once per page (maybe of for H1), but what about h2? And what about all the other classes you use? (after all over-riding p,h5,li etc is a class because it can be used more than once)

What benefit is there to using <h1 id=...> instead of <h1 class=...>? Or even <h1> over-ridden by a class in the css sheet?

I don't understand why you say you use id more often, I can't see any benefit to the example you have given. What, for example, if you needed to h1 tags on the same page, just for the odd page? How would you do that, now that you have declared h1 will only ever be used once per page?

your_store

5:05 pm on Sep 17, 2003 (gmt 0)

10+ Year Member



Well, I guess Nick's example is a tad confusing; because, you would never actually have two h1's on the same page. As for changing an id on a certain page, I always include and id and class on my body tag so that I can use decendent selectors for this purpose. If you're h1 were usually black, but you wanted them white on your "section" pages you could use:

h1 {
color: #000;
}

body#section h1 {
color: #fff;
}

Like TGecho, I also use id's for my main page sections, i.e. content, nav bar, etc. This way you can style most elements by using decendent selectors, which keeps your markup clean of extra class declarations. It also helps when reading your CSS; because, all your selectors are clearly named.

Id's also come into play when specificity is concerned. A selector including an id will always trump one that only has a class.

Reflection

5:23 pm on Sep 17, 2003 (gmt 0)

10+ Year Member



Like TGecho, I also use id's for my main page sections, i.e. content, nav bar, etc. This way you can style most elements by using decendent selectors, which keeps your markup clean of extra class declarations. It also helps when reading your CSS; because, all your selectors are clearly named.

Decendant selectors are great. They make your css file much easier to organize and read and they also eleminate the need to have a whole bunch of classes. In one instance I cut the size of one of my css files in half by implementing decendant selectors, just wish I would have been doing this from the start :).

Nick_W

5:36 pm on Sep 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My apologies for botching my example ;)

That should be pretty much the other way around. eg there is a class that defines the basic rules for the h1 but you have one special page that needs to be changed. So, you develope an id for it.

That way, all your h1's can be the same but the one with an id in addition to it's class will be different.

Having said that, I agree with all of the above and never use this in practise.

I do however, consider it good practise to give unique elements such as nav columns etc unique identifiers..

Nick

piskie

5:52 pm on Sep 17, 2003 (gmt 0)

10+ Year Member



I have found cross browser problems with applying css to id when exactly the same css applied using class has been bug free.

PCInk

9:30 pm on Sep 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hmmm, still confused, and I'm sure I'm not the only one!

Surely an id should be only ever used where there should only be one of the item on any given page in any circumstances.

I use (for example):

<label for='name'>
Name:
</label>
<input type='text' id='name'>

Which allow the user to click on the word 'Name:' to move the text caret to inside the input box. i.e. there should only ever be one box called name in this situation, two and the browser would not know what to do.

Other than that, there does not seem to be anything to an id tag that class cannot do.

But back to the example mms gave of using id or class in a <p> tag - if there is only ever one paragraph of that style in a document, he/she could use either id or class. But if there could be more than one instance of it in one given page, he/she MUST use class. Right? (P.S. I am relatively new to CSS and this is one subject I looked up but didn't get too far - class worked fine and I used it for almost everything).

TGecho

11:18 pm on Sep 17, 2003 (gmt 0)

10+ Year Member



That would be correct. ID is for elements that only appear once on a page (like a header div or form input). If there is going to be more than one instance than use class.

If you use the same ID on multiple elements, the results will be unpredictable.

The main advantages of IDs (that come to mind) are that they get priority in the cascade and they can be accessed with javascript. Oh yeah, and it takes three less charecters to apply them than class :)