When defining styles, sometimes the usage of id and class are confused, so I will explain it here:
The id and class attributes are two common attributes in web pages. They work together to make the entire page colorful. When we define a style for an element. You can use id or class.
1. When writing in a CSS style sheet, the prefix symbol ‘#’ should be added before the id selector, and the prefix symbol ‘.’ should be added before the class selector.
2. The id attribute can generally only be used once in a page, while class can be referenced multiple times.
3. ID is used as a tag of an element to distinguish different structures and contents, while class is a style that can be applied to any structure and content.
4. In terms of layout ideas, we generally adhere to this principle: id determines the structure and content of the page first, and then defines a style for it: on the contrary, class defines a type of style first, and then Apply class styles to different elements and content on the page as needed.
5. Currently, browsers can allow multiple IDs with the same attribute value to appear on the same page. Under normal circumstances, they can be displayed normally. However, when using JavaScript to control elements through IDs, errors will occur. .
6. In practical applications, class is more often used in text sections and page modifications, while id is more often used to implement grand layouts and design containing blocks, or styles of containing boxes.