Jump to content
xisto Community
KansukeKojima

Class Vs. Id?

Recommended Posts

#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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Edited by sonesay (see edit history)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 class="right"></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 class="right">content</p><div class="right">Differently styled right class</div>

I hope you have found this useful. :)
Edited by KingSingh (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.