特定于元素的 CSS 选择器不好吗?
p.error
比 .error
更好还是更差?
我读过,特定于元素的选择器很糟糕,只有在真正需要的时候才应该使用,但似乎没有人知道为什么。我的意思是我确实理解 .error
更适合代码重用,但是是否有某种特定原因为什么我不应该始终使用元素来处理类?
Is p.error
better or worse than .error
?
I have read that element-specific selectors are bad and should be used only if really needed but noone seems to know why. I mean I do understand that .error
is better for code reuse, but is there somekinda specific reason why I shouldn't address class with element always?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
CSS 选择器是从右到左读取的。因此,
p.error
比.error
多了一个步骤。这可能会导致较小的集合,也可能不会 - 取决于您的标记。然而,这是一个微观优化。除非我们讨论大量的选择器,否则不会影响性能。
这是一篇关于 CSS 选择器的精彩文章,详细介绍了如何评估它们: http://css-tricks .com/efficiently-rendering-css/
CSS selectors are read right to left. So
p.error
has one additional step to.error
. This may result in a smaller set or not - depends on your markup.However, this is a micro micro optimization. There is not going to be a performance hit unless we're talking about a massive amount of selectors.
Here's a great article on CSS selectors that elaborates on how they are evaluated : http://css-tricks.com/efficiently-rendering-css/
.error
比p.error
更有效。要了解为什么这样做更有效,我建议您阅读 这篇文章 的 css 技巧。
.error
is more efficient thanp.error
.To understand why this is more efficient I recommend you read this article over at css tricks.
不,这还不错,但它可能并不总是
像 Google 的 PageSpeed 和 YSlow 这样的必要工具!将这些类型的选择器称为“过度合格”,也许这就是您从 - 阅读材料
以
p#myid
为例 - 无论如何,ID 在页面上应该始终是唯一的,因此根本不需要限定它与p
元素一起使用。当计算特异性时,ID 已经具有最高的权重,因此添加额外的部分来尝试添加更多特异性是完全多余的。然而,对于像您的示例这样的类名,有时绝对需要添加限定符,因为您可能希望该类可在不同类型的元素上重用,但具有不同的属性,具体取决于它是
div
还是例如,“限定符”使选择器更加具体上面的代码意味着您可以在任何元素上使用
error
类,但是它的边距为 5px如果你在p
元素上设置错误类,第二个选择器实际上正在做一些事情,它会覆盖第一个选择器的边距,但仍然获得背景颜色所以它们完成了工作,但你经常看到许多人在不必要的时候过度限定所有元素。例如,如果您只将
.error
类应用于p
元素,那么您就不会需要第二个选择器。经验法则是从右侧开始尽快使选择器唯一。
no it's not bad, but it may not always be necessary
tools like Google's PageSpeed and YSlow! refer to these type of selectors as being "over qualified" perhaps that's where you're hearing the "it's bad" part from - reading material
take for example
p#myid
- an ID should always be unique on a page anyway, therefore there is no need at all to qualify it with thep
element. an ID already has the highest weight when specificity is being counted so again it's totally redundant to add the extra part to try and add more specificty.However with class names like your example it can sometimes definitely be desirable to add the qualifier as you may want the class to be re-usable on different type elements but have different properties depending on if it's a
div
or ap
for example, the "qualifier" then makes the selector slightly more specificThe code above means you can use the
error
class on any element and it will have 5px margins however if you set the error class on ap
element the second selector is actually doing something, it's over-riding the first's margins but still getting the background colorSo they do a job, but too often you see too many people over qualifying all their elements when it is not necessary.. for example if you're only ever applying that
.error
class to ap
element then you wouldn't need the second selector.The rule of thumb is to make the selector unique as quickly as possible starting from the right side of it.
拥有一个非常具体的选择器不会导致性能不佳,但如果有很多声明适用于一个元素,那么性能就会受到影响。否则唯一的问题是它会增加 no 。加载样式表时要下载的字节数。相信我,HTML 中传递的每个额外字符都是有害的,并且会降低页面加载速度。
在现代浏览器应用 CSS 级联期间,以下是每个 CSS 属性发生的过程:每个网页元素:
从所有来源收集该属性的所有声明。这包括默认浏览器样式和自定义用户样式以及作者样式表。如果有多个,请继续执行 2。
按重要性和来源对声明进行排序(从最低优先级到最高优先级):
优先级最高的获胜。如果多个声明具有相同的优先级,则继续执行 3。
按选择器特异性排序。具有最具体选择器的获胜者。如果没有明显的获胜者,请继续执行 4。
源中最后出现的获胜!
如果级联未在元素上设置 CSS 属性,则浏览器将回退到使用从元素的父级继承的属性(这只发生在某些属性上),否则该属性将设置为 CSS 默认值。
根据上面的过程,如果你使用很多更具体的选择器,那么至少在3层深度之后才会做出选择。因此,越多则没有。可能适用于某个元素的声明越多,性能就越低。
因此,您必须尽可能具体。
Having a very specific selector will not amount to bad performance, but if there are a lot of declarations applicable for an element, then the performance will take a hit. The only concern otherwise is that it increases the no. of bytes to be downloaded for loading the stylesheet. Trust me, Every extra character in HTML passed is evil and will amount to lower page load speed.
During CSS cascading is applied by modern-day browsers, the following is the process that occurs for each CSS property for each web page element:
Gather all the declarations for the property from all the sources. This includes default browser styles and custom user style, as well as author style sheets. If there is more than one, proceed to 2.
Sort the declarations by importance and origin in the following order (from lowest to highest priority):
The one with the highest priority wins. If more than one have the same priority then proceed to 3.
Sort by selector specificity. The one with the most specific selector wins. If no clear winner, proceed to 4.
The one that comes last in the source wins!
If the cascade does not set a CSS property on an element, then the browser will fall back to using an inherited property from the element’s parent (this only happens for some properties), otherwise the property is set to the CSS default value.
According to the above process, if you use a lot of more specific selectors, there would be a choice made after atleast 3 levels deep. Hence, the more the no. of declarations which might be applicable to an element, the lower the performance would be.
So, You must as specific as it makes sense to be.
原因是特殊性。例如...
因此,如果您有一个类和一个标签访问,则该样式的特异性为 2 (1+1)。
稍后,如果您尝试设置所有
.error
元素的样式,但p.error
元素中的样式发生冲突,则较高的特异性将获胜。这可能会导致一些令人头痛的问题。这就是为什么你可能不想总是使用标签+类。(话虽这么说,特异性解决的问题比它产生的问题多得多,并且通常被认为是非常棒的。)
The reason is specificity. For example...
So, if you have a class and a tag access, that style has a specificity of 2 (1+1).
Later, if you're trying to style all
.error
elements, but you have a conflicting style in thep.error
elements, the higher specificity will win. This may cause some headaches down the line. That is why you may not want to always use tag+class.(That being said, specificity solves many more problems than it creates, and is generally regarded as Pretty Awesome.)
作为一般经验法则,浏览器评估的选择器越少越好。
如果
.error
用于多个标记,则p.error
不一定比.error
“更糟”。例如div.error
(请参阅底部的脚注)。但如果它只在一个段落中使用,那么拥有
p.error
只会让浏览器更加努力地工作,即首先它必须找到具有类属性
error
的所有元素然后仅通过p
标签来过滤它们。以下是一些有关优化浏览器渲染对 Google 页面速度的有趣读物地点。
脚注
但是,如果您需要在多个标签上使用一个类,最好只放入适用于这些标签的 css 样式,而不是尝试将其分开。例如
,无论如何,这种方式都不需要为类添加标签前缀。
我希望这有帮助!
As a general rule of thumb, the less selectors a browser has to evaluate the better.
p.error
isn't necessarily "worse" than.error
, if.error
is used for multiple tags. e.g.div.error
(see a foot note at the bottom).But if it's only used on a paragraph anyway, then having
p.error
is just making the browser work harder i.e.First it will have to find all elements with the class attribute
error
and then filter these by only having tags that arep
.Here is some interesting reading on Optimize browser rendering on Google's Page Speed site.
Foot Note
However if you need to use a class on multiple tags, it's probably best only to put in the css styles which apply to those tags instead of trying to separate it. e.g.
So that kind of defeats the need to prefix classes with tags anyway.
I hope this helps!