The topic of this article is how to use ID and CLASS. I have mentioned application principles and skills in the title. The Application principles and skills here are my experience summaries, not from which books, therefore, it is not an authoritative discussion. please refer to the topic of this article, that is, how to use ID and CLASS, and the principles and skills of application are mentioned in the title, the application principles and skills here are my summary of experience, not from which book, so they are not authoritative discussions. please have an idea to browse this article, do not be confused by my enchanting words.
Application principles of ID and CLASS
It is said that W3C sets IDs and classes to be unique and has a wide range of classes. Therefore, our application principles here are also based on this feature. IDS cannot be repeated. Therefore, in the XHTML structure, the large structure must use IDs. Such as tag, navigation, subject content, and copyright. The naming rules I have developed are # logo, # nav, # content, # copyright. Some people say that layout uses ID and color background uses CLASS, which is really inaccurate. ID has no direct relationship with layout, and CLASS and color are equal. Some people use IDs or all use classes for the sake of trouble. it is also understandable that it is not normal to use all IDs, which is contrary to the uniqueness of IDs. If each ID is different, it is not exhausting. The application principle I have summarized through practice is: ID must be unique and should be used as far as possible. The CLASS is reproducible and should be used within the structure as much as possible. This is beneficial to the protection and correction of website code in the future. This will make all the classes become sub-or grandchildren of IDs. You can have two sons, but can you have two fathers. When we write CSS, we can write it like this # father. child {...} Try not to include the Class ID,. father # child {...} It seems a bit ridiculous to write it like this. Of course, this is not a matter of course. it should be taken into special consideration in special cases. However, this situation is usually not necessary.
The above is the application principle of ID and CLASS. In a word, ID is unique and has a parent level, and CLASS can be repeated and sub-level.
Application skills of ID and CLASS
- The child-level naming contains the part of the parent name that starts. This helps you understand the hierarchy when writing CSS.
- It is best not to use ID for the child level in the CLASS. Of course, special situations are special.
- CLASS naming is best case-sensitive and practical. For example, newMovie is the most suitable method to work with the first article to understand the relationship. Note that browsers other than IE are very case sensitive. Also, it must start with a letter.
In fact, all of the content above is what everyone understands, but it is a summary to write it down first. I hope that you will come up with new ideas for the application of ID and CLASS. Summarize the most appropriate and efficient specifications.