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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Harminder SinghPosted May 5, 2015, 10:33 AM
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
Jaganathan BantheswaranPosted May 4, 2015, 6:01 AM
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/
Manoj BhoirPosted Apr 27, 2015, 12:56 AM
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/
Keertti Raj Thangavelu BabuPosted Apr 27, 2015, 12:43 AM
see this link you will get more idea about id and class.
https://css-tricks.com/the-difference-between-id-and-class/