Some tips for writing high-performance HTML applications

Some tips for writing high-performance HTML applications

How can you improve web page performance?

Most developers will optimize through JavaScript and images, through server configuration, compressing and concatenating files - even tweaking CSS (combining small images).

Poor HTML is often overlooked, even though it has always been the core language of the web.

HTML is getting bigger and bigger. Most of the HTML pages of the top 100 websites are around 40K. Amazon and Yahoo use thousands of HTML pages. On the main youtube.com page, there are 3,500 HTML elements.

Reducing HTML complexity and the number of elements on a page will not significantly improve parsing time - but HTML is a critical factor in building fast web pages that adapt to different devices and affect success.
In this article, you’ll learn how to write concise and clean HTML that will enable you to create websites that load quickly, support multiple devices, and will be easy to debug and maintain.

There's no single way to write code - especially HTML. This is just a general experience, but it is not the only correct choice.
HTML, CSS and JavaScript

HTML is a markup language used to represent structure and content.

HTML should not be used to display style and appearance. Don't write text in heading tags (h1-h6) just to make it appear "bigger" or use blockquotes just for the sake of indentation. Instead, use CSS to change the appearance and layout of elements.

The default appearance of HTML elements is achieved through the browser's default styles: Firefox, Internet Explorer, and Opera are all different. For example, in Chrome the default h1 element renders at a size of 32px.

Three basic principles:

HTML is used to represent the structure, and CSS is used to express different styles and themes. JavaScript to respond to user actions.

Use HTML, with CSS where necessary, and JavaScript when necessary. For example, in many cases you might use HTML forms for validation and CSS or SVG for animation.

Separate CSS and JavaScript from your HTML code. Allowing them to be cached makes the code easier to debug. In production, CSS and JavaScript can be minified and combined and should be part of your build system. Note* See JavaScript build (compilation) system competition Document structure

Using HTML5 document type:

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

Reference the CSS file at the top of the page, such as in the head element:

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

This way, the browser can preload the styles before parsing the HTML without rendering a messy page layout.

Put JavaScript at the very bottom of the page, before closing the body. This will increase page rendering time, as the browser can render the page before JavaScript is loaded:

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

Add event handling in JavaScript. Don't add it in HTML. This is very difficult to maintain, for example:

XML/HTML CodeCopy content to clipboard
  1. index.html:
  2.   
  3. < head >   
  4. ...
  5.    < script   src = "js/local.js" >   
  6.   
  7. </ head >   
  8.   
  9. < body   onload = "init()" >   
  10. ...
  11.    < button   onclick = "handleFoo()" > Foo </ button >   
  12. ...
  13. </ body >   

This is much better:

JavaScript CodeCopy content to clipboard
  1. <head>
  2. ...
  3. </head>
  4.   
  5. <body>
  6. ...
  7. <button id= "foo" >Foo</button>
  8. ...
  9. <script src= "js/local.js" >
  10. </body>
  11.   
  12. js/local.js:
  13.   
  14. init();
  15. var fooButton =
  16. document.querySelector( '#foo' );
  17. fooButton.onclick = handleFoo();

Valid HTML

A major factor in the success of Web pages is the browser's ability to process invalid HTML. Browsers also have some standardized rules for how to render invalid code.

But this is not a reason for you to let it go. Valid HTML is easier to debug, tends to be smaller files, faster, and takes up less resources because it renders faster. Invalid HTML makes responsive design difficult to implement.

Writing valid HTML is especially important when using templates.

Validate HTML in your BUILD system: Use validation plugins such as HTMLHint and SublimeLinter to check the syntax of your HTML.

Use HTML5 doctype.

Make sure to maintain the HTML hierarchy: nest elements correctly and make sure there are no unclosed elements. It can help debuggers add comments.

XML/HTML CodeCopy content to clipboard
  1. < div   id = "foobar" >   
  2. ...
  3. </ div >   <!-- foobar ends -->   

Be sure to add a closing tag after non-self-closing elements. For example, the following will work:

XML/HTML CodeCopy content to clipboard
  1. < p > Pesto is good to eat...
  2. < p > ...and pesto is easy to make.

But the following way of writing can avoid mistakes and make the paragraph hierarchy more obvious:

<p>Pesto is good to eat...</p>
<p>...and pesto is easy to make.</p>

The items element (li) does not have to be closed, some very smart programmers will write it like this, however, the list element (ul) must be closed.

XML/HTML CodeCopy content to clipboard
  1. < ul >   
  2.    < li > Basil
  3.    < li > Pine nuts
  4.    < li > Garlic
  5. </ ul >   

One thing you have to pay attention to is the video and audio elements. They are not self-enclosed:

XML/HTML CodeCopy content to clipboard
  1. <!-- Error: liable to cause layout grief -->   
  2. < video   src = "foo.webm"   />   
  3.   
  4. <!-- Correct -->   
  5. < video   src = "foo.webm" >   
  6.    < p > Video element not supported. </ p >   
  7. </ video >   

On the contrary, by removing unnecessary code, the HTML page will become cleaner.

There is no need to add "/" to self-enclosing elements, like img, etc.

Setting the property has no value, if you don't add the property (in this case, it will not play automatically and there are no controls),

video, which has no attributes

XML/HTML CodeCopy content to clipboard
  1. < video   src = "foo.webm" >   

The following two are better

XML/HTML CodeCopy content to clipboard
  1. < video   src = "foo.webm"   autoplay = "false"   controls = "false" >   
  2. < video   src = "foo.webm"   autoplay = "true"   controls = "true" >   

This is more readable

XML/HTML CodeCopy content to clipboard
  1. < video   src = "foo.webm" autoplay controls >   

The stylet and script tags do not require a type attribute; the default is css and javascript

It is better to optimize the protocol address (remove http or https, it will automatically configure according to the current protocol)

XML/HTML CodeCopy content to clipboard
  1. <   href = "//en.wikipedia.org/wiki/Tag_soup" > Tag soup </ a >   

Improve readability, e.g., it looks like a title at first glance

XML/HTML CodeCopy content to clipboard
  1. < h2 > < a   href = "/contact" > Contact </ a > < h2 >   

This is like a link

<a href="/contact"><h2>Contact</h1></a>

Should use lowercase

XML/HTML CodeCopy content to clipboard
  1. < A   HREF = "/" > Home </ A >   

Mixing uppercase and lowercase letters looks even worse.

XML/HTML CodeCopy content to clipboard
  1. < H2 > Pesto </ h2 >   

Semantic markup

"Semantics" means related to meaning

HTML should mark up meaningful content: elements should match the content they describe.

HTML5 introduces some new 'semantic elements' like <header>, <footer> and <nav>.

Using the right elements for the right content helps with accessibility.

Use <h1>, <h2>, <h3> for titles, <ul> or <ol> for lists

Note that the title of <article> should start with <h1>

Using <header>, <footer>, <nav> and <aside>

Use <p> to write the body text

Use <em> and <strong> instead of <i> and <b> for emphasis

The form uses the <label> element, input type

Mixing text and elements can cause layout problems

XML/HTML CodeCopy content to clipboard
  1. < div > Name: < input   type = "text" > </ div >   

It is best expressed as follows

XML/HTML CodeCopy content to clipboard
  1. < div > < label > Name: </ label > < input   type = "text" > </ div >   

layout

HTML should be structured with meaning, not style.

Use the <p> element for text, not layout.

Avoid using <br> to wrap lines, use block-level elements and CSS instead.

Avoid using horizontal dividers <hr>. Use CSS border styles to control.

Don't use unnecessary DIVs. W3C defines DIV as the last element in the sorting order.

Be aware of which elements are block-level elements and avoid placing unnecessary block-level elements inside a DIV. It is not necessary to put a list inside a div.

Don't use tables for layout.

Flex box is widely recommended, so use it if you can.

Use CSS padding and margin, and understand the box model.
CSS

This post is about HTML, but here are some basic CSS tips.

Avoid inline CSS. For performance reasons, CSS can be embedded into your web pages at BUILD time.

Avoid duplication of IDs.

If you want to apply a style to multiple elements, use a class, preferably on the parent element rather than on the children:

XML/HTML CodeCopy content to clipboard
  1. <!-- a bit stupid :( -->   
  2. < ul >   
  3.    <   class = "ingredient" > Basil </ li >   
  4.    <   class = "ingredient" > Pine nuts </ li >   
  5.    <   class = "ingredient" > Garlic </ li >   
  6. </ ul >   
  7.   
  8. <!-- Even better :) -->   
  9. < ul   class = "ingredients" >   
  10.    < li > Basil </ li >   
  11.    < li > Pine nuts </ li >   
  12.    < li > Garlic </ li >   
  13. </ ul >   

Accessibility

Using semantic elements

Provides backward compatibility

Add a title attribute to the link, and avoid using the same content as the link text.

Add type and placeholder attributes to the input element

<<:  Introduction to TypeScript interfaces

>>:  Mysql practical exercises simple library management system

Recommend

Web design dimensions and rules for advertising design on web pages

1. Under 800*600, if the width of the web page is...

Solving problems encountered when importing and exporting Mysql

background Since I converted all my tasks to Dock...

Element uses scripts to automatically build new components

Table of contents background How does element-ui&...

win10 docker-toolsbox tutorial on building a php development environment

Download image docker pull mysql:5.7 docker pull ...

Detailed tutorial on installing MySQL 5.7.20 on RedHat 6.5/CentOS 6.5

Download the rpm installation package MySQL offic...

Solution to the Chinese garbled characters problem in MySQL under Ubuntu

Find the problem I have been learning Django rece...

Detailed explanation of using grep command in Linux

Linux grep command The Linux grep command is used...

Detailed explanation of Angular dynamic components

Table of contents Usage scenarios How to achieve ...

Convert XHTML CSS pages to printer pages

<br />In the past, creating a printer-friend...

Bootstrap 3.0 study notes grid system principle

Through the brief introduction in the previous tw...

Native JavaScript implementation of progress bar

The specific code for JavaScript to implement the...

Docker configuration Alibaba Cloud image acceleration pull implementation

Today I used docker to pull the image, but the sp...

Vue+Bootstrap realizes a simple student management system

I used vue and bootstrap to make a relatively sim...