問題描述
div.container 比 .container 快嗎?你知道在 jquery 中,如果你更具體地使用你的選擇器,它會更快,因為它迭代更少.. css 是這種情況嗎?
Is div.container faster than .container ? You know like in jquery if you be more specific with your selectors it is faster since it iterates through less.. Is this the case with css ?
有沒有辦法衡量 css 的性能?性能方面,這樣的事情是否重要,或者它基本上都取決于文本重量?
Is there a way to measure performance in css ? Performance wise, does things like this even matter or does it all depend on text weight basically ?
如果有人知道答案,我會很高興,我實際上發現了一個沒有確定答案的類似問題.如果指定得更好,CSS會更高效嗎?
I'd be happy if someone knows the answer to it, I've actually found a similar question with no certain answer. Can CSS be more efficient if it is better specified?
推薦答案
在現實世界中,速度差異可以忽略不計.
技術方面的 .container
會更快,因為它需要處理的選擇器更少.
In real world the speed difference would be negligible.
To be technical .container
would be faster as it has fewer selectors to process.
選擇器具有固有的效率.CSS 選擇器從高到低的順序是這樣的:
Selectors have an inherent efficiency. The order of more to less efficient CSS selectors goes thus:
- ID,例如
#header
- 類,例如
.promo
- 類型,例如
div
- 相鄰的兄弟,例如
h2 + p
- 孩子,例如
李>ul
- 后裔,*例??如
ul a*
- 通用,即
*
- 屬性,例如
[type="text"]
- 偽類/元素,例如
a:hover
關于你的第二個問題:
有沒有辦法衡量 CSS 的性能?
Is there a way to measure performance in CSS ?
Steve Souders 推出了一項在線測試來衡量 CSS 的性能,仍然可以在此處訪問該測試.
Steve Souders put out an online test to measure performance of CSS that can still be accessed here.
現在有更好的方法來衡量性能,但這是一種快速簡便的資源,您可以使用.
There are better ways to measure performance nowadays, but this is a quick and easy resource you can play with.
在性能方面,這樣的事情是否重要,或者它基本上都取決于文本重量?
Performance wise, does things like this even matter or does it all depend on text weight basically ?
簡短的回答是不是真的".
The short answer is "not really".
答案很長,視情況而定".如果你在一個簡單的網站上工作,除了一般知識之外,你真的沒有必要對 CSS 性能大驚小怪,你可以從最佳實踐中獲得.
The long answer is, "it depends". If you are working on a simple site there is really no point to make a fuss about CSS performance other than general knowledge you may gain from best practices.
如果您要創建一個包含數萬個 DOM 元素的網站,那么是的,這很重要.
If you are creating a site with tens of thousands of DOM elements then yes, it will matter.
這篇關于當您特定時,CSS 會更快嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!