Page 1 of 2
class id class id ???
Posted: Tue Oct 18, 2011 2:49 pm
by zelouzelou
I'm still having a hard time understanding the difference between an id and a class, and when to use each.
Can someone please explain?
Re: class id class id ???
Posted: Tue Oct 18, 2011 7:32 pm
by smith206
More of less it is up to you. They both basically do the same thing. There are a few differences though. IDs can only be applied to one specific object within your page so each id you use needs to be unique. Classes can be applied to as many objects as you want. So the advantage of a class is you can change multiple objects with one line of css where with ids you would need to style each id individually. There are some cool tricks you can do with css using both ids and classes but it gets quite technical.
Re: class id class id ???
Posted: Wed Oct 19, 2011 5:17 pm
by zelouzelou
Hmmm interesting. So is it kind of like if I wanted to style just one specific sentence within a webpage, I might use an id, whereas if I have a whole bunch of sentences scattered throughout the webpage that I want to style the same way, I might use a class and style it all at once?
Thanks Kyle!
Re: class id class id ???
Posted: Wed Oct 19, 2011 6:36 pm
by Instructor
zelouzelou wrote:Hmmm interesting. So is it kind of like if I wanted to style just one specific sentence within a webpage, I might use an id, whereas if I have a whole bunch of sentences scattered throughout the webpage that I want to style the same way, I might use a class and style it all at once?
Thanks Kyle!
Yes and no. Both instances you're describing would probably best be filled by class styles. IDs typically are used for things like div tags and images. You might use an ID targeting a particular div to style the type within it, though.
I always think of class styles as precision instruments, use for a letter, a word, or line of type, or maybe a single image or div tag. IDs and particularly tag styles are blunt instruments that require less interaction, but give less control.
Re: class id class id ???
Posted: Wed Oct 19, 2011 9:03 pm
by zelouzelou
ohh I think I need to play with them more.
thanks Michael
Re: class id class id ???
Posted: Thu Oct 20, 2011 2:26 pm
by Ravana
Instructor wrote:zelouzelou wrote:Hmmm interesting. So is it kind of like if I wanted to style just one specific sentence within a webpage, I might use an id, whereas if I have a whole bunch of sentences scattered throughout the webpage that I want to style the same way, I might use a class and style it all at once?
Thanks Kyle!
Yes and no. Both instances you're describing would probably best be filled by class styles. IDs typically are used for things like div tags and images. You might use an ID targeting a particular div to style the type within it, though.
I always think of class styles as precision instruments, use for a letter, a word, or line of type, or maybe a single image or div tag. IDs and particularly tag styles are blunt instruments that require less interaction, but give less control.
Not to challenge the instructor or anything, but personally, I look at them differently (IDs and classes); in fact, the exact opposite. To me, IDs are precision instruments which you would use on one thing and one thing only, whereas classes are for styling a bunch of things, and therefore more widely used. I think maybe the difference in our line of thought is looking at it from an amount of content perspective vs. a number of elements perspective? /shrug In the end, it doesn't much matter as you'll get your styles to work either way.
Re: class id class id ???
Posted: Thu Oct 20, 2011 6:02 pm
by Lundholm
An id can only be assigned to one thing on a page. A class can be assigned to more than one thing on a page.
So if you want to assign a CSS value to more than one thing on your page, for example there's TWO things you want to assign width:45px to, it might be better to assign them to a class and assign width:45px to that class. But if only one thing on each page has a certain css value, then it's always better to use an id, though even then sometimes I use a class so that I can add something to that class later if it helps my layout.
Does that make sense?
I've been told in the past that sometimes my explanations are confusing. If so I'm sorry.
Re: class id class id ???
Posted: Thu Oct 20, 2011 6:06 pm
by Lundholm
I think the best way to look at ID's and classes is that eventually when you learn how they work and how they differ, each designer comes up with his/her own way to use them and I don't think anyone's wrong as long as the site validates properly and looks the way it's intended to.
Re: class id class id ???
Posted: Fri Oct 21, 2011 7:35 pm
by Instructor
Eh, there are very few "wrong" ways to use CSS. Back when I was learning CSS in the very early "oughts", I was always taught to use IDs for broad categories and then classes for fine point stuff. Looks like the theory is changing a bit from what our programming students are saying. I can definitely see the advantages of either way (automatic application vs. multi-use). It comes down to what YOU are comfortable with. Try both ways and see which you like better!
Re: class id class id ???
Posted: Sun Oct 23, 2011 1:55 pm
by Lundholm
Yeah, my theory is that the only wrong way to use CSS is to not use it.