+ 1
What is the difference between class and id?
9 Réponses
+ 5
you can give the same class to more than element but can't by id ;one id to one element.
+ 2
Any element inside body accepts classes and id
+ 1
Can we give class when using a single element?
+ 1
yes, we can
+ 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.
+ 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 )
+ 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
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.
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.