11 rules to keep your code tidy
Writing Web pages is like building a house. The foundation will be solid so that the house will not fall down. The same is true when we make Web pages. A good HTML structure is the beginning of making a beautiful website. Similarly, good CSS only exists in equally good HTML, so the advantages of a clean and semantic HTML are many. So in daily production, have we achieved this? Let’s look at a picture together:
The picture above shows two pieces of code. I think everyone will only like the first one. Let’s not talk about its semantics first. At least its structure makes us look refreshing. But what about the second one? Code that looks like bad code at first glance, code that makes people hate it. So how can we write good code, clean code? Let's learn from the following twelve aspects together. As long as you can adhere to the following twelve principles when writing code in the future, you can ensure that the quality of your code will improve, and the code you write will be loved by everyone.
1. Statement of DOCTYPE
If we want to do something well, we must first know what rights we have to do it. Just like the "DOCTYPE" statement, we do not need to discuss whether to use HTML4.01 or XHTML1.0 or the current HTML5 provides strict Version or transitional version, these can well support the code we write:
Since our current layout can make a good layout without table layout, we can consider not using transitional type but using strict type "DOCTYPE". For backward compatibility, I recommend using HTML5 declaration mode:
If you want to know more about this, you can click:
W3C: Recommended DTDs to use in your Web document
Fix Your Site With the Right DOCTYPE!
No more Transitional DOCTYPEs, please
2. Character set and encoding characters
At the beginning of each page, we set the character set in
. We all use "UTF-8" here<meta charset="UTF-8" />
And when we usually write on the page, we often encounter symbols like "&", so we should not write "&" directly on the page:
We should use character encoding in the code to achieve this. For example, "&" we should use "&" to replace it in the code.
If you want to know more about this, you can click:
Wikipedia: UTF-8
A tutorial on character code issues
The Extended ASCII table
# 3. Correct code indentation
In page editing, whether the indentation of the code is correct will not affect any function of your website, but if you do not have a standardized indentation principle, the people who read your code will be very angry, so the correct code Indentation improves the readability of your code. The indentation of a standard program should be a tab character (or a few spaces). To make it more vivid, let’s look at the picture at the beginning of the article, or look at the picture shown below. After reading it, you will know how to use it in the future. How to write the code so that people can read it happily:
Needless to say, everyone will like the code below. This is just a matter of one's habits, but it is recommended to do it well from the beginning to benefit others and yourself. For an introduction to this aspect, you can also refer to: Clean up your Web pages with HTML TIDY.
IV. External links to your CSS styles and Javascript scripts
There are many ways to write CSS styles in the page. Some of them directly put the style into the "
" of the page. This will be a very bad habit, because this will not only mess up our markup, but also these styles. Only suitable for this HTML page. Therefore, we need to separate the CSS and save it externally, so that subsequent pages can also link to these styles. If your page needs to be modified, we only need to modify the style file. As shown in the picture below:
What we are talking about above is just styles. In fact, JavaScript scripts are the same as CSS styles. With pictures and texts, what I ultimately want to express is "When making web pages, try to put your CSS styles and javascript scripts in a separate file, and then reference these files through links. This way The biggest advantage is that it facilitates the management and modification of your styles and scripts”
. 5. Correct tag nesting
When we write HTML, we always need the hierarchical nesting of tags to help us complete the writing of HTML, but there are certain rules for the nesting of these HTML. If we want to explain it in detail, we may need to use several chapters to describe it. So what I want to say here today is that we should not make the following super mistakes when writing HTML:
The structure in the picture above is common to us. For example, the title of the homepage, then we should pay attention to it. We cannot put "
" in the "" tag. In other words, we cannot put any elements. and in inline elements. The above is just an example, I just hope that everyone should not make such super mistakes in daily production.
6. Delete unnecessary tags
First, let’s take a look at a screenshot of an example:

The above picture is obviously the production of a navigation menu. In the example above: there is a "p#topNav" wrapping the list "ul#bigBarNavigation", and the "p" and "ul" lists are both block elements, plus "p" used here to wrap "ul" has no effect at all. Although the appearance of "p" has brought us great benefits in making web pages, we don't need to use it everywhere. I wonder if you usually pay attention to such details? I made such a mistake. If you have also had such an experience, then starting from today, from now on, we will work together to correct such mistakes.
For information on how to use labels correctly, if you are interested, you can click: pitis: what it is, and how to cure it.
Seven. Use better naming
The naming mentioned here is to define class names or ID names for relevant elements in your page. Many students have this habit. For example, if an element has a red font, add "red" to it, or even layout They all write "left-sidebar", etc., but have you ever thought about what if this element is defined with "red" and the customer asks for "blue" in a few days? In other words, the "left-sidebar" sidebar at that time no longer wanted to be placed on the left, but on the right. In this way, our previous naming can be said to have no meaning at all, as shown in the picture below Shown:

Then it is very important to define a good name. Not only can I understand your code, but others can also easily understand your code. For example, a good class name and ID name "mainNav", "subNav", " footer" etc., he can describe what is involved. The bad ones are as mentioned above.
If you want to know more about this, you can click:
Standardizing CSS class and id names
CSS Tip #2: Structural Naming Convention in CSS
CSS coding: semantic approach in naming convention
CSS Naming Conventions and Coding Style
Eight, leave the version of CSS
When we design menus, we sometimes require the text of all menu options to be in uppercase. Do you usually set them to uppercase directly in the HTML tag? If so, I think it's not good. For better scalability in the future, we shouldn't set them to all uppercase in HTML. A better solution is to achieve it through CSS:

9. Define the class name or ID name of
I don’t know if you have encountered such a problem when making web pages. The entire website uses the same layout and structure. In other words, you use the same structure and the same class name in the layout of the page. But suddenly your supervisor says that in response to customer demand, the layout of a page needs to be swapped between the sidebar and the main content. At this time, you don’t want to modify the structure of the entire page just to change the layout. A good solution is to define a special class name or ID name in the "
" of your page. , so that you can easily achieve what you want. I don’t know if you have used this method before:

Defining a unique class and ID name for "
" is very powerful, not only to help you change the layout like above, but the most important thing is that sometimes it can help you implement a certain part of the page to achieve special effects. It does not affect the performance of other pages. I don’t need to explain why there is such a function. I think everyone knows it. Because the content of each page is the descendant element of "".
If you want to know more about this, you can click:
ID Your Body For Greater CSS Control and Specificity
Case study: Re-using styles with a body class
10. Verify your code
People will inevitably make mistakes, and it's the same when we write code. Sometimes you will always write in lowercase or more words. For example, you forget to close your element tags, and you don't remember to write the necessary attributes of the elements. Although there are some mistakes that will not be given. No matter what disastrous consequences you bring, it will also bring you mistakes that you cannot predict. Therefore, it is recommended that you verify your code after you finish writing it. Verified code is always better than unverified code:

In order to effectively verify your code, we can use relevant tools or browser plug-ins to help us complete it. If there are no errors in your code, the W3C verification tool will display green text in front of you, which makes you extremely excited, because it once again proves that the code you wrote can withstand W3C standards.
If you want to know more about this, you can click:
The W3C Markup Validation Service
XHTML-CSS Validator
Free Site Validator (checks entire site, not just one page)
11. Logical sequence
This is a rare error situation, because I think everyone will not disrupt the logical order when writing pages. In other words, if possible, it is best to have a logical order for your website, such as first Write the header, then the body, and finally the footer. Of course, sometimes we encounter special circumstances. The footer part is above the sidebar of our code. This may be because it best suits your website design needs. This may be understandable, but if you have other ways to implement it, we You should put the footer at the end of a page, and then use specific techniques to make it meet your design needs:

Above we discussed several ways to get you started writing clean HTML code. From the beginning of a project, this is very easy, but if you need to fix an existing code, it will be more or less difficult. What I said above is mainly to tell you how to learn to write a good, clean HTML code and stick to it. I hope that after reading this article, you can start from scratch and insist on writing a clean HTML code in your next project.
Related documents: 10 most common HTML tag mistakes
English source: 12 Principles For Keeping Your Code Clean
The above is the detailed content of 11 rules to keep your code tidy. For more information, please follow other related articles on the PHP Chinese website!


Standardizing CSS class and id names
CSS Tip #2: Structural Naming Convention in CSS
CSS coding: semantic approach in naming convention
CSS Naming Conventions and Coding Style


ID Your Body For Greater CSS Control and Specificity
Case study: Re-using styles with a body class

The W3C Markup Validation Service
XHTML-CSS Validator
Free Site Validator (checks entire site, not just one page)


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Some users encountered errors when installing the device, prompting error code 28. In fact, this is mainly due to the driver. We only need to solve the problem of win7 driver code 28. Let’s take a look at what should be done. Do it. What to do with win7 driver code 28: First, we need to click on the start menu in the lower left corner of the screen. Then, find and click the "Control Panel" option in the pop-up menu. This option is usually located at or near the bottom of the menu. After clicking, the system will automatically open the control panel interface. In the control panel, we can perform various system settings and management operations. This is the first step in the nostalgia cleaning level, I hope it helps. Then we need to proceed and enter the system and

What to do with blue screen code 0x0000001? The blue screen error is a warning mechanism when there is a problem with the computer system or hardware. Code 0x0000001 usually indicates a hardware or driver failure. When users suddenly encounter a blue screen error while using their computer, they may feel panicked and at a loss. Fortunately, most blue screen errors can be troubleshooted and dealt with with a few simple steps. This article will introduce readers to some methods to solve the blue screen error code 0x0000001. First, when encountering a blue screen error, we can try to restart

Solve the "error:expectedinitializerbefore'datatype'" problem in C++ code. In C++ programming, sometimes we encounter some compilation errors when writing code. One of the common errors is "error:expectedinitializerbefore'datatype'". This error usually occurs in a variable declaration or function definition and may cause the program to fail to compile correctly or

The win10 system is a very excellent high-intelligence system. Its powerful intelligence can bring the best user experience to users. Under normal circumstances, users’ win10 system computers will not have any problems! However, it is inevitable that various faults will occur in excellent computers. Recently, friends have been reporting that their win10 systems have encountered frequent blue screens! Today, the editor will bring you solutions to different codes that cause frequent blue screens in Windows 10 computers. Let’s take a look. Solutions to frequent computer blue screens with different codes each time: causes of various fault codes and solution suggestions 1. Cause of 0×000000116 fault: It should be that the graphics card driver is incompatible. Solution: It is recommended to replace the original manufacturer's driver. 2,

If you need to program any device remotely, this article will help you. We will share the top GE universal remote codes for programming any device. What is a GE remote control? GEUniversalRemote is a remote control that can be used to control multiple devices such as smart TVs, LG, Vizio, Sony, Blu-ray, DVD, DVR, Roku, AppleTV, streaming media players and more. GEUniversal remote controls come in various models with different features and functions. GEUniversalRemote can control up to four devices. Top Universal Remote Codes to Program on Any Device GE remotes come with a set of codes that allow them to work with different devices. you may

Termination Code 0xc000007b While using your computer, you sometimes encounter various problems and error codes. Among them, the termination code is the most disturbing, especially the termination code 0xc000007b. This code indicates that an application cannot start properly, causing inconvenience to the user. First, let’s understand the meaning of termination code 0xc000007b. This code is a Windows operating system error code that usually occurs when a 32-bit application tries to run on a 64-bit operating system. It means it should

As a programmer, I get excited about tools that simplify the coding experience. With the help of artificial intelligence tools, we can generate demo code and make necessary modifications as per the requirement. The newly introduced Copilot tool in Visual Studio Code allows us to create AI-generated code with natural language chat interactions. By explaining functionality, we can better understand the meaning of existing code. How to use Copilot to generate code? To get started, we first need to get the latest PowerPlatformTools extension. To achieve this, you need to go to the extension page, search for "PowerPlatformTool" and click the Install button

Blue screen is a problem we often encounter when using the system. Depending on the error code, there will be many different reasons and solutions. For example, when we encounter the problem of stop: 0x0000007f, it may be a hardware or software error. Let’s follow the editor to find out the solution. 0x000000c5 blue screen code reason: Answer: The memory, CPU, and graphics card are suddenly overclocked, or the software is running incorrectly. Solution 1: 1. Keep pressing F8 to enter when booting, select safe mode, and press Enter to enter. 2. After entering safe mode, press win+r to open the run window, enter cmd, and press Enter. 3. In the command prompt window, enter "chkdsk /f /r", press Enter, and then press the y key. 4.
