posted April 11, 2008  

Have you ever wondered what the difference between an “id” and a “class” was? You probably haven’t, but I’m going to talk about it anyway.

There’s not much to explain on this topic.

If a div is given an “id,” it is meant for that div to be unique. What I mean is, the div with that id should be declared only once on a page.

If a div is given a “class” instead, it is meant that the div is generic. There may be more than one div of the same class in the same page.

For example, the usual container div would be classified with an “id” because there’s only one container div on the page. Things like input boxes or buttons would be classified as a “class” because forms could have any number of input boxes and buttons.

This is just the way the CSS standards are set up; and if you think of it, it kinda makes sense.

Of course, you could have more than one div with the same id. And of course, you could have one div with a class. Frankly, your page would look the same if you switched “id”s with “class”es.

To me, “id” and “class” are just a way of better organizing things in your CSS.

filed under: general

tags: , ,


comments:
Nick

I like your use of the word “generic” to describe classes. But let’s not forget that IDs are “unique”.

“Of course, you could have more than one div with the same id.”

Doing this would render in most browsers. However, your web page fails W3C validation if you use the same ID more than once.

leave a comment:
Copyright © 2008-2009 csswoes.com • Bryan Duran • All Rights Reserved