What is the difference between "id" and "class" in terms of their usage? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the difference between "id" and "class" in terms of their usage?

when can i use id and when can i use class?

12th May 2020, 5:29 PM
KWASI NYARKO
KWASI NYARKO - avatar
2 Answers
+ 3
You may consider this easy : 1) Id : First of all what is id? Id is 'identity' that is you have your own identity using what one can recognise you right? For example your NID card. It is unique and will not be matched with other. Similarly, in html, we give an id to a tag so that we can recognise it using it's uniqueness. We can not use the id twice in a html document. It must be unique for every. For example : <div id="one"></div> As we have declared the id 'one' we should not use it with others anymore. <header id="one"></header> This is illegal. So in short, id is an unique identifier for a tag. 2) Class : What is a class? Why do we use it? Well, the word class itself indicates a specific group, doesn’t it? For example all of your classmates including you in a school is a class. Therefore, we can use the same class for many tags to make it a specific group. For example : <div class="box"></div> <div class="box"></div> <div class="box"></div> ........
12th May 2020, 5:33 PM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
+ 4
[...continued] Now you can use just one class to style the three divs altogether :))
12th May 2020, 5:33 PM
Arb Rahim Badsa
Arb Rahim Badsa - avatar