HOME       >>       Programming

Class Vs. Id?


KansukeKojima

#random_div {random: random;random: random;}.random_div {random: random;random: random;}

Ok... well.. above we have a id (the first one) and then the second one is a class. Could anyone please explain to me what the difference between the two is? And is one any better?

Forbez

Class can change like the header tag and other tags like that. So you could have.random_div h1 {random: random;random: random;}Anything within the div which is inbetween the <h1> tag will change according to what you put. Id doesn't do this I believe. Use a mixture of both to get a better effect.


KansukeKojima

ahh... ok thank you.


sonesay

ID's are used when you need define a single element and apply CSS to that element only or you need an easy way for javascript to get to it.Class are used when you have 2 or more elments you need consistent styling. e.g All the the content sections need to be the same style(you may have several division blocks of content).I cant think of any other reasons why you would them at this stage.


jlhaslip

As stated above, ID's should be used when you have one (and only one) Element on the page that you want to style.
Classes can occur once or many times on the same page.

Id's are more 'specific' when the styling is parsed, so the styles for an ID will override a class if there is a conflict in the Cascade.

Check this link: http://www.w3.org/TR/CSS21/cascade.html%23specificity


martinnovoty

Class can change like the header tag and other tags like that. So you could have
.random_div h1 {
random: random;
random: random;
}
Anything within the div which is inbetween the <h1> tag will change according to what you put. Id doesn't do this I believe. Use a mixture of both to get a better effect.


That's not the true, you can do same with id's.

BRs,
M.

Pinkeboo

id tags in HTML (<div id=?header?>) are tags which should only be used once per web page. Generally, you want to use an ID to denote the page structure, so you might have id?s for a web page of ?header?, ?content?, ?sidebar? and ?footer?, because you?re not going to have two headers or two footers for any one webpage. Class: Unlike ID tags, class tags can be used multiple times. This is great when you want different parts of the design to look the same.


KingSingh

Following on from what Pinkeboo said above, id's are used for thing's like header's, footer's, sidebar's etc. and are like this in The CSS:

#idname { property: value; text-align: right }
ID's are shown with HTML usually with div tags like this:
<div id="idname">Content content content etc</div>
Classes can be used several times and a commonly used style of classes is to align. For example, the CSS to centre align anything
.center { text-align: center; }
and the HTML would be like this:
<div ></div>
If you want the same class for different HTML tags, you can do this:
p.right { property:value; }div.right { different stuff: from above; }
and
<p >content</p><div >Differently styled right class</div>

I hope you have found this useful.


VIEW DESKTOP VERSION REGISTERGET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.