search
HomeWeb Front-endHTML TutorialWrite high-performance html web applications

How can you improve web page performance?

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

 Poor HTML is always ignored, even though it has always been the core language of the Internet.

 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 page of youtube.com, there are as many as 3,500 HTML elements.

Reducing the complexity of HTML and the number of elements on a page does not significantly improve parsing time - but HTML is a critical factor in building extremely fast web pages, adapting to different devices and affecting success.

Write high-performance html web applications

In this article, you will learn how to write concise and clean HTML, allowing you to create a website that loads quickly and supports multiple devices, and will be easy to debug and maintain.

  There is no one way to write code - especially HTML. This is just a general experience, but it is not the only right choice.

 HTML, CSS and JavaScript

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

 HTML should not be used to display styles and styles. Don’t put text in title tags (h1~h6) to appear “bigger” or use blockquotes elements just for 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 style: Firefox, Internet Explorer and Opera are all different. For example, in Chrome the h1 element is rendered to a size of 32px by default.

  Three basic principles:

Use HTML to express structure, and CSS to express different styles and themes. JavaScript to respond to user actions.

  Use HTML, resort to CSS when necessary, and add JavaScript when necessary. For example: In many cases, you might use HTML forms for validation and CSS or SVG for animations.

  Separate CSS and JavaScript from your HTML code. Making them cacheable makes the code easier to debug. In production, CSS and JavaScript can be minified and merged and should be included as part of your Build system. Note* See JavaScript Construction (Compilation) System Competition

Document document structure

Use HTML5 document type:

<!DOCTYPE html>
<html>
<head>
 <title>Recipes: pesto</title>
</head>
<body>
  <h1 id="Pesto">Pesto</h1>
  <p>Pesto is good!</p>
</body>
</html>

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

<head>
  <title>My pesto recipe</title>
  <link rel="/css/global.css">
  <link rel="css/local.css">
</head>

In this way, the browser This allows you to preload styles before parsing the HTML without rendering a confusing page layout.

Put JavaScript at the very bottom of the page, before the body is closed. This will improve page rendering time because the browser can render the page before the JavaScript is loaded:

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

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

index.html:
<head>
  ...
  <script src="js/local.js">
</head>
<body onload="init()">
  ...
  <button onclick="handleFoo()">Foo</button>
  ...
</body>

 This is much better:

<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();

Legal HTML

 A major factor in the success of web pages is that browsers can handle invalid HTML. Browsers also have some standardized rules for how to render invalid code.

 However, this is no reason for you to let it go. Valid HTML is easier to debug, tends to have smaller file sizes, is faster, and uses fewer resources because it renders faster. Invalid HTML makes responsive design difficult to implement.

It is especially important to write valid HTML 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 document type.

Be sure to keep your HTML hierarchical: nest elements correctly and make sure there are no unclosed elements. It helps debuggers add comments.

<div id="foobar">
...
</div> <!-- foobar ends -->

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

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

But the following writing can avoid errors 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 It does not have to be closed. Some very smart programmers will write it like this. In any case, the list element (ul) must be closed.

<ul>
  <li>Basil
  <li>Pine nuts
  <li>Garlic
</ul>

  有一点你必须注意video和audio元素。他们不是自封闭的:

<!-- 错误: liable to cause layout grief -->
<video src="foo.webm" />
<!-- 正确 -->
<video src="foo.webm">
  <p>Video element not supported.</p>
</video>

  相反,通过删除不必要的代码HTML页面会变得更干净

  没有必要为自封闭元素添加"/",像img等

  设置属性是没有值的,如果不加属性的话(这种情况下,它不会自动播放,没有控制控件),

  video,它是没有任何属性的

<video src="foo.webm">

  下面两种更好

<video src="foo.webm" autoplay="false" controls="false">
<video src="foo.webm" autoplay="true" controls="true">

  这种可读性更强

<video src="foo.webm" autoplay controls>

  stylet和script标签不需要type属性;默认就是css和javascript

  优化协议地址更好(去除置http或https,它会根据当前协议自动配)

<a href="//en.wikipedia.org/wiki/Tag_soup">Tag soup</a>

  增强可读性,如,第一眼看上去就像是个标题

<h2 id="a-nbsp-href-contact-Contact-a-h-pre-p-而这种则像个链接-p-pre-class-brush-php-toolbar-false-a-nbsp-href-contact-h-Contact-h-a-pre-p-应该使用小写-p-pre-class-brush-php-toolbar-false-A-nbsp-HREF-Home-A-pre-p-大小写混合看上去更恶心-p-pre-class-brush-php-toolbar-false-H-Pesto"><a href="/contact">Contact</a><h2>

  而这种则像个链接

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

  应该使用小写

<A HREF="/">Home</A>

  大小写混合看上去更恶心

<H2>Pesto</h2>

语义标记

  “语义”意思是跟含义相关

  HTML应该标记有意义的内容:元素和描述的内容相符。

  HTML5引入了一些新的‘语义元素’像

,

  使用正确的元素表达正确的内容对于可访问性是有帮助的。

  使用

,

代表标题,
    代表lists

      注意

    的标题应该以

    开始

      使用

    ,
    ,

      使用

    写正文

      使用 代替 表示强调

      表单使用

      混合文字和元素会导至布局的问题

    <div>Name: <input type="text"></div>

      最好用下面的表示

    <div><label>Name:</label><input type="text"></div>

     布局

      HTML应该使用有意义的组织结构,而不是通过样式来实现。

      使用

    元素代表文本,而不是用来布局。

      避免使用
    来换行,使用块级元素和CSS来代替。

      避免使用水平分隔线


    。使用CSS的border样式来控制。

      不要使用不必要的DIV。W3C对DIV的定义是排序的是最后一个元素。

      要了解哪些元素是块级元素,避免在DIV中放置不必要的块级元素。将一个list放到div中是没有必要的。

      不要使用table来布局。

      Flex box是被广泛推荐的,能用就用吧。

      使用CSS的padding和margin,理解盒子模型。

     CSS

      这篇文章是关于HTML的,但是这里有一些基本的CSS小贴士。

      避免内嵌的CSS。出于性能考虑,CSS可以在BUILD时内嵌到你的网页中。

      避免ID出现重复。

      如果你想对多个元素应用某个样式,那么请使用class,在父级元素上使用class比在子级上好:

    <!-- 有点笨 :( -->
    <ul>
      <li class="ingredient">Basil</li>
      <li class="ingredient">Pine nuts</li>
      <li class="ingredient">Garlic</li>
    </ul>
    <!-- 更好 :) -->
    <ul class="ingredients">
      <li>Basil</li>
      <li>Pine nuts</li>
      <li>Garlic</li>
    </ul>

    可访问性

      使用语义元素

      提供向后兼容

      在链接上添加title属性,而且应该避免与link文本出现相同的内容

      在输入元素上添加type和placeholder属性

      原文地址: samdutton.wordpress.com


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
What is the difference between an HTML tag and an HTML attribute?What is the difference between an HTML tag and an HTML attribute?May 14, 2025 am 12:01 AM

HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

The Future of HTML: Evolution and TrendsThe Future of HTML: Evolution and TrendsMay 13, 2025 am 12:01 AM

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.