Let's take an example to differentiate the two:
For example, if there are many people in a house, ID is the name of the person, and Class is the clothes.
If two people have the same name, they will be confused, and if two people wear the same clothes, they will not be in a similar situation. So after understanding this metaphor, you will understand the relationship between them in half!
Let's talk about the differences between them.
In terms of definition:
Id is the name of an element, which can be accessed by js or other script programs.
Class is the css class Name of the element.
W3C principles and regulations
ID is unique, that is, ID is unique on a page.
Class is universal
Tips for using ID and CLASS: www.2cto.com
1. The child name contains the part in the parent name that starts. This makes it easy to clarify the hierarchy when writing CSS.
2. It is best not to use ID for Sub-Level in CLASS.
3. It is best to use case-insensitive names for CLASS naming.
For example, newMovie is the most suitable method to use the explicit relationship with the first statement. Note that browsers other than IE are very case sensitive. Also, it must start with a letter.
The priority of ID and class is different.
For the priority, see Style> ID> Class> default Html element.
Summary
1. ID is unique and should be used in the periphery whenever possible. The CLASS is reproducible and should be used within the structure as much as possible.
2. This is conducive to later maintenance and modification, so that all classes become sublevels or grandchildren of IDs.
3. When repeated definitions appear, the priority is: Style> ID> Class> default Html element.