Beautiful code is the foundation of a beautiful website. Excellent CSS only exists on top of equally excellent HTML. Clean, semantic HTML code makes a website more robust. This article describes 12 laws for achieving clean web design code, which is suitable for anyone engaged in web design. 1. Strict DOCTYPE If you have to do it, do it right. Whether it is HTML 4.01 or XHTML 1.0, they all provide Strict mode. Using Strict mode can ensure that our code does not hide any errors. 2. Character set declaration, special characters are encoded The character set declaration should be placed first in the hea> section so that the browser knows how to display all content in the web page, including the title. In addition, some special characters, such as &, are best replaced with &, which is the safest method. 3. Proper locking Indentation will not affect the rendering of web pages, but will significantly improve the experience when reading source code. There are no specific rules for indentation, but it's a good practice to be consistent throughout. 4. Place CSS and JavaScript in external files Placing CSS and JavaScript references in external files not only reduces the size of a single web page, but also means that other web pages can share these codes. In addition, the browser's caching mechanism can effectively reduce repeated downloads of the same code. 5. Nest tags correctly As shown below, in the first line of code, the h1> tag is nested in the a> tag. Although most browsers will render it correctly, this is not a good habit. The tag is a block object, but an inline object. Inline objects should not contain block object. 6. Eliminate unnecessary divs Divs are often abused (especially in the DIV+CSS myth we are in now - Translator), people want to put everything in a div so that they can have CSS styles assigned to them, and this abuse can lead to bloat. 7. Use better naming conventions As shown below, the CSS class of Cat is named red italic, which implies that Cat uses red italic. What if you want to change Cat to blue bold? 8. Try to use CSS to control text layout As shown in the figure below, do not use uppercase letters directly. Use CSS to control the formatting of these words, which will be more flexible. 9. Assign an independent class/id to the body Assigning an independent class/id to the body Tag can easily locate any object in the page, because all objects in the page are located in the body. 10. Verification Needless to say, you should verify the code of your web pages whenever possible. Although some coding errors can be automatically corrected by the browser, some errors will have negative consequences, especially if you are in Strict mode. Even if you do nothing, seeing that green W3C verification mark can at least make you feel better. There is no need to be rigid to pass the W3C verification. There are more considerations in Web design. Only sticking to the W3C verification results may affect some more important factors. For example, IE6 has many bugs in the W3C standard. If you want 100 %If you announce that your website does not support IE6 through W3C verification, at least in China, you will lose more than you gain - Translator 11. Reasonable structural order Keep the structure of your web page in a logical order. 12. Do your best If you start writing from scratch, it is of course much easier to maintain the above principles. If you want to modify the old code, it will be very painful. The poor coding of some CMS systems will put you in a quagmire, or your website will need to be modified if it is large in scale. There is so much stuff, no matter what, it is important to always maintain good habits.