15 coding principles that Web developers should know

Source: Internet
Author: User

HTML has gone through nearly 20 of the development process. From HTML4 to XHTML to the most recent fiery HTML5, it has almost witnessed the development of the entire Internet. However, even now, there are many basic concepts and principles that still require the developer to be highly aware. Below, we introduce the development principles that should be followed.




1, use Div to layout

When developing a Web page, the first thing to consider is to differentiate the page from the main point. By including the content in div tags, the code of the page will show a neat, well-indented style.

    1. <div id= "header" ></DIV>&NBSP;&NBSP;&NBSP;&NBSP;

    2. <div id= Body-container ">&NBSP;&NBSP;&NBSP;&NBSP;

    3.          <div id= "Content" > <!-- Content -- > </div>       

    4.         <div id= " Right-side-bar "> <!-- right side bar content area -- ></ div>    

    5. </div>      

    6. <div id= "Footer" ></DIV>&NBSP;&NBSP;



2. Separating HTML tags from CSS style sheets

A good page should separate the HTML tag from the CSS style sheet. This is a principle that every web developer should know when they first touch web development. However, until today, there are still many developers who do not strictly follow this principle.
Do not embed style sheet code in HTML tags. Developers should get into the habit of creating files separately to store CSS stylesheets. This will also make it easier for other developers to get the job done quickly when modifying your code.

    1. <p style= "color: #CCC; font-size:16px; Font-family:arial ">

    2. An example to illustrate inline style in html</p>



3. Optimize CSS Code

Adding more than one CSS file to a Web site is now a common practice. However, when a Web site contains too many CSS files, it can slow down the response time of the site. The workaround is to streamline the code and optimize multiple CSS files to merge them into one file. This method can significantly improve the loading speed of the website. In addition, there are many tools that can be used to optimize CSS files, such as CSS Optimizer, clean CSS, and so on.
For CSS, we've also recommended 9 good CSS frameworks you should know about CSS frameworks and how to use them.


4. Optimize JavaScript files and place them at the bottom of the page

As with CSS, it is common practice to add multiple JavaScript files to a page. But it also reduces the responsiveness of the site. To do this, developers should streamline and optimize these JavaScript files.
However, unlike CSS, browsers typically do not support parallel loading. This means that when a browser loads a JavaScript file, it will no longer load other content at the same time. This causes the page to load as if it were slowing down.
A good solution is to put JavaScript files in the last order of loading. To achieve this, developers can put JavaScript code at the bottom of the HTML document, and the best place to put it is near the </body> tag.


5. Use the TITLE element




6. Use appropriate HTML tags in the right place

HTML tags are the key to constructing a canonical content structure. For example,,<em> tags are used to emphasize emphasis. <p> tags apply to highlight article paragraphs. If you want to empty lines between paragraphs, do not use the <BR/> label.

    1. <em>emphasized text</em>

    2. <strong>strongly emphasized text</strong>



For a set of related elements, it is recommended to use the <ul>, <ol>, or <dl> tags. However, do not use the <blockquote> tag incorrectly because it was originally used to define the block application.


7. Avoid Misuse <div> labeling

Not all block elements should be created with the <div> tag. For example, you can add a display:block to an inline element's properties to display it as a block element.


8. Create a navigation using the list

You can create beautiful navigation menus by using the <ul> List tab, with the corresponding CSS style.


9, do not forget to close the label

Now, whenever I recall the first lesson I learned about web development in college, the importance of the HTML structure that the Professor mentions always comes to mind. According to the standard, labels should be closed. That is because, in some browsers, if you do not follow the standard to close the label, there will be a problem showing the abnormal. This is particularly evident in IE6, 7 and 8.


10. Label lowercase syntax

The use of lowercase syntax for labels is an industry standard. Although the uppercase syntax does not affect the display of the page, the code is poorly readable. The following code is very poor in readability:

    1. <DIV>

    2. <a href= "#" title= "click here" >click here</a>

    3. <p>some Sample Text</p>

    4. </DIV>



11. Add alt attribute for picture label

In the tab, the ALT attribute is often useful. Because search engines are often unable to crawl image files directly. However, if the developer adds a description of the image to the ALT attribute, it will be convenient for the search engine to crawl.

    1. <!--have an ALT attribute, which would validate, but Alt value is meaningless-->

    2. <!--the correct-->



12. Use <label> and <fieldset> in the form

To improve code quality and make it easier for users to understand table content, we should create table elements with <label> and <fieldset> tags.

    1. <fieldset>  

    2.     <legend>Personal  particular</legend>  

    3.     <label for= "name" >Name </label><input type= "text"  id= "name"  name= "name" &NBSP;/>&NBSP;&NBSP;

    4.     <label for= "Email" >e-mail</label><input type= "text"   id= "Email"  name= "email"  />  

    5.     <label for= "Subject" >subject</label><input type= "text"  id= "subject"  name= "Subject"  />   

    6.     <label for= "message"  >message body </label>  

    7. <textarea rows= "ten"  cols= " id=" message "  Name= "message" &NBSP;></TEXTAREA>&NBSP;&NBSP;

    8. </fieldset>  



13, the browser-compatible code to identify information and separate from each other

Cross-browser compatibility is a key concern for a web developer. In general, developers will code for different browsers, which is CSS hack. However, if the developer is coding, it can be noted that the code for which version of the browser is written, will be very convenient for future maintenance work. Here's a good example:

    1. <!--[if IE 7]>

    2. <link rel= "stylesheet" href= "Css/ie-7.css" media= "All" >

    3. <! [endif]-->

    1. <!--[if ie 6]>  

    2. <link rel= "stylesheet"  href= "Css/ie-6.css"  media= "all" >  

    3. <script type= "Text/javascript"  src = "Js/dd_belatedpng_0.0.8a-min.js" ></SCRIPT>&NBSP;&NBSP;

    4. <script type= "text/ JavaScript ">&NBSP;&NBSP;

    5.         dd_belatedpng.fix (' #logo ');   

    6. </script>  

    7. <![ endif]-->  



14. Avoid excessive annotation

As a developer, adding comments to your code is a good habit, easy to understand and easy to maintain. This is common in other programming languages such as PHP, JAVA, and C #. However, html/xhtml is a text markup language that is very easy to understand. Therefore, there is no need to add comments for each line of code.


15. Test Code

Developers are recommended to test the code using the text tag verification service. It is an efficient test tool that can help you find errors in the page. It also allows you to navigate to the appropriate code, starting with a page fault. This is often difficult to do after the coding is complete. However, developers need to be aware that the code passed by the validation is not the code that performs well.

Reference source http://www.zhihaijiangku.com/

15 coding principles that Web developers should know

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.