We think 提高
one of the most influential measures the performance of web pages, is loading so as not to delay the rendering of web pages CSS
.
By default, the browser loads CSS
when the termination style page rendering (synchronous load), which is loaded CSS
will block DOM树
rendering (but do not block DOM树
construction), can be simply understood as: When loading CSS
the same time, but also in the building DOM树
, but did not apply the style.
Rendering Process
Simply go over the browser rendering process:
- Load
HTML
Resources - Resolve
HTML
- Load
CSS
resources, while buildingDOM树
- Parsing
CSS
, and renderingDOM树
When CSS文件
too large, it will stay in 第3步
, so when Suman, open a Web site often encountered when no style case.
So we need to be loaded first part CSS
( 首屏
the need to use CSS
), other lower priority CSS
to asynchronously load. And script
elements of different, no async
or defer
attribute may be simply applied to link
the elements, but can be simulated.
Asynchronous loading
1. Using media queries
Set the value of a current browser does not support:
<link rel="stylesheet" href="./index.css" media="none" onload="this.media='all'">
Such asynchronous browser will load the CSS
file (low priority), after loading is complete, use the onload
property link
of the media type is set all
, then began to render.
If there is the following code:
<link rel="stylesheet" href="./index2.css" media="none" onload="this.media='all'">
<link rel="stylesheet" href="./index1.css">
web前端开发学习Q-q-u-n:784783012 ,分享学习的方法和需要注意的小细节,不停更新最新的教程和学习方法(详细的前端项目实战教学视频)
The browser loads the priority is as follows:
Or there is the following code:
<link rel="stylesheet" href="./index1.css" media="screen and (max-width: 800px)">
<link rel="stylesheet" href="./index2.css" media="screen and (min-width: 800px)">
When refresh the page, if the 视窗
width is less than 800px
, the priority load index1.css
, it is greater than if 800px
, on the contrary:
Summary: Media query does not match the style of low priority?.
2. Load resources in advance
This is similar with the above, but the priorities are 最高
, but still loaded asynchronously without blocking the DOM rendering, but browser support is relatively low?.
<link rel="preload" href="./index.css" as="style">
Tells the browser "请提前加载好此资源,我后面会用到!"
. When used, the browser cache begins to pick up?.
Therefore, the correct operation with media queries as:
<link rel="preload" href="./index.css" as="style" onload="this.rel='stylesheet'">
Of course, the property can also be applied to other resources when you need to use these resources, the browser will direct from 缓存
the take, not again send a request.
<link rel="preload" href="./index.js" as="script">
<link rel="preload" href="./index.png" as="image">
<link rel="preload" href="./index.mp4" as="video" type="video/mp4">
3. Aberdeen fishing wording
let link = document.createElement("link");
link.rel = "stylesheet";
link.href = "./index1.css";
document.head.appendChild(link);
web前端开发学习Q-q-u-n:784783012 ,分享学习的方法和需要注意的小细节,不停更新最新的教程和学习方法(详细的前端项目实战教学视频)
to sum up
Important style uses synchronous load (conventional writing), low priority using asynchronous loading, asynchronous loading will not block page rendering.
More and more people are discovering, may only need a simple HTML
method, while simple is often the best.
<link rel="stylesheet" href="./main.css">
<link rel="stylesheet" href="./other.css" media="none" onload="this.media='all'">