Must-Know HTML Optimization Skills_HTML/Xhtml_Web Page Production

WBOY
Release: 2016-05-16 16:36:06
Original
1515 people have browsed it

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:

  • Structural separation: use HTML to add structure, not style content;
    Keep it tidy: add code verification tools to workflow; use tools or style wizards to maintain code structure and format
    Learn New language: Get element structure and semantic markup.
    Ensure accessibility: Use ARIA attributes, Fallback attributes, etc.
    Test: Make the website run well on multiple devices, use emulators and performance tools.

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:

  • Use HTML to construct the page structure, CSS to modify the page presentation, and JavaScript to implement the page functions. CSS ZenGarden demonstrates behavioral separation very well.
    Use less HTML code if it can be implemented with CSS or JavaScript.
    Store CSS and JavaScript files separately from HTML. This can help with caching and debugging.

The document structure can also be optimized, as follows:

1. Use HTML5 document type, the following is an empty file:

XML/HTML CodeCopy content to clipboard
  1. >
  2. <html>
  3. <head>
  4. <title>Recipes: pesto title>
  5. head>
  6. <body>
  7. <h1>Pestoh1>
  8. <p>Pesto is good!p>
  9. body>
  10. html>

2. Quote the CSS file at the beginning of the document, as follows:

XML/HTML CodeCopy content to clipboard
  1. <head>
  2. <title>My pesto recipe title>
  3. <link rel="stylesheet" href="/css/global.css">
  4. <link rel="stylesheet" href="css/local.css">
  5. head>

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.

XML/HTML CodeCopy content to clipboard
  1. <body>
  2. ...
  3. <script src="/js /global.js">
  4. <script src="js/ local.js">
  5. body>

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:

XML/HTML CodeCopy content to clipboard
  1. <head>  
  2.      
  3.   ...   
  4.   
  5.   <script src="js/local.js">  
  6.   
  7. head>  
  8.   
  9. <body onload="init()">  
  10.   
  11.   ...   
  12.   
  13.   <button onclick="handleFoo()">Foobutton>  
  14.   
  15.   ...   
  16.   
  17. body>  
  18.    

下面的写法比较好:

index.html:

XML/HTML Code复制内容到剪贴板
  1. <head>  
  2.   
  3.   ...   
  4.   
  5. head>  
  6.   
  7. <body>  
  8.   
  9.   ...   
  10.   
  11.   <button id="foo">Foobutton>  
  12.   
  13.   ...   
  14.   
  15.   <script src="js/local.js">  
  16.   
  17. body>  
  18.   

js/local.js:

JavaScript Code复制内容到剪贴板
  1. init();
  2. var fooButton =
  3. document.querySelector('#foo');
  4. fooButton.onclick = handleFoo();

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:

  • Add validation functionality to your workflow: Use validation plugins like HTMLHint or SublineLinter to help you detect code errors.
    Use HTML5 document type
    Ensure that the hierarchical structure of HTML is easy to maintain and avoid nesting elements in a left-open state.
    Be sure to add the closing tag of each element.
    Remove unnecessary code; there is no need to add closing tags for self-closing elements; Boolean attributes do not need to be assigned a value, and are True if present;

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

,
and
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!