+ 17
Is id or class “better”
Does anyone have an opinion that <div id=“example”> or <div class=“example”> Is better? Just wondering.
25 Réponses
+ 10
BEST 2 ANSWERS:
SHORT:
id attribute is used when a specific (usually only one) element needs to be styled.
class attribute is used when a single or usually a group of elements needs to be styled.
—Asiya Rehman
LONGER:
two or more elements can have the same class but the Id of each element should be unique. therefore, Id has more priority .but if you want two or more elements to look same, assign them the same class and give that class some styling which will be then applied to all elements belonging to that class. but if you want an element of a class to look different from other elements of the same class, you can assign that element an id and give some styling to that id which will be then also applied to the element. so we can say that, sometimes id is more preferrable than class and sometimes classes can be more preferrable than id.
—Paavan Gupta
+ 24
They're both used for their own purposes.
Usually, id is used when you want to uniquely identify an element, and class is used when you want to group a bunch of elements
Hope this made sense :)
+ 13
Both of them have their own purpose.
Id must be unique.
Class is to group other bunch of elements together.
+ 11
Just A Rather Ridiculously Long Username
It does. Your username however, does not make sense. 😂🤣
+ 7
They both are use for their own purpose . Id is basically for uniquely identify the element and class is for group of bunch of elements .
well using id in JavaScript helps a lot.
+ 7
Mzee
I actually like # more. 😂🤣
+ 5
id attributes can be used only once for an element
+ 5
Princethaone
What do you mean?
+ 3
That makes sense. And, believe it or not I've got that a rather ridiculous number of times.
+ 3
Is it part of the top 10 questions scientist still can’t answer?
+ 3
it depends on what you wanna do. For styling i like class (i get irritated by # tags) but for rules (js) i use id. You know you might use both of then, right?!
+ 3
id must have a unique value. But class can have a name that used by another. id is useful for linking that element inside the page
eg: <a href="#link>go there</a>
<p id="link">♤♡♢♧</p>
And id can be used to control one elements behaviour using js.
Classes would be helpful when working with more than 1 element at once.
+ 3
I hate to beat this further with a stick, but you're going to use both ID's and classes in any project you do that utilizes HTML/CSS/JS at the same time - that, and no one is bringing up the actual reason you want to use classes for styling - SPECIFICITY.
https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
Use ID's when you want to target a specific element on your page, if you are using them to style - you're doing it wrong by todays standards.
to clarify - the higher the specificity, the more of a PAIN IN THE A** it is to adapt later on.
lowest-highest specificity:
1) base selectors (h1, a, p, input, etc.) - offers some customization
2) Classes - Offers total REUSABLE customization
3) ID's - what more do you need than total customization?
also, because it's a unique identifier you might as well use inline styling
4) Inline styling - NEVER, EVER, we have external stylesheets for a reason
5) !important - again, if you absolutely have to, you probably did something wrong
+ 2
I usually use id, as it is quicker to type than class, though class is better for bunching elements together.
+ 2
two or more elements can have the same class but the Id of each element should be unique. therefore, Id has more priority .but if you want two or more elements to look same, assign them the same class and give that class some styling which will be then applied to all elements belonging to that class. but if you want an element of a class to look different from other elements of the same class, you can assign that element an id and give some styling to that id which will be then also applied to the element. so we can say that, sometimes id is more preferrable than class and sometimes classes can be more preferrable than id.
+ 2
class
+ 2
In a page only an element can have same id while a class can be applied on number of elements. so it depends on the scenario
+ 2
id allows you to access *one* specific part of your HTML code, while class allows you to access a whole set of them.
Which is better will depend on the case. If you want to for example modify the optic of several parts of your code the same way, you want to write that only once.
But if you want to just access one specific element, like a canvas or something, you'd use 'id'.
+ 1
I'd say classes get an upper hand in this case. You get the freedom to make generic styling sheets that can be used in different projects at different times.
id usually are provided to target specific elements and hence are generally assigned with id name specific to functionality.
Classes can be flexible in such cases to start making your styles more adaptive to any new projects you create. More like your own style, flexible and easily adaptable.
+ 1
Id