web前端優(yōu)化時為什么不建議使用css @import
發(fā)布時間:2013-12-27 15:33:05 作者:佚名
我要評論
研究web前端優(yōu)化時最多的建議就是不要使用 css @import 因為用這種方式加載css相當(dāng)于把css放在了html底部,關(guān)于這一點(diǎn)下面為大家詳細(xì)介紹下
曾經(jīng)研究web前端優(yōu)化時在網(wǎng)上多處看到這樣一條建議,大意是:
不要使用 css @import, 因為用這種方式加載css相當(dāng)于把css放在了html底部。
關(guān)于這一點(diǎn),我一直很疑惑: 為什么用@import就等于把css放在了頁面底部? 原理是什么?? 但一直不得而知,因為網(wǎng)絡(luò)文章一大抄,轉(zhuǎn)載的很多,去細(xì)究原因的卻很少。
直到今天,在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 <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:
<link rel="stylesheet" href="first.css">
<link rel="stylesheet" href="second.css">
我們確實(shí)要避免使用css @import, 但原因卻不是什么相當(dāng)于放在了頁面底部,而是這樣做會導(dǎo)致css無法并行下載,因為使用@import引用的文件只有在引用它的那個css文件被下載、解析之后,瀏覽器才會知道還有另外一個css需要下載,這時才去下載,然后下載后開始解析、構(gòu)建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標(biāo)簽的方式引入。
不要使用 css @import, 因為用這種方式加載css相當(dāng)于把css放在了html底部。
關(guān)于這一點(diǎn),我一直很疑惑: 為什么用@import就等于把css放在了頁面底部? 原理是什么?? 但一直不得而知,因為網(wǎng)絡(luò)文章一大抄,轉(zhuǎn)載的很多,去細(xì)究原因的卻很少。
直到今天,在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 <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:
<link rel="stylesheet" href="first.css">
<link rel="stylesheet" href="second.css">
我們確實(shí)要避免使用css @import, 但原因卻不是什么相當(dāng)于放在了頁面底部,而是這樣做會導(dǎo)致css無法并行下載,因為使用@import引用的文件只有在引用它的那個css文件被下載、解析之后,瀏覽器才會知道還有另外一個css需要下載,這時才去下載,然后下載后開始解析、構(gòu)建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標(biāo)簽的方式引入。
相關(guān)文章

CSS3打造的現(xiàn)代交互式登錄界面詳細(xì)實(shí)現(xiàn)過程
本文介紹CSS3和jQuery在登錄界面設(shè)計中的應(yīng)用,涵蓋動畫、選擇器、自定義字體及盒模型技術(shù),提升界面美觀與交互性,同時優(yōu)化性能和可訪問性,感興趣的朋友跟隨小編一起看看吧2025-06-20CSS普通流、浮動與定位網(wǎng)頁布局三大機(jī)制及最佳實(shí)踐
本文給大家講解CSS 的三種核心布局機(jī)制——普通流(Normal Flow)、浮動(Float)和定位(Positioning)對于創(chuàng)建靈活、響應(yīng)式的網(wǎng)頁至關(guān)重要,本文將深入探討這三種機(jī)制的工作原2025-06-19
css實(shí)現(xiàn)角標(biāo)效果并帶有文章或圖標(biāo)效果(完整代碼)
文章介紹如何用CSS實(shí)現(xiàn)角標(biāo)效果,通過.active類結(jié)合::after和::before偽元素,利用定位、邊框和旋轉(zhuǎn)創(chuàng)建紅色邊框與白色三角形的提示標(biāo)志,適用于按鈕或卡片元素的視覺增強(qiáng)設(shè)計2025-06-19CSS Anchor Positioning重新定義錨點(diǎn)定位的時代來臨(最新推薦)
CSS Anchor Positioning是一項仍在草案中的新特性,由 Chrome 125 開始提供原生支持需啟用實(shí)驗 flag,它允許你在 CSS 中通過錨點(diǎn)(anchor)來相對于任意 DOM 元素定位,本文2025-06-17CSS中的Static、Relative、Absolute、Fixed、Sticky的應(yīng)用與詳細(xì)對比
CSS 中的 position 屬性用于控制元素的定位方式,不同的定位方式會影響元素在頁面中的布局和層疊關(guān)系,以下是 static、relative、absolute、fixed、sticky 的詳細(xì)對比和應(yīng)用2025-06-17CSS place-items: center解析與用法詳解
place-items: center; 是一個強(qiáng)大的 CSS 簡寫屬性,用于同時控制 網(wǎng)格(Grid) 和 彈性盒(Flexbox) 布局中的對齊方式,本文給大家介紹CSS place-items: center; 詳解與用法2025-06-17
CSS實(shí)現(xiàn)元素?fù)螡M剩余空間的五種方法
在日常開發(fā)中,我們經(jīng)常需要讓某個元素占據(jù)容器的剩余空間,本文將介紹5種不同的方法來實(shí)現(xiàn)這個需求,并分析各種方法的優(yōu)缺點(diǎn),感興趣的朋友一起看看吧2025-06-17CSS中前端單位 px、vw、vh 等的區(qū)別與使用建議
CSS單位區(qū)別與使用場景總結(jié):px絕對、vw/vh響應(yīng)式,%繼承父尺寸,em/rem文字縮放,vmin/vmax適應(yīng)寬高變化,固定布局用px或%,響應(yīng)式布局用vw/vh/rem,文字用em或rem,本文給大家2025-06-16CSS 樣式表的四種應(yīng)用方式及css注釋的應(yīng)用小結(jié)
這篇文章主要介紹了CSS 樣式表的四種應(yīng)用方式及css注釋的應(yīng)用小結(jié),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2025-05-21- 在CSS布局中,padding屬性是控制元素內(nèi)容與其邊框之間距離的關(guān)鍵工具,本文介紹CSS基礎(chǔ)中padding,通過本文的介紹,我們深入了解了padding的基本概念、簡寫方法以及它對元2025-05-16




