关于前端优化中的@import

研究web前端优化时最多的建议就是不要使用 css @import 由于用这种方式加载css至关于把css放在了html底部css

网上不少文章说起到的@import,其关注的问题是:html

不要使用 css @import, 由于用这种方式加载css至关于把css放在了html底部。前端

那为何用@import就等于把css放在了页面底部呢?在google developers看一篇文章时,无心间找到了这个缘由,原文以下:web

Avoid CSS @import Overview浏览器

Using CSS @import in an external stylesheet can add additional delays during the loading of a web page. Details前端优化

CSS @importallows stylesheets to import other stylesheets. When CSS @import isused from an external stylesheet, the browser is unable to downloadthe stylesheets in parallel, which adds additional round-trip timesto the overall page load. For instance, iffirst.css contains the following content:优化

@import url("second.css")ui

The browser must download, parse, andexecute first.css before it is able to discover that itneeds to downloadsecond.css. Recommendationsgoogle

Use the <link> tag instead of CSS @import Instead of @import, use a <link> tag for each stylesheet. This allows the browser to download stylesheets in parallel, which results in faster page load times:url

<link rel="stylesheet" href="first.css"> <link rel="stylesheet" href="second.css">

咱们确实要避免使用css @import, 但缘由却不是什么至关于放在了页面底部,而是这样作会致使css没法并行下载,由于使用@import引用的文件只有在引用它的那个css文件被下载、解析以后,浏览器才会知道还有另一个css须要下载,这时才去下载,而后下载后开始解析、构建render tree等一系列操做。

浏览器在页面全部css下载并解析完成后才会开始渲染页面(Before a browser can begin to render a web page, it mustdownload and parse any stylesheets that are required to lay out thepage. Even if a stylesheet is in an external file that is cached,rendering is blocked until the browser loads the stylesheet from disk.)

所以css @import引发的css解析延迟会加长页面留白期。 因此,要尽可能避免使用css @import而尽可能采用link标签的方式引入。

相关文章
相关标签/搜索