网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

web前端优化时为何不建议使用css@import-创新互联

今天就跟大家聊聊有关web前端优化时为何不建议使用css @import,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

成都创新互联公司-成都网站建设公司,专注网站设计制作、网站设计、网站营销推广,国际域名空间,雅安服务器托管网站托管有关企业网站制作方案、改版、费用等问题,请联系成都创新互联公司

曾经研究web前端优化时在网上多处看到这样一条建议,大意是:

不要使用 css @import, 因为用这种方式加载css相当于把css放在了html底部。

关于这一点,我一直很疑惑: 为什么用@import就等于把css放在了页面底部? 原理是什么?? 但一直不得而知,因为网络文章一大抄,转载的很多,去细究原因的却很少。

直到今天,在google developers看一篇文章时,无意间找到了这个原因,原文如下:

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")

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

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




我们确实要避免使用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标签的方式引入。

看完上述内容,你们对web前端优化时为何不建议使用css @import有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


名称栏目:web前端优化时为何不建议使用css@import-创新互联
本文URL:http://bjjierui.cn/article/dghecc.html

其他资讯