+ 1

What is the difference between class and id?

25th Feb 2017, 3:48 PM
Abhi Ram
Abhi Ram - avatar
9 Answers
+ 5
you can give the same class to more than element but can't by id ;one id to one element.
25th Feb 2017, 3:50 PM
AmgadAlaidy
AmgadAlaidy - avatar
+ 2
Any element inside body accepts classes and id
25th Feb 2017, 3:54 PM
Animesh
Animesh - avatar
+ 1
Can we give class when using a single element?
25th Feb 2017, 3:53 PM
Abhi Ram
Abhi Ram - avatar
+ 1
yes, we can
25th Feb 2017, 3:55 PM
AmgadAlaidy
AmgadAlaidy - avatar
+ 1
Example: class of .btn for all buttons. ID could be #btn_cart to single out a particular button. Like a class of people or one person. IDs are more specific and carry more weight when styled.
25th Feb 2017, 4:48 PM
Richard Rogers
Richard Rogers - avatar
+ 1
@Raj Kumar Chaudan wrote: "generally class in used for inline elements and id for block elements. id breaks a line but class doesn't. " Id don't break line at all, and id and classes can be used on block and/or inline element: give a class or an id don't change the behavior of the element, until you apply some css rules to it... The fundamental difference was already given ( id is unique in page, class can apply to many elements ), but there is another little difference: you can set only on id per element, but you can assign ( combine ) how much classes you want... This is valid: <div id="myDiv" class="myClass1 myClass2 myClass3"></div> But you need to be organized in your css rules, for knowing which rule is prioritary on which other ( as @Richard Rogers said, ids carry more weight than classes in priorization, but rules are relatively complex, so what css rule apply isn't always evident :P )
26th Feb 2017, 3:33 AM
visph
visph - avatar
+ 1
class we use it when u need add properties in elements as well as id but id we use it with Javascript to connect the element of html with Javascript
17th Aug 2017, 6:16 AM
Hossam Hassan
Hossam Hassan - avatar
0
in addition of Amgad: generally class in used for inline elements and id for block elements. id breaks a line but class doesn't.
25th Feb 2017, 4:24 PM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
0
@visph about breaking a line I may be wrong(I read it somewhere), and for where to use I wrote "generally" . I didn't say you must use id for block level elements.
26th Feb 2017, 4:16 AM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar