CSS选择器性能 [英] CSS selector performance
问题描述
我知道性能差异很小,但是它是一个更快的CSS选择器?
I know the performance difference is miniscule, but which is a faster CSS selector?
div.class{ }
或
.class{ }
推荐答案
使用 div.class $ c $ c>如果你需要一些样式只适用于该类的
div
元素,否则使用 .class
根据您的样式需求所做的决定,而不是一些无限小的性能优势。
Use div.class
if you need some styles to only apply to div
elements with that class, use .class
otherwise. Base your decision on what your styling needs are, not some infinitesimal performance benefit.
注意:有一些选择器(相对)慢,可能值得改变,例如 .class> *
。 但,即使对于性能非常差的选择器,即使你处于一个合理的阶段,你的项目开始考虑优化的东西,你应该担心的事情,获得CSS选择器优化。
Note: There are some selectors that really are (relatively) slow and might be worth changing, things like .class > *
. But, even for selectors with really bad performance, and even if you're at a reasonable stage in your project to start thinking about optimizing things, there are exactly a million things you should worry about first before you get to CSS selector optimization.
这篇关于CSS选择器性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!