Css id and classes
why we use classes in css although we can do the same work with id.i am confuse in it. can any one please explain.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Keertti Raj Thangavelu BabuRank #3,979 · 374 reputationPosted Apr 26, 2015, 8:43 PM EDT
see this link you will get more idea about id and class.
https://css-tricks.com/the-difference-between-id-and-class/
Manoj BhoirRank #599 · 4K reputationPosted Apr 26, 2015, 8:56 PM EDT
Use a class when you want to consistently style multiple elements throughout the page/site. Classes are useful when you have, or possibly will have in the future, more than one element that shares the same style. An example may be a div of "comments" or a certain list style to use for related links.
Additionally, a given element can have more than one class associated with it, while an element can only have one id. For example, you can give a div two classes whose styles will both take effect.
Furthermore, note that classes are often used to define behavioral styles in addition to visual ones. For example, the jQuery form validator plugin heavily uses classes to define the validation behavior of elements (e.g. required or not, or defining the type of input format)
Examples of class names are: tag, comment, toolbar-button, warning-message, or email.
Use the ID when you have a single element on the page that will take the style. Remember that IDs must be unique. In your case this may be the correct option, as there presumably will only be one "main" div on the page.
Examples of ids are: main-content, header, footer, or left-sidebar.
A good way to remember this is a class is a type of item and the id is the unique name of an item on the page.
Reference : http://stackoverflow.com/questions/12889362/difference-between-id-and-class-in-css-and-when-to-use-it
For more details please see :
ID's and Classes-
http://ryanfait.com/articles/the-difference-between-ids-and-classes/
The Difference Between ID and Class-
https://css-tricks.com/the-difference-between-id-and-class/
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted May 4, 2015, 2:01 AM EDT
1. IDs are unique for each element but classes are not (one class can be used with many element).
2. When you have same property in class & ID css, ID css property takes the priority.
For ex:
#input-large {
background: red;
}
.input-large-class {
background: green;
}
usage:
input-large-class" />
Here, the input text's background would be red not green.
Nicely described here https://css-tricks.com/the-difference-between-id-and-class/
Harminder SinghRank #1,153 · 1.6K reputationPosted May 5, 2015, 6:33 AM EDT
when we want to give same type of properties to the tags then we use class and if we want to
give some unique property to a particular tag then we use id beacause id's are unique and classes are not...
for example for tags
to
we want to give red color and for
we want to give blue color,then for tags
to
we will use classes and for
we will use id.
Thank you