Comparison of usage principles for ID and class

Source: Internet
Author: User
Belongs up one word: ID is unique and is parent, class is repeatable and child-level. Keeping a good code writing habit can be a great help for future code maintenance.

Many divcss layout techniques have been discussed in the previous articles in webjx.com. When using CSS to control the layout of a Web page, we may all have a problem: when defining an attribute, is it using ID or class? Today webjx.com again talk about the use of ID and class principles, the usual time to do the station's experience to you briefly, I hope to be able to help you.
  Principle of use of IDs
First of all, Id,id is unique, and its use principle is based on this feature. IDS cannot be duplicated, so in the structure of XHTML, the large structure must be with an ID. such as logo, navigation, subject content, copyright. These are named after the #logo, #nav, #content, #copyright, etc., in accordance with the principles of its uniqueness. The garden suggests defining IDs as far as possible on the periphery.
  The use principle of class
Class is universal in the definition of CSS. The white point is that class has a repeatable unlimited number of uses many times, the garden suggests that you try to use within the structure. The benefit of this is that it helps to maintain and modify the site code later, so that all classes become children of IDs or grandchildren. When we write CSS, it can be written like this #father. Child {...}, the other thing to note is try not to let class contain IDs, such as. Father #child {...} If it's written in this way it's obviously very undesirable. Of course, this is only relative to the good writing habits of some suggestions, only for your reference.
To sum up, belongs up a word: ID is unique and is a parent, class can be repeated and is child-level. Keep a good code writing habits for future code maintenance will be a great help, if you have better suggestions, please leave a message for me to explore.



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.