web前端優(yōu)化時(shí)為什么不建議使用css @import
發(fā)布時(shí)間:2013-12-27 15:33:05 作者:佚名
我要評(píng)論

研究web前端優(yōu)化時(shí)最多的建議就是不要使用 css @import 因?yàn)橛眠@種方式加載css相當(dāng)于把css放在了html底部,關(guān)于這一點(diǎn)下面為大家詳細(xì)介紹下
曾經(jīng)研究web前端優(yōu)化時(shí)在網(wǎng)上多處看到這樣一條建議,大意是:
不要使用 css @import, 因?yàn)橛眠@種方式加載css相當(dāng)于把css放在了html底部。
關(guān)于這一點(diǎn),我一直很疑惑: 為什么用@import就等于把css放在了頁(yè)面底部? 原理是什么?? 但一直不得而知,因?yàn)榫W(wǎng)絡(luò)文章一大抄,轉(zhuǎn)載的很多,去細(xì)究原因的卻很少。
直到今天,在google developers看一篇文章時(shí),無(wú)意間找到了這個(gè)原因,原文如下:
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)于放在了頁(yè)面底部,而是這樣做會(huì)導(dǎo)致css無(wú)法并行下載,因?yàn)槭褂聾import引用的文件只有在引用它的那個(gè)css文件被下載、解析之后,瀏覽器才會(huì)知道還有另外一個(gè)css需要下載,這時(shí)才去下載,然后下載后開(kāi)始解析、構(gòu)建render tree等一系列操作。 星球?yàn)g覽器在頁(yè)面所有css下載并解析完成后才會(huì)開(kāi)始渲染頁(yè)面(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解析延遲會(huì)加長(zhǎng)頁(yè)面留白期。 所以,要盡量避免使用css @import而盡量采用link標(biāo)簽的方式引入。
不要使用 css @import, 因?yàn)橛眠@種方式加載css相當(dāng)于把css放在了html底部。
關(guān)于這一點(diǎn),我一直很疑惑: 為什么用@import就等于把css放在了頁(yè)面底部? 原理是什么?? 但一直不得而知,因?yàn)榫W(wǎng)絡(luò)文章一大抄,轉(zhuǎn)載的很多,去細(xì)究原因的卻很少。
直到今天,在google developers看一篇文章時(shí),無(wú)意間找到了這個(gè)原因,原文如下:
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)于放在了頁(yè)面底部,而是這樣做會(huì)導(dǎo)致css無(wú)法并行下載,因?yàn)槭褂聾import引用的文件只有在引用它的那個(gè)css文件被下載、解析之后,瀏覽器才會(huì)知道還有另外一個(gè)css需要下載,這時(shí)才去下載,然后下載后開(kāi)始解析、構(gòu)建render tree等一系列操作。 星球?yàn)g覽器在頁(yè)面所有css下載并解析完成后才會(huì)開(kāi)始渲染頁(yè)面(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解析延遲會(huì)加長(zhǎng)頁(yè)面留白期。 所以,要盡量避免使用css @import而盡量采用link標(biāo)簽的方式引入。
相關(guān)文章
- CSS Grid 是一種二維布局系統(tǒng),可以同時(shí)控制行和列,相比 Flex(一維布局),更適合用在整體頁(yè)面布局或復(fù)雜模塊結(jié)構(gòu)中,這篇文章主要介紹了前端CSS Grid 布局詳解,需要的朋2025-04-16
CSS Padding 和 Margin 區(qū)別全解析
CSS 中的 padding 和 margin 是兩個(gè)非常基礎(chǔ)且重要的屬性,它們用于控制元素周?chē)目瞻讌^(qū)域,本文將詳細(xì)介紹 padding 和 margin 的概念、區(qū)別以及如何在實(shí)際項(xiàng)目中使用它們2025-04-07- will-change 是一個(gè) CSS 屬性,用于告訴瀏覽器某個(gè)元素在未來(lái)可能會(huì)發(fā)生哪些變化,本文給大家介紹CSS will-change 屬性詳解,感興趣的朋友一起看看吧2025-04-07
- 本文給大家分享在 CSS 中,去除a標(biāo)簽(超鏈接)的下劃線的幾種方法,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-04-07
- 在前端開(kāi)發(fā)中,CSS(層疊樣式表)不僅是用來(lái)控制網(wǎng)頁(yè)的外觀和布局,更是實(shí)現(xiàn)復(fù)雜交互和動(dòng)態(tài)效果的關(guān)鍵技術(shù)之一,隨著前端技術(shù)的不斷發(fā)展,CSS的用法也日益豐富和高級(jí),本文將2025-04-07
css中的 vertical-align與line-height作用詳解
文章詳細(xì)介紹了CSS中的`vertical-align`和`line-height`屬性,包括它們的作用、適用元素、屬性值、常見(jiàn)使用場(chǎng)景、常見(jiàn)問(wèn)題及解決方案,感興趣的朋友跟隨小編一起看看吧2025-03-26淺析CSS 中z - index屬性的作用及在什么情況下會(huì)失效
z-index屬性用于控制元素的堆疊順序,值越大,元素越顯示在上層,它需要元素具有定位屬性(如relative、absolute、fixed或sticky),本文給大家介紹CSS 中z - index屬性的作用2025-03-21- 文章詳細(xì)介紹了CSS中的打印媒體查詢@mediaprint包括基本語(yǔ)法、常見(jiàn)使用場(chǎng)景和代碼示例,如隱藏非必要元素、調(diào)整字體和顏色、處理鏈接的URL顯示、分頁(yè)控制、調(diào)整邊距和背景等2025-03-18
CSS模擬 html 的 title 屬性(鼠標(biāo)懸浮顯示提示文字效果)
本文介紹了如何使用CSS模擬HTML的title屬性,通過(guò)鼠標(biāo)懸浮顯示提示文字效果,通過(guò)設(shè)置`.tipBox`和`.tipBox.tipContent`的樣式,實(shí)現(xiàn)了提示內(nèi)容的隱藏和顯示,感興趣的朋友一起2025-03-10前端 CSS 動(dòng)態(tài)設(shè)置樣式::class、:style 等技巧(推薦)
本文介紹了Vue.js中動(dòng)態(tài)綁定類(lèi)名和內(nèi)聯(lián)樣式的兩種方法:對(duì)象語(yǔ)法和數(shù)組語(yǔ)法,通過(guò)對(duì)象語(yǔ)法,可以根據(jù)條件動(dòng)態(tài)切換類(lèi)名或樣式;通過(guò)數(shù)組語(yǔ)法,可以同時(shí)綁定多個(gè)類(lèi)名或樣式,此外2025-02-26