CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   HTML & CSS (http://www.codingforums.com/forumdisplay.php?f=13)
-   -   what is main difference between class and Id? (http://www.codingforums.com/showthread.php?t=286292)

sunilmkt 01-23-2013 01:44 PM

what is main difference between class and Id?
 
This very common question which are asked. I want the main difference with example.

L0adOpt1c 01-23-2013 02:00 PM

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

jerry62704 01-23-2013 02:03 PM

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.

VIPStephan 01-23-2013 03:06 PM

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.

DanInMa 01-23-2013 03:06 PM

Quote:

Originally Posted by L0adOpt1c (Post 1308144)
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.

L0adOpt1c 01-23-2013 03:34 PM

Quote:

Originally Posted by DanInMa (Post 1308164)
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.

VIPStephan 01-23-2013 04:21 PM

Quote:

Originally Posted by L0adOpt1c (Post 1308173)
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.

xhtmlchamps1 01-24-2013 09:08 AM

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;}

DanInMa 01-24-2013 02:15 PM

Quote:

Originally Posted by L0adOpt1c (Post 1308173)
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.

L0adOpt1c 01-24-2013 02:25 PM

Quote:

Originally Posted by DanInMa (Post 1308439)
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.

felgall 01-24-2013 06:35 PM

Quote:

Originally Posted by DanInMa (Post 1308439)
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.

felgall 01-25-2013 06:01 AM

Quote:

Originally Posted by yguyfan (Post 1308654)
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.


All times are GMT +1. The time now is 06:15 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.