Home > Article > Web Front-end > Must-Know HTML Optimization Skills_HTML/Xhtml_Web Page Production
How to improve the performance of web pages, many developers start from many aspects, such as JavaScript, image optimization, server configuration, file compression or adjusting CSS.
It is clear that HTML has reached a bottleneck, even though it is an essential core language for developing Web interfaces. The load of HTML pages is also getting heavier and heavier. Most pages require an average of 40K of space. For example, some large websites contain thousands of HTML elements, and the page size will be larger.
How to effectively reduce the complexity of HTML code and the number of page elements. This article mainly solves this problem. It introduces how to write concise and clear HTML code from many aspects, which can make the page load faster and enable Works well across multiple devices.
The following principles need to be followed during the design and development process:
The relationship between HTML, CSS and JavaScript
HTML is a markup language used to adjust the structure and content of pages. HTML cannot be used to modify style content, nor can you enter text content in the header tag, making the code lengthy and complex. Instead, it is more appropriate to use CSS to modify layout elements and appearance. The default appearance of HTML elements is defined by the browser's default style sheet. For example, in Chrome, the h1 tag element will be rendered into a 32px Times bold font.
Three universal design rules:
The document structure can also be optimized, as follows:
1. Use HTML5 document type, the following is an empty file:
2. Quote the CSS file at the beginning of the document, as follows:
Using these two methods, the browser will prepare the CSS information before parsing the HTML code. This helps improve page loading performance.
Enter JavaScript code before the body closing tag at the bottom of the page. This will help improve the page loading speed, because the browser will load the page before parsing the JavaScript code. Using JavaScript will have a positive impact on page elements.
Use Defer and async attributes. Script elements with async attributes are not guaranteed to be executed in order.
Handlers can be added in JavaScript code. Never add it to HTML inline code. For example, the following code can easily lead to errors and is difficult to maintain:
index.html:
下面的写法比较好:
index.html:
js/local.js:
Verification
One way to optimize your web pages is for your browser to handle illegal HTML code. Legal HTML code is easy to debug, takes up less memory, consumes less resources, is easy to parse, renders and runs faster. Illegal HTML code makes it extremely difficult to implement responsive design.
When using templates, legal HTML code is extremely important. It often happens that templates run well alone, but when integrated with other modules, various errors are reported. Therefore, the quality of HTML code must be ensured. You can take The following measures:
Code format
Format consistency makes HTML code easy to read, understand, optimize, and debug.
Semantic tags
Semantics refers to things related to meaning. HTML can see semantics from the content of the page: the naming of elements and attributes expresses the role and function of the content to a certain extent. HTML5 introduces new semantic elements such as
Choosing the right elements to write your code ensures the readability of your code:
For example:
It would be better to write it in another way:
1:
2:
3:
Layout
To improve the performance of HTML code, follow HTML code to achieve functionality and goals, not style.
element to modify the text instead of the layout; the default
automatically provides edges, and other styles are also provided by the browser by default.
Avoid using
lines, you can use block elements or CSS display attributes instead.
Avoid using
CSS
Although this article explains how to optimize HTML, here are some basic skills for using CSS:
The above are the techniques for optimizing HTML code introduced in this article. A high-quality and high-performance website often depends on the handling of details