Is there a reason why the examples always uses class and not id? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Is there a reason why the examples always uses class and not id?

Thanks :)

9th Oct 2018, 4:34 PM
Stefan
Stefan - avatar
3 Answers
+ 2
Yes. You should try to use classes if possible, and also make your classes reusable. So if you have X elements that need green text and a border, maybe make two classes called "green" and "bordered" and give them both. This makes your web design more consistent too and you don't need to repeat yourself in the CSS as much. I rarely need ids in CSS and I usually only need them for javascript.
9th Oct 2018, 7:37 PM
Schindlabua
Schindlabua - avatar
+ 1
You shuold better know about the difference between “class” and “id”; here it is: Unlike the id selector, the class selector is most often used on several elements. This allows you to set a particular style for many HTML elements with the same class. The class selector uses the HTML class attribute, and is defined with a "." id is used when we have to apply CSS property to one attribute only.
9th Oct 2018, 4:43 PM
Mehdi Shafaeian
Mehdi Shafaeian - avatar
0
Thank you for your answer. I already know the difference between class and id. I asked because i think you could also use id because the css style (for example the border colors) are only applied to one p element. Or is it best practice to always use class unless id is specifically needed?
9th Oct 2018, 4:52 PM
Stefan
Stefan - avatar