Differences between class and id tags in HTML

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.