How to Use Id and class in CSS style sheets

Source: Internet
Author: User
My previous Article " Div + CSSStyle table ID and class naming rules I mainly talked about the naming rules of ID and class. With the foundation of the previous section, what I am talking about today is Div + CSS style sheet The use principles and skills of ID and Clas in.
First ID And Class Usage principles
The usage principle here is based on W3C setting of ID and class: ID is unique and class has universal characteristics. IDS cannot be repeated. Therefore, in the XHTML structure, IDS must be used for large structures. Such as logo, navigation, subject content, and copyright. In the Div + CSSStyle Sheet And class naming rules" The naming rules are # logo, # Nav, # Content, # copyright. These are fixed names. Some web designers think that the layout uses ID, and the background and color use class. This is unreasonable. Because ID has no direct relationship with typographical layout, class and color are not equal.

Some designers simply use ID or all classes for the sake of no distinction. At first I used all classes. we can still understand how to use all the classes. it is not normal to use all the IDS. ID Uniqueness. If each ID is different, it is not exhausting. ID must be 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. The advantage of doing so is that it is beneficial to the website Code Later maintenance and modification, this approach will make all 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 would be a bit silly to write it like this. Of course, this is not certain. Special Compiling is required in special cases.
The above is the usage principle of ID and class. In a word, ID is unique and has a parent level, and class can be repeated and sub-level.
Second, ID And Class Usage tips Take a look at the following three points: Www.div-css.net ( 1 ), Sub-Level naming contains the part of the parent name as the beginning. This facilitates the compilation CSS Specify the level.
( 2 ), Class It is best not to use the Sub-Level in ID . Of course, special circumstances are special.
( 3 ), Class It is best to use case-sensitive names for naming. Example . Newmovie This method is most suitable for the use of clear relationships with the first one. Note that IE Other browsers are case sensitive. Also, it must start with a letter. We hope that you can sum up the most suitable and efficient rules based on your own experience for using the IDs and classes in the DIV + CSS style sheet (Principles and Techniques.
Related Articles: div + CSS Columns
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.