Shoutbox

CSS - id vs class - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: CSS - id vs class (/showthread.php?tid=90981)

CSS - id vs class by stoshrocket on 06-08-2009 at 06:23 PM

Something I never really looked too deeply into, all I know that an id and class use different predeceeding symbols in the style sheet (eg #id_name and .class_name), but what's the actual difference and where would you use one over the other? When I first started to use CSS I just used id because it's shorter and easier to type, and so I rarely use class, but for no real reason...


RE: CSS - id vs class by Eljay on 06-08-2009 at 06:26 PM

http://lmgtfy.com/?q=CSS+-+id+vs+class


RE: CSS - id vs class by gif83 on 06-08-2009 at 06:31 PM

As far as I know classes are for things that turn up time and time again so you can have different things with the same class.
IDs are for individual things as it's used as some sort of identifier for that particular thing.


EDIT:
Yeah, go read Matti's post, it's far more eloquently put.


RE: CSS - id vs class by Matti on 06-08-2009 at 06:40 PM

IDs have to be unique, there can only be one element in the document tree with a certain ID. Classes can be used multiple times throughout the tree and you can apply any amount of classes on one element.

In practice, you'd use IDs for unique things in your layout such as #page-container, #header, #content, #sidebar and #footer whereas classes can be used for things which are used more than once like .post, .comment, .alignright, .smallicon etc. At least, that's how I use them. :P


RE: CSS - id vs class by stoshrocket on 06-08-2009 at 07:46 PM

Yeah, cool... Good to know, although to be honest I've never seen an instance where using id for multiple objects on a page caused a problem, the only reason I can see is for validation purposes...

Thanks for the info anyways! :happy:


RE: CSS - id vs class by NanaFreak on 06-08-2009 at 08:50 PM

ids are used more in javascript, because they are meant to be only used once...