Home Web Front-end Front-end Q&A 15 coding principles that web developers must know

15 coding principles that web developers must know

Nov 24, 2016 pm 03:17 PM
web Developer coding

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>
Copy after login

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>
Copy after login

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 tag.

5. Make good use of title elements

to

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

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>
Copy after login

6. Use appropriate HTML tags in the right place

HTML tags are the key to constructing a standardized content structure. For example, the tag is used to emphasize important content. The

tag is suitable for highlighting article paragraphs. If you want to add blank lines between paragraphs, don't use the
tag.

<em>emphasized text</em>
<strong>strongly emphasized text</strong>
Copy after login

7、避免滥用

标签

并不是所有块元素都应该用

标签来创建。例如,可以在内联元素的属性里添加display:block,将其以块元素的方式显示。

8、使用列表创建导航

使用

    列表标签,再配以相应的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>
    Copy after login

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

    标签里,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" />
    Copy after login

    12、在表格里使用

    为了提高代码质量,并让用户容易理解表格内容,我们应该用

    标签创建表格元素。

    <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>
    Copy after login

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

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

    <!--[if IE 7]>
    <link rel="stylesheet" href="css/ie-7.css" media="all">
    <![endif]-->
    Copy after login
    <!--[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]-->
    Copy after login

    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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Knowledge graph: the ideal partner for large models Knowledge graph: the ideal partner for large models Jan 29, 2024 am 09:21 AM

Large language models (LLMs) have the ability to generate smooth and coherent text, bringing new prospects to areas such as artificial intelligence conversation and creative writing. However, LLM also has some key limitations. First, their knowledge is limited to patterns recognized from training data, lacking a true understanding of the world. Second, reasoning skills are limited and cannot make logical inferences or fuse facts from multiple data sources. When faced with more complex and open-ended questions, LLM's answers may become absurd or contradictory, known as "illusions." Therefore, although LLM is very useful in some aspects, it still has certain limitations when dealing with complex problems and real-world situations. In order to bridge these gaps, retrieval-augmented generation (RAG) systems have emerged in recent years. The core idea is

Several common encoding methods Several common encoding methods Oct 24, 2023 am 10:09 AM

Common encoding methods include ASCII encoding, Unicode encoding, UTF-8 encoding, UTF-16 encoding, GBK encoding, etc. Detailed introduction: 1. ASCII encoding is the earliest character encoding standard, using 7-bit binary numbers to represent 128 characters, including English letters, numbers, punctuation marks, control characters, etc.; 2. Unicode encoding is a method used to represent all characters in the world The standard encoding method of characters, which assigns a unique digital code point to each character; 3. UTF-8 encoding, etc.

What are web standards? What are web standards? Oct 18, 2023 pm 05:24 PM

Web standards are a set of specifications and guidelines developed by W3C and other related organizations. It includes standardization of HTML, CSS, JavaScript, DOM, Web accessibility and performance optimization. By following these standards, the compatibility of pages can be improved. , accessibility, maintainability and performance. The goal of web standards is to enable web content to be displayed and interacted consistently on different platforms, browsers and devices, providing better user experience and development efficiency.

How to enable administrative access from the cockpit web UI How to enable administrative access from the cockpit web UI Mar 20, 2024 pm 06:56 PM

Cockpit is a web-based graphical interface for Linux servers. It is mainly intended to make managing Linux servers easier for new/expert users. In this article, we will discuss Cockpit access modes and how to switch administrative access to Cockpit from CockpitWebUI. Content Topics: Cockpit Entry Modes Finding the Current Cockpit Access Mode Enable Administrative Access for Cockpit from CockpitWebUI Disabling Administrative Access for Cockpit from CockpitWebUI Conclusion Cockpit Entry Modes The cockpit has two access modes: Restricted Access: This is the default for the cockpit access mode. In this access mode you cannot access the web user from the cockpit

Tmall Elf Cloud access service upgrade: free developer charges Tmall Elf Cloud access service upgrade: free developer charges Jan 09, 2024 pm 10:06 PM

According to news from this site on January 9, Tmall Elf recently announced the upgrade of Yunyun access service. The upgraded Yunyun access service will change from free mode to paid mode starting from January 1. This site comes with new features and optimizations: optimizing the cloud protocol to improve the stability of device connections; optimizing voice control for key categories; account authorization upgrade: adding the display function of developer third-party apps in Tmall Genie to help users update faster It is convenient for account binding. At the same time, the third-party App account authorization for developers has been added to support one-click binding of Tmall Elf accounts; the terminal screen display interaction capability has been added. In addition to voice interaction, users can control devices and obtain information through the app and screen speakers. Equipment status; new intelligent scene linkage capabilities, new product attributes and events, which can be reported as status or events to define Tmall

what does web mean what does web mean Jan 09, 2024 pm 04:50 PM

The web is a global wide area network, also known as the World Wide Web, which is an application form of the Internet. The Web is an information system based on hypertext and hypermedia, which allows users to browse and obtain information by jumping between different web pages through hyperlinks. The basis of the Web is the Internet, which uses unified and standardized protocols and languages ​​to enable data exchange and information sharing between different computers.

Is PHP front-end or back-end in web development? Is PHP front-end or back-end in web development? Mar 24, 2024 pm 02:18 PM

PHP belongs to the backend in web development. PHP is a server-side scripting language, mainly used to process server-side logic and generate dynamic web content. Compared with front-end technology, PHP is more used for back-end operations such as interacting with databases, processing user requests, and generating page content. Next, specific code examples will be used to illustrate the application of PHP in back-end development. First, let's look at a simple PHP code example for connecting to a database and querying data:

Build conversational AI into your web applications Build conversational AI into your web applications Nov 02, 2023 am 11:04 AM

In this article, we will explore the possibilities and benefits of integrating ChatGPT into a ReactJS application, along with step-by-step instructions on how to do so.

See all articles