[HTML] When do I use .class and when do I use #id? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

[HTML] When do I use .class and when do I use #id?

for example, I know that for div elements it is better to use class, <div class="a">...</div> why didn't we use id? why <p id="b"> and not <p class="b">?

20th Dec 2022, 4:19 AM
Ahmad Sulaiman
2 Answers
+ 3
Id is used to refference a specific element, whereas class can be used to reference a defined group of elements for example a class of headings where all the headings belong to a specific class. Elements can also belong to more than one class.
20th Dec 2022, 6:05 AM
Jerry
+ 2
If you have multiple paragraphs and you need to style one uniquely, you can give it an ID. Please note that the id must be unique and you cannot give the same to other elements. Another id is used to get control on an element in javascript let x = document.GetElement.By.ID(#id)
20th Dec 2022, 7:25 AM
Knight
Knight - avatar