+ 17

Is id or class “better”

Does anyone have an opinion that <div id=“example”> or <div class=“example”> Is better? Just wondering.

17th May 2018, 7:21 PM
🐺Michael🐺
🐺Michael🐺 - avatar
25 Respostas
+ 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
20th May 2018, 12:04 AM
🐺Michael🐺
🐺Michael🐺 - avatar
+ 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 :)
17th May 2018, 7:24 PM
Just A Rather Ridiculously Long Username
+ 13
Both of them have their own purpose. Id must be unique. Class is to group other bunch of elements together.
17th May 2018, 7:47 PM
JTLZ
+ 11
Just A Rather Ridiculously Long Username It does. Your username however, does not make sense. 😂🤣
17th May 2018, 7:28 PM
🐺Michael🐺
🐺Michael🐺 - avatar
+ 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.
18th May 2018, 6:27 AM
developer 👩‍💻
developer 👩‍💻 - avatar
+ 7
Mzee I actually like # more. 😂🤣
18th May 2018, 11:14 AM
🐺Michael🐺
🐺Michael🐺 - avatar
+ 5
id attributes can be used only once for an element
18th May 2018, 4:13 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 5
Princethaone What do you mean?
18th May 2018, 4:14 AM
🐺Michael🐺
🐺Michael🐺 - avatar
+ 3
That makes sense. And, believe it or not I've got that a rather ridiculous number of times.
17th May 2018, 7:39 PM
Just A Rather Ridiculously Long Username
+ 3
Is it part of the top 10 questions scientist still can’t answer?
18th May 2018, 3:42 AM
Princethaone
+ 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?!
18th May 2018, 4:49 AM
mzee
mzee - avatar
+ 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.
18th May 2018, 12:22 PM
Seniru
Seniru - avatar
+ 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
19th May 2018, 8:57 AM
ImBrett
+ 2
I usually use id, as it is quicker to type than class, though class is better for bunching elements together.
18th May 2018, 6:06 AM
Rowsej
Rowsej - avatar
+ 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.
19th May 2018, 4:25 AM
Paavan Gupta
Paavan Gupta - avatar
+ 2
class
19th May 2018, 10:11 AM
Sonu Rana
Sonu Rana - avatar
+ 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
20th May 2018, 5:43 PM
M Saeed Ahmad
+ 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'.
31st Jan 2020, 2:51 PM
HonFu
HonFu - avatar
+ 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.
18th May 2018, 8:06 PM
Sonik Sainsate
Sonik Sainsate - avatar
+ 1
Id
19th May 2018, 10:16 AM
Danial Retrov
Danial Retrov - avatar