Home  >  Article  >  Web Front-end  >  15 coding principles that web developers must know

15 coding principles that web developers must know

伊谢尔伦
伊谢尔伦Original
2016-11-24 15:17:051653browse

HTML has gone through nearly 20 years of development. From HTML4 to XHTML, and then to the recently very popular HTML5, it has almost witnessed the development of the entire Internet. However, even now, there are many basic concepts and principles that developers still need to pay close attention to. Below, I will introduce to you the development principles that should be followed.

1. Make good use of DIVs for layout

When developing a Web page, the first thing to consider is to distinguish the focus of the page. By including these contents in DIV tags, the code on the page will appear neat and well-indented.

<div id="header"></div> 
<div id="body-container"> 
        <div id="content"> <!-- Content -- > </div>   
        <div id="right-side-bar"> <!-- Right Side Bar Content Area -- ></div> 
</div>   
<div id="footer"></div>

2. Separate HTML tags and CSS style sheets

A good page should separate HTML tags and CSS style sheets. This is a principle that every web developer should know when they first come into contact with web development. However, until today, there are still many developers who do not strictly follow this principle.

Don’t embed style sheet code in HTML tags. Developers should develop a habit of creating separate files to store CSS style sheets. This will also make it easier for other developers to complete their work quickly when modifying your code.

<p style="color: #CCC; font-size:16px; font-family: arial">
    An example to illustrate inline style in html</p>

3. Optimize CSS code

Nowadays, it is very common to add multiple CSS files to a website. However, when a website contains too many CSS files, it will slow down the website's response speed. The solution is: streamline the code and optimize multiple CSS files and 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, etc.

For CSS, we have also recommended 9 excellent CSS frameworks that you should know. You can learn about the types and related usage of CSS frameworks.

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

Like CSS, it is also common to add multiple Javascript files to the page. But this will also reduce the response speed of the website. For this reason, developers should streamline and optimize these Javascript files.

But one thing is different from CSS, browsers usually do not support parallel loading. This means that when the browser loads a Javascript file, it will no longer load other content at the same time. This causes the page loading speed to seem to slow down.

A good solution is to put the loading order of Javascript files last. To achieve this, developers can place Javascript code at the bottom of the HTML document, and the best location is close to the 36cc49f0c466276486e50c850b7e4956 tag.

5. Make good use of title elements

4a249f0d628e2318394fd9b75b4636b1 to 4e9ee319e0fa4abc21ff286eeb145ecc These elements are used to highlight the key content of the page. This helps users focus more on the important parts of the page. For blogs, I (referring to the author of this article) recommend using the 4a249f0d628e2318394fd9b75b4636b1 tag to highlight the blog title. Because, the blog title is almost the most important part of the page.

<h1>This is the topmost heading</h1>
<h2>This is a sub-heading underneath the topmost heading.</h2>
<h3>This is a sub-heading underneath the h2 heading.</h3>

6. Use appropriate HTML tags in the right place

HTML tags are the key to constructing a standardized content structure. For example, the 907fae80ddef53131f3292ee4f81644b tag is used to emphasize important content. The e388a4556c0f65e1904146cc1a846bee tag is suitable for highlighting article paragraphs. If you want to add blank lines between paragraphs, don't use the df250b2156c434f3390392d09b1c9563 tag.

<em>emphasized text</em>
<strong>strongly emphasized text</strong>

7、避免滥用dc6dce4a544fdca2df29d5ac0ea9906b标签

并不是所有块元素都应该用dc6dce4a544fdca2df29d5ac0ea9906b标签来创建。例如,可以在内联元素的属性里添加display:block,将其以块元素的方式显示。

8、使用列表创建导航

使用ff6d136ddc5fdfeffaf53ff6ee95f185列表标签,再配以相应的CSS样式,可以创建美观的导航菜单。

9、别忘了封闭标签

现在,每当我回忆起在大学里学到的关于Web开发的第一堂课时,教授提到的HTML结构的重要性总是浮现在我的脑海。根据W3C标准,标签应该被封闭。那是因为,在一些浏览器下,如果没有按照标准来将标签封闭,会出现显示不正常的问题。而这一情况在IE6、7和8里尤为明显。

10、标签小写语法

标签采用小写语法是一项行业标准。虽然大写语法并不影响页面的显示效果,但是,代码的可读性很差。下面这段代码可读性就非常差:

<DIV>
<IMG SRC="images/demo_image.jpg" alt="demo image"/>
<A HREF="#" TITLE="click here">Click Here</A>
<P>some sample text</P>
</DIV>

11、为图片标签添加alt属性

在a1f02c36ba31691bcfe87b2722de723b标签里,alt属性通常非常有用。因为搜索引擎通常无法直接抓取图片文件。但是,如果开发者在alt属性里添加了图片的描述内容,将会方便搜索引擎的抓取。

<!-- has an alt attribute, which will validate, but alt value is meaningless -- >
<img id="logo" src="images/bgr_logo.png" alt="brg_logo.png" />
 
<!-- The correct way -- >
<img id="logo" src="images/bgr_logo.png" alt="Anson Cheung - Web Development" />

12、在表格里使用2e1cf0710519d5598b1f0f14c36ba674 和 2b5469ab79cf842344327415c3b3bb95

为了提高代码质量,并让用户容易理解表格内容,我们应该用2e1cf0710519d5598b1f0f14c36ba674 和 2b5469ab79cf842344327415c3b3bb95标签创建表格元素。

<fieldset>
    <legend>Personal Particular</legend>
    <label for="name">Name</label><input type="text" id="name" name="name" />
    <label for="email">E-mail</label><input type="text" id="email" name="email" />
    <label for="subject">Subject</label><input type="text" id="subject" name="subject" />
    <label for="message" >Message Body</label>
  <textarea rows="10" cols="20" id="message" name="message" ></textarea>
</fieldset>

13、将浏览器兼容代码标明信息并相互分开

对一名Web开发者来说,跨浏览器兼容是一个被重点关注的问题。通常,开发者会针对不同的浏览器来编码,也即是CSS hack。但是,如果开发者在编码时,能注明代码为哪一个版本的浏览器所写,会为以后的维护工作带来极大方便。下面就是一个很好的示例:

<!--[if IE 7]>
<link rel="stylesheet" href="css/ie-7.css" media="all">
<![endif]-->
<!--[if IE 6]>
<link rel="stylesheet" href="css/ie-6.css" media="all">
<script type="text/javascript" src="js/DD_belatedPNG_0.0.8a-min.js"></script>
<script type="text/javascript">
        DD_belatedPNG.fix(&#39;#logo&#39;);
</script>
<![endif]-->

14、避免过度注释

作为一名开发者,在代码中添加注释是一个好习惯,能方便理解并易于维护。这在其它编程语言如PHP、JAVA 和 C#里很普遍。但是,HTML/XHTML是文本标记语言,非常容易理解。因此,无需为每行代码都添加注释。

15、测试代码

It is recommended that developers use the W3C text markup verification service to test code. It is an efficient testing tool that can help you find errors in your pages. Moreover, it can also help you locate the corresponding code starting from the page error. This is often difficult to do after coding is complete. However, developers need to note that code that passes verification is not code with excellent performance.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn