Home > Article > Web Front-end > How to write efficient HTML_html/css_WEB-ITnose
How to write efficient HTML
Personal translation, welcome to reprint!
Original English text: https://samdutton.wordpress.com/2015/04/02/high-performance-html/
This is my first time translating a foreign blog post. Any comments and suggestions are welcome!
When seeing this question, most developers will think of image optimization, JavaScript optimization, server configuration upgrade, file compression and even CSS compression. . However, the core language of web pages, HTML, has been ignored.
Nowadays, the burden of HTML is getting heavier and heavier. Among the top 100 websites in the world, the average HTML code size of each page is about 40k, of which Amazon and Yahoo have an average of several thousand HTML codes per page. OK, Youtube’s homepage even has 3500 HTML elements. Although reducing the complexity of HTML per page and reducing the number of elements will not improve the loading time of the page a lot, good HTML coding habits are an important basis for improving the loading speed of web pages. The reason why I wrote this article is to tell you how to write clean and tidy HTML code, so that your web pages can load and run quickly and normally on many devices. In this process, you can learn how to build websites and apps that are easy to maintain and debug.
There are many ways to write code, especially HTML. This article only makes the relatively best suggestions based on our experience. It does not mean that every suggestion can achieve the effect under any circumstances. It is for reference only.
- Each one performs its own duties: it is enough to control the style with CSS, do not use HTML elements to forcefully obtain the desired style;
- Meticulous: be sure to add code verification tools during the development process to ensure that the code does not have any grammatical and logical errors;
- Clean and tidy: use automatic typesetting tools to maintain the consistency of code structure and format;
- Language proficiency: You should understand the usage of all elements and use more semantic elements in HTML;
- Treat everyone equally: All situations must be considered during the design process, and backup plans are very important. You should even use ARIA (Accessible Rich Internet Application) specifically for special groups to ensure that your website can be displayed through a screen reader or a plain text browser;
- Comprehensive testing: test your website through various tools How the website performs on different devices and different screen sizes.
There shouldn’t be much explanation on the meaning of HTML, please go to Baidu Encyclopedia.
The first thing I want to say is that although the two brothers HTML and CSS are passionate about each other, they cannot make the relationship too complicated. It is enough to control the style with CSS. Do not use HTML elements to forcefully obtain the desired style. , for example, don't use the 4a249f0d628e2318394fd9b75b4636b1, c1a436a314ed609750bd7c7d319db4da, 684271ed9684bde649abda8831d4d355 title tags just to make the text larger, and don't use the b8a712a75cab9a5aded02f74998372b4 tag just to indent it.
Chrome, Firefox, Internet Explorer and Opera all have their own default style sheets, and the default display mode of HTML elements is determined by these default style sheets. For example, the default style of 4a249f0d628e2318394fd9b75b4636b1 in Chrome is 32px bold, and the font is Times.
Three principles of friends:
HTML is used to establish the structure, and CSS is used For rendering styles, JavaScript is used to control behavior; First complete the design of HTML, then design CSS according to style requirements, and finally design JavaScript when really needed; Combine CSS and Archive the JavaScript files separately from the HTML files (this not only helps with page caching, but also makes later debugging easier). Afterwards, the CSS and JavaScript are linked to the HTML, and the CSS and JavaScript codes can be compressed as needed. encryption.
You need to pay attention to these when building the structure of HTML:
When adopting the HTML5 standard, the beginning should be added with 8b05045a5be5764f313ed5b9168a17e6 , like this:
<!DOCTYPE html><html> <head> <title>Recipes: pesto</title> </head> <body> <h1>Pesto</h1> <p>Pesto is good!</p> </body></html>
The CSS file should be introduced in the head tag, so that the browser can get the CSS information before outputting HTML:
<head> <title>My pesto recipe</title> <link rel="/css/global.css"> <link rel="css/local.css"></head>
Introduce JavaScript files at the end of the 6c04bd5ca3fcae76e30b72ad730ca86d tag, so that the JavaScript files can be compiled after the page is displayed to speed up page reading, and at the same time help JavaScript operate on elements in the page, like Like this:
<body> ... <script src="/js/global.js"> <script src="js/local.js"></body>
Operations on elements should be added in JavaScript code, not in HTML. The following example is wrong and will be difficult to maintain later.
<head> ... <script src="js/local.js"></head><body onload="init()"> ... <button onclick="handleFoo()">Foo</button> ...</body>
<head> ...</head><body> ... <button id="foo">Foo</button> ... <script src="js/local.js"></body>
***js/local.js***
javascriptinit(); var fooButton = document.querySelector('#foo');fooButton.onclick = handleFoo();
虽然现在浏览器的容错力越来越高,但这不能成为代码粗制滥造的借口。不管什么时候,正确的HTML代码都更易于debug、体积更小、运行更快、渲染时消耗资源更少,而错误的HTML代码只会使页面的最终设计难以实现想要的效果。特别是在使用模板来开发时,正确有效的HTML就更显得尤为重要,也许一个正常运行的模块会在其他环境中出现可怕的异常。
如何才能提高HTML的正确性呢?可以有这些方式:
确保正确的HTML层级:嵌套元素时确保元素首尾完整,在一个有大量内容的元素的结束标签后应添加注释,这样有助于后期debug,特别是在使用模板的时候,如下所示:
<div id="foobar"> ...</div> <!-- foobar ends -->
在所有不能自动结束的元素末尾添加结束标签;
<p>Pesto is good to eat...<p>...and pesto is easy to make.
<p>Pesto is good to eat...</p><p>...and pesto is easy to make.</p>
bed06894275b65c1ab86501b08a632eb结束标签不是必须的,所以有些人认为可以不写bed06894275b65c1ab86501b08a632eb,这可以接受,但是929d1f5ca49e04fdcb27f9465b944689和f6f112ef45f603be226bc581f9dd5e90标签一定要加:
<ul> <li>Basil <li>Pine nuts <li>Garlic</ul>
39000f942b2545a5315c57fa3276f220和b97864c2e0ef2353a16c4d64c7734e92元素必须要有结束标签:
<!-- 这样写不好 --><video src="foo.webm" /><!-- 还是这样写吧 --><video src="foo.webm"> <p>Video element not supported.</p></video>
另一方面,要去掉冗余代码:
布尔型的属性可以不赋值,只要该属性出现,值就为true;
<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>
外部链接可以省略协议部分(如http,ftp),这样可以避免内容太长而引起问题。像这样写是可以的:
<a href="//en.wikipedia.org/wiki/Tag_soup">Tag soup</a>
保持一致的代码排版可以使HTML代码更易于理解、优化和debug,要做到良好的代码排版应注意以下这几点:
用更直接易读的方式写HTML代码,比如这句话,就可以很明显的看出这是个标题:
<h2><a href="/contact">Contact</a><h2>
这样写的话就更像是一个链接:
<a href="/contact"><h2>Contact</h2></a>
使用小写字母来写标签和属性,大写字母很不易读,像这样:
<A HREF="/">Home</A>
混合式的写法简直就是反人类:
<H2>Pesto</h2>
语义化的意思是从名称一眼就能看出其内容和作用是什么,HTML的标签就是通过使用浅显易懂的元素名和属性名来实现语义化的。HTML5又引进了一些新的语义化元素,比如1aa9e5d373740b65a0cc8f0a02150c53, c37f8231a37e88427e62669260f0074d和c787b9a589a3ece771e842a6176cf8e9。
为了是你的代码更易理解,一定要针对内容使用相应的语义化元素:
不要把文字和元素混合在一起,这样容易导致布局出错,比如这样:
<div> Name: <input type="text"></div>
这样写会更好些:
<div> <label>Name:</label> <input type="text"></div>
首先再次强调一遍:
样式由CSS来控制就够了,不要用HTML元素来强行获取想要的样式。
布局需要注意的问题有这些:
这篇文章虽然是讲HTML的,但也有些CSS的注意事项想说一说:
有时候给父元素添加class要比给子元素添加更简洁易维护(class的命名方式可以采用BEM规则),像这样:
<!-- 这样看着好累>o< --> <ul> <li class="ingredient">Basil</li> <li class="ingredient">Pine nuts</li> <li class="ingredient">Garlic</li> </ul> <!-- 看起来舒服多了^v^ --> <ul class="ingredients"> <li>Basil</li> <li>Pine nuts</li> <li>Garlic</li> </ul>
要多为用户考虑,不同的设备条件、使用环境以及输入法等都会给你的HTML带来不同的体验:
有时全大写的标题会起到吸引注意力的作用,但没必要在HTML中真的输入大写的文字,可以在CSS中通过修改text-transform和font-variant来完成。这样做还有个好处,就是当用户复制这段文字时,他们可能不想要全大写的,像下面的3f7b3decd2dcafb07b84d2d3985d9f40,当用户复制文字内容时,得到的是大小写混合的文字:
HTML
<h4>W3C Web Accessibility Initiative ARIA guidance</h4>
CSS
h4 { font-variant: small-caps;}
重中之重,就是一定要做测试!
在工作流程、调试工具和部署过程中都可以加入HTML测试。一定要测试你的页面在不同条件的设备,不同大小的屏幕以及不同网速的环境下的读取情况。还要使用纯文字浏览器(如: Lynx)或者屏幕阅读器(如:ChromeVox)来测试页面在特殊环境下的交互性。可以使用Chrome Dev Tools device mode这种仿真器来监视页面的变化。工作流程中可以加入Page Speed, Web Page Test等工具来做自动化测试。