ID & Class
Hi Everybody can someone please tell me that what is difference between ID and Class in css..................... Thanks in advance
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
Amit ChoudharyRank #145 · 15.8K reputationPosted May 3, 2011, 7:26 AM EDT
When you want to apply unique css style to particular HTML element then we use ID as it is the unique for all elements on any HTML page.
Basically used with "#" for e.g. #Div1{ border: solid 1px gray;}
if you want to use the same css style on different elements then we use Class.
Class is defined with "." for e.g. .CommonDiv{border:none; color:red;}
hope this make sense to you.
Thanks
Mpholo Leboea15 reputationPosted May 4, 2011, 12:49 AM EDT
ID
ID is the only identity that can be used on the web page to uniquely identify a single element.
ID can only be assign/used once to identify element on a page.
Class
Class can identify a group of elements on the web page.
Class can appear/be assigned to more than one element on the web page.
They can both be used in stylesheets to style you pages and Javascript/JQuery to refer to element(s).
Ankit NandekarRank #703 · 3.4K reputationPosted May 4, 2011, 1:56 AM EDT
ID's are unique
- Each element can have only one ID
- Each page can have only one element with that ID
your code will not pass validation if you use the same ID on more than one element. Validation should be important to all of us, so that alone is a big one.Classes are NOT unique
- You can use the same class on multiple elements.
- You can use multiple classes on the same element.
Any styling information that needs to be applied to multiple objects on a page should be done with a class. Take for example a page with multiple "Maindiv":For more details visit this link
http://www.soft32.com/download_112.html