Home  >  Article  >  Web Front-end  >  HTML optimization tips you should know

HTML optimization tips you should know

高洛峰
高洛峰Original
2017-03-12 17:18:131181browse

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 can run well on a variety of 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 validation tools to your workflow; use tools or style wizards to maintain code structure and format

  • Learn a new language: Get element structure and semantic markup.

  • Ensure accessibility: Use ARIA attributes and Fallback attributes, etc.

  • Test: Make the website run well on multiple devices and can be used emulators and performance tools.

HTML optimization tips you should know

The relationship between HTML, CSS and JavaScript

HTML is a markup language used to adjust the structure and content of the page. 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. HTML ElementThe default appearance 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 general design rules:

  1. Use HTML to construct the page structure, CSS to modify the page presentation, and JavaScript to implement the page function. CSS ZenGarden demonstrates behavioral separation very well.

  2. If it can be achieved with CSS or JavaScript, use less HTML code.

  3. Store CSS and JavaScript files separately from HTML. This can aid caching and debugging.

The document structure can also be optimized, as follows:

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

<!DOCTYPE html>
<html>

<head>
 <title>Recipes: pesto</title>
</head>

<body>

  <h1>Pesto</h1>

  <p>Pesto is good!</p>

</body>
</html>


  • Reference the CSS file at the beginning of the document, as follows :

<head>
  <title>My pesto recipe</title>

  <link rel="stylesheet" href="/css/global.css">
  <link rel="stylesheet" href="css/local.css">

</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 closing tag of the body 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. .

<body>

  ...

  <script src="/js/global.js">
  <script src="js/local.js">

</body>


Using 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:

<head>
  
  ...

  <script src="js/local.js">

</head>

<body onload="init()">

  ...

  <button onclick="handleFoo()">Foo</button>

  ...

</body>

The following way of writing is better:

index.html:


##

<head>

  ...

</head>

<body>

  ...

  <button id="foo">Foo</button>

  ...

  <script src="js/local.js">

</body>

js/local.js:


init();
var fooButton =
    document.querySelector(&#39;#foo&#39;);
fooButton.onclick = handleFoo();


Verification

One way to optimize a web page is that the browser can 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 report various errors when integrated with other modules. Therefore, the quality of HTML code must be ensured. , you can take the following measures:

  • Add validation functionality to your workflow: Use validation plug-ins such as 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 the left-open state

    .

  • Make 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, if they exist, they are True;
  • <video src="foo.webm" autoplay controls>
  • Code format

Format consistency makes HTML code easy to read, understand, optimize, and debug.

语义标记

语义指意义相关的事物,HTML 可从页面内容中看出语义:元素和属性的命名一定程度上表达了内容的角色和功能。HTML5 引入了新的语义元素,如

选择合适的元素来编写代码可保证代码的易读性:

  • 使用

    (

    ,

    …)表示标题,
      实现列表

  • 注意使用

    标签之前应添加

    标签;

  • 选择合适的HTML5语义元素如

    ,

  • 使用

    描述Body 文本,HTML5 语义元素可以形成内容,反之不成立。

  • 使用标签替代标签。

  • 使用

  • 将文本和元素混合,并作为另一元素的子元素,会导致布局错误,

例如:

<p>Name: <input type="text" id="name"></p>


换种写法会更好:


   1:  <p>
   2:    <label>Name:</label><input>
   3:  </p>


 <br>

布局

要提高HTML代码的性能,要遵循HTML 代码以实现功能和为目标,而不是样式。

  • 使用

    元素修饰文本,而不是布局;默认

    是自动提供边缘,而且其他样式也是浏览器默认提供的。

  • 避免使用
    分行,可以使用block元素或CSS显示属性来代替。

  • 避免使用


    来添加水平线,可使用CSS的border-bottom 来代替。
  • 不到关键时刻不要使用p标签。

  • 尽量少用Tables来布局。

  • 可以多使用Flex Box

  • 使用CSS 来调整边距等。

CSS

虽然本文讲解的是如何优化HTML,下面介绍了一些使用css的基本技能:

  • 避免内联css

  • 最多使用ID类 一次

  • 当涉及多个元素时,可使用Class来实现。

以上就是本文介绍的优化HTML代码的技巧,一个高质量高性能的网站,往往取决于对细节的处理,因此我们在日常开发中,能够考虑到用户体验,后期维护等方面,则会产生更高效的开发。在进行前端开发时,不忘借助开发工具来助力开发过程。新一代HTML5 / JavaScript UI控件Wijmo,大而全面的前端开发工具包,为企业应用提供更加灵活的操作体验,现已全面支持Angular 2。

The above is the detailed content of HTML optimization tips you should know. For more information, please follow other related articles on the PHP Chinese website!

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