Go Back   CodingForums.com > :: Client side development > HTML & CSS

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-23-2013, 01:44 PM   PM User | #1
sunilmkt
New Coder

 
Join Date: Jan 2013
Location: India
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
sunilmkt can only hope to improve
what is main difference between class and Id?

This very common question which are asked. I want the main difference with example.
sunilmkt is offline   Reply With Quote
Old 01-23-2013, 02:00 PM   PM User | #2
L0adOpt1c
New Coder

 
L0adOpt1c's Avatar
 
Join Date: Jan 2013
Location: <bed />
Posts: 95
Thanks: 0
Thanked 7 Times in 7 Posts
L0adOpt1c is an unknown quantity at this point
Class can affect several elements.

ID is typically used for a single element, though you can use it on several elements.

Id:
Code:
#id1 {
text-align:center;
color:blue;
}
Class:
Code:
.colorcode {
color: red;
}
Classes begin with '.'
ID's begin with '#'

Hope this helps.


Here's a helpful link:
http://w3schools.com/css/css_id_class.asp
__________________
Validate, early and often. | Use W3Schools! |
Search for your answer before posting.
Thank the people that help you!
Please mark things as resolved.

Last edited by L0adOpt1c; 01-23-2013 at 02:01 PM.. Reason: Added link
L0adOpt1c is offline   Reply With Quote
Old 01-23-2013, 02:03 PM   PM User | #3
jerry62704
Senior Coder

 
jerry62704's Avatar
 
Join Date: Oct 2007
Location: Springfield, IL
Posts: 1,077
Thanks: 13
Thanked 84 Times in 84 Posts
jerry62704 is on a distinguished road
An "id" is unique and applies to one and only one element on the page. A "class" is not unique and can apply to none, every or some elements on a page.

CSS
#unique {color: red;}
.notunique {color: blue;}

HTML
<div id="unique">
...
</div>

<div id="unique2" class="notunique">
<p class="notunique">
...
</p>
</div>

The "#" denotes an id in the CSS, but is not used in the HTML. The "." denotes a class in the CSS, but is not used in the HTML.
__________________
.
.
...and gladly would he learn and gladly teach

Visit www.LiberalsWin.com for humor and the unique Bush/Obama Approval Polls

Last edited by jerry62704; 01-23-2013 at 02:08 PM..
jerry62704 is offline   Reply With Quote
Old 01-23-2013, 03:06 PM   PM User | #4
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,697
Thanks: 5
Thanked 875 Times in 850 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Also, IDs can be used as anchor hooks to link to, e. g.:
Code:
<a href="#example">link</a>
…
…
<div id="example">clicking the link above brings me to this element</div>
And in CSS, IDs have a higher specificity (“importance”) than classes.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 01-23-2013, 03:06 PM   PM User | #5
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,335
Thanks: 13
Thanked 207 Times in 207 Posts
DanInMa is on a distinguished road
Quote:
Originally Posted by L0adOpt1c View Post
Class can affect several elements.

ID is typically used for a single element, though you can use it on several elements.
You should read the page you linked him..

Quote:
The id Selector

The id selector is used to specify a style for a single, unique element.
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Old 01-23-2013, 03:34 PM   PM User | #6
L0adOpt1c
New Coder

 
L0adOpt1c's Avatar
 
Join Date: Jan 2013
Location: <bed />
Posts: 95
Thanks: 0
Thanked 7 Times in 7 Posts
L0adOpt1c is an unknown quantity at this point
Quote:
Originally Posted by DanInMa View Post
You should read the page you linked him..
And you should check your facts.

And am I not correct in saying that you CAN use them for several elements?

It's possible. I've done it.
__________________
Validate, early and often. | Use W3Schools! |
Search for your answer before posting.
Thank the people that help you!
Please mark things as resolved.
L0adOpt1c is offline   Reply With Quote
Old 01-23-2013, 04:21 PM   PM User | #7
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,697
Thanks: 5
Thanked 875 Times in 850 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Quote:
Originally Posted by L0adOpt1c View Post
And am I not correct in saying that you CAN use them for several elements?
You can but I think it was a misunderstanding between you two. The way you said it could be used could be misunderstood that it can be used for several elements in the same document which is definitely wrong. It can generally be used on almost all HTML elements, that’s true, but an ID must occur only once per document.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 01-24-2013, 09:08 AM   PM User | #8
xhtmlchamps1
New to the CF scene

 
Join Date: Jan 2013
Location: Hyderabad
Posts: 3
Thanks: 0
Thanked 1 Time in 1 Post
xhtmlchamps1 is an unknown quantity at this point
code for class and I.D

The id selector is used to specify a style for a single, unique element.
The id selector uses the id attribute of the HTML element, and is defined with a "#".

The style rule below will be applied to the element with id="para1":

Example-

#para1
{
text-align:center;
color:red;
}

Now for Class-

The class selector is used to specify a style for a group of elements. Unlike the id selector, the class selector is most often used on several elements.

Example-
.center {text-align:center;}
xhtmlchamps1 is offline   Reply With Quote
Old 01-24-2013, 02:15 PM   PM User | #9
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,335
Thanks: 13
Thanked 207 Times in 207 Posts
DanInMa is on a distinguished road
Quote:
Originally Posted by L0adOpt1c View Post
And you should check your facts.

And am I not correct in saying that you CAN use them for several elements?

It's possible. I've done it.
No you are not correct. You can give multiple elements the same ID all day long, but its not compliant to the standard and it causes issues if you are trying to target an element with javascript by id if there are mutiple elements with the same id.

Id's are and always have been meant to be unique. Sorry, you are incorrect.
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Old 01-24-2013, 02:25 PM   PM User | #10
L0adOpt1c
New Coder

 
L0adOpt1c's Avatar
 
Join Date: Jan 2013
Location: <bed />
Posts: 95
Thanks: 0
Thanked 7 Times in 7 Posts
L0adOpt1c is an unknown quantity at this point
Quote:
Originally Posted by DanInMa View Post
No you are not correct. You can give multiple elements the same ID all day long, but its not compliant to the standard and it causes issues if you are trying to target an element with javascript by id if there are mutiple elements with the same id.

Id's are and always have been meant to be unique. Sorry, you are incorrect.


Sorry, but the person before was correct. Of course you can't use one ID multiple times per page, you can use them across several pages on one website.
__________________
Validate, early and often. | Use W3Schools! |
Search for your answer before posting.
Thank the people that help you!
Please mark things as resolved.
L0adOpt1c is offline   Reply With Quote
Old 01-24-2013, 06:35 PM   PM User | #11
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,530
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by DanInMa View Post
You can give multiple elements the same ID all day long, but its not compliant to the standard and it causes issues if you are trying to target an element with javascript by id if there are mutiple elements with the same id.
It isn't just from JavaScript that specifying multiple copies of the same id breaks. It also breaks in HTML when you link to it.

If the web page were the world and the id was "Birmingham" and you needed to go there - which Birmingham would you go to?

Code:
<div id="Birmingham">England</div>


<a href="#Birmingham">go to Birmingham</a>


<div id="Birmingham">Alabama</div>
Almost all of the functionality that applies to ids relies on the fact that there will be only one.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 01-25-2013, 06:01 AM   PM User | #12
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,530
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by yguyfan View Post
you can use them across several pages on one website.
No one has claimed that you can't. It was the claim that you can use the same id multiple times in the same page that was being refuted.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:35 AM.


Advertisement
Log in to turn off these ads.